连续的1位递增


36

给定Bits的模式(字符串或数组格式): [0,1,1,1,0,1,1,0,0,0,1,1,1,1,1,1]

任务是用从1开始的升序数字序列替换任意数量的连续1位。

输入值

  • 模式(可以作为字符串或数组接收)示例:
    • 串: 1001011010110101001
    • 数组: [1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1]

输出量

  • 升序数字序列(可以作为字符串或数组返回)示例:
    • 串: 1 0 0 1 0 1 2 0 1 0 1 2 0 1 0 1 0 0 1
    • 数组: [1, 0, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1]

规则

  • (仅适用于字符串)输入不会在1和之间包含空格0
  • 假设输入 length > 0
  • (仅适用于字符串)输出以空格分隔(如果您不需要数字或字母中的字母,请使用任何其他分隔符)

例:

Given [0,1,1,1,0,1,1,0,0,0,1,1,1,1,1,1] 
Output [0,1,2,3,0,1,2,0,0,0,1,2,3,4,5,6]

--------------------------------------------------------------------------

Given 0110101111101011011111101011111111     
Output 0 1 2 0 1 0 1 2 3 4 5 0 1 0 1 2 0 1 2 3 4 5 6 0 1 0 1 2 3 4 5 6 7 8

---------------------------------------------------------------------------

Given 11111111111101    
Output 1 2 3 4 5 6 7 8 9 10 11 12 0 1

获奖标准:Codegolf

Answers:


19

05AB1E,4个字节

γ€ƶ˜

在线尝试! 或作为测试服

说明

γ      # split input into groups of consecutive equal elements
 €ƶ    # multiply each number in each sublist by its 1-based index in the sublist
   ˜   # flatten

1
哎呀,比我好。我永远不会想到这一点。
魔术章鱼缸

3
我不是100%熟悉codegolf字节计数规则(并且Google搜索只发现了这篇没有得出结论的帖子)。当您的答案是4个字符时,不应至少8个字节(例如,不带BOM的utf-16-be 03 B3 20 AC 01 B6 02 DC),9个字节(utf-8:)CE B3 E2 82 AC C6 B6 CB 9C或10个字节(例如,包含2字节BOM的UTF-16)在任何非玩具编码中?(是的,可以构造一个类似于iso-8859编码的玩具8位编码,将这4个符号表示为1字节,但这似乎是作弊。)
jimbob博士

6
@drjimbob是的,很好的问题。实际上,可以使用05AB1E代码页将代码转换为二进制文件。例如,γ€ƶ˜将表示为04 80 8F 98。代码页的主要目的是使编写代码更加容易。要运行这个4字节的文件,您需要运行带有--osabie标志的解释器。
阿德南(Adnan)

18

Haskell,15个字节

scanl1$(*).succ

在线尝试!

解释/取消

scanl1 使用获取最终结果的函数从列表的左侧进行迭代,而当前元素使用结果生成一个新列表,从而使空列表和单例保持“未修改”状态。

(*).succ 等于 \x y-> (x+1)*y

scanl1只能将函数与该函数一起使用,因为递增的序列(1,2,3,..)以1开头,并且没有前一个元素(在这种情况下,它是列表中的第一个元素,不会被“修改”)或者他们的前导0



14

外壳5 4 3字节

ṁ∫g

在线尝试!

说明

ṁ∫g  -- full function, example input: [1,1,1,0,1]
  g  -- group: [[1,1],[0],[1]]
ṁ    -- map the following and concatenate result (example with [1,1,1])
 ∫   -- | cumulative sum: [1,2,3]
     -- : [1,2,3,0,1]

编辑历史

-1使用字节scanl1以上zipWith

由-1移植字节丹尼斯溶液



11

JavaScript(ES6),22个字节

将输入作为数组。

a=>a.map(s=n=>s=n*-~s)

在线尝试!

a=>a.map(n=>a=n*-~a)不幸的是,较短的(20个字节)将失败,[1]因为将单例数组强制转换为它们所持有的整数。




6

果冻,4字节

‘×¥\

在线尝试!

‘×¥\
   \   Accumulate the input with:
  ¥   The dyad
‘      Increment the left element
 ×    Multiply by the second element (1 or 0)
       The result always begins with the first element unchanged

6

K(oK)11 8字节

解:

{y*1+x}\

在线尝试!

说明:

遍历列表。累加器,乘以当前项(如果项为0,则重置累加器):

{y*1+x}\ / the solution
{     }\ / iterate (\) over lambda function
     x   / accumulator
   1+    / add 1
 y*      / multiply by current item

5

果冻,4字节

ŒgÄF

在线尝试!

怎么运行的

ŒgÄF  Main link. Argument: A (bit array)

Œg    Group adjacent, identical bits.
  Ä   Accumulate; take the cumulative sum of each chunk.
   F  Flatten.

随着组的快速运行,Erik建议这将是三个字节!(如果我理解它将正确执行的操作)
dylnan

@dylnan问题在于很难如此快速地决定行为。:(这就是为什么快仍处于间断。
埃里克Outgolfer

主要的可能实现可能有多个技巧
dylnan

5

R46 31字节

function(a)sequence(rle(a)$l)*a

在线尝试!

sequence这里非常方便,它“主要与R的早期历史有关”

function(a)                       # function, taking a vector as argument
                    rle(a)$l      # take the lengths of the run-length encoding
           sequence(        )     # and generate the list [1:x for x in lengths]
                             *a   # multiply by a to maintain 0s, and return

5

RAD,8个字节

(⊢×1+⊣)⍂

在线尝试!

怎么样?

  • (⊢×1+⊣)如果右边的参数是0,则返回0,否则增加左边的参数
  • LTR Scan((A f B) f C而不是A f (B f C)),将其应用于整个阵列

4

Japt,7 6 5字节

åÏ*°X

试试吧


说明

åÏ        :Cumulatively reduce
   °X     :  Increment the current total (initially 0)
  *       :  Multiply by the current element

4

Java 8,55 48字节

a->{int p=0,i=0;for(int v:a)a[i++]=v<1?p=0:++p;}

修改输入数组,而不是返回一个新的数组以节省字节。

-7个字节,感谢@TimSeguine

在线尝试。

说明:

a->{             // Method with integer-array parameter and no return-type
  int p=0,       //  Previous integer, starting at 0
      i=0;       //  Index-integer, starting at 0
  for(int v:a)   //  Loop over the values of the input-array:
    a[i++]=v<1?  //   If the current value is 0:
          p=0    //    Reset the previous integer to 0
         :       //   Else:
          ++p;}  //    Increase `p` by 1 first with `++p`
                 //    and set the current item to this new value of `p`

1
您可以将其剃光到48:a->{int p=0,i=0;for(int b:a)a[i++]=b<1?p=0:++p;}
Tim Seguine

@TimSeguine谢谢!现在,我看到了,我不敢相信自己还没有考虑过。
凯文·克鲁伊森

1
我能够摆脱p,但是它的大小是相同的:(a->{int i=0;for(int v:a)a[i]+=v*i++<1?0:a[i-2];}
Tim Seguine

4

TIS,68 + 33 = 101字节

代码(68字节):

@0
MOV UP ACC
SUB 47
MOV ACC ANY
@1
ADD 1
JRO UP
SUB ACC
MOV ACC ANY

布局(33字节):

2 1 CC I0 ASCII - O0 NUMERIC - 32

在线尝试!

说明:

|    Input 0    |    Input is given in ASCII (`0` is 48, `1` is 49)
+--------+------+
| Node 0 |      |    This node prepares the input data
+--------+      |
| MOV UP ACC    |    Read in a character
| SUB 47        |    Subtract 47 to map [48, 49] to [1, 2]
| MOV ACC ANY   |    Send the 1 or 2 to the next node
|               |    Implicitly wrap back to top of node
+--------+------+
| Node 1 |      |    This node does the incrementing/printing
+--------+      |
| ADD 1         |    Increment counter (starts at zero)
| JRO UP        |    Get value from above, and jump forward that many lines  (skip next line or not)
| SUB ACC       |    Reset counter to zero (if input was zero)
| MOV ACC ANY   |    Send the counter value downward to be printed
|               |    Implicitly wrap back to top of node
+---------------+
|   Output 0    |    Output is space-delimited numeric values

4

盖亚,5个字节

ẋ+⊣¦_

在线尝试!

说明

ẋ+⊣¦_     Full program
ẋ         Split into chunks of equal adjacent values.
   ¦_     And for each chunk, flattening the result afterwards...
 +⊣       Reduce it cumulatively on + (addition); aka cumulative sums

gh,我以为SE代码字体是等宽字体。


它们是等距的...第一行中没有空格。
micsthepick '18

查看编辑。它仍然未对齐。
Xcoder先生

您必须从移动设备或其他设备上看-对我来说很好
micsthepick '18



4

Perl 6的29个24 18字节

-6个字节,感谢肖恩!

*.map:{($+=1)*=$_}

在线尝试!

内部函数可以通过($+=1)*=*,但是匿名变量将在函数调用之间保留。我们通过将其包装在显式代码块中来实现此目的。

说明:

*.map:               # Map the array to
      {($+=1)    }   # The anonymous variable incremented
             *=$_    # Multiplied by the element

我将相同的基本方法缩减到16个字节:*.map(($+=1)*=*)。此解决方案的前提条件是状态变量$在对函数的调用之间仍然存在,因此,如果传递给一个调用的最后一个元素和传递给下一个调用的第一个元素都不为零,则计数将从错误的数字开始。
肖恩

@Sean,是的,当我最初回答时,我还为此感到挣扎。幸运的是,自那时以来,我已经学到了解决方法
Jo King

您可以再删除一个字节:*.map:{...}
肖恩



3

Pyth,6个字节

m=Z*hZ

在这里尝试!

怎么运行的

m = Z * hZ –完整程序。Q =评估输入。
m –对于Q中的每个整数d。
 = Z –将变量Z(预初始化为0)分配给...
   * hZ –(Z +1)* d; (d在末尾是隐式的)。



3

QBasic,60个字节

INPUT s$
FOR i=1TO LEN(s$)
b=MID$(s$,i)>="1
v=-b*v-b
?v
NEXT

将输入作为字符串;给出以换行符分隔的数字形式的输出。

说明

我们读取字符串s$和循环i1到它的长度。

MID$(s$,i)获取从字符i(1索引)到字符串结尾的子字符串。如果以a开头1,则在字典上>=是字符串"1"; 如果以a开头0,则不会。因此b得到0,如果在索引中的字符i0,或-1如果人品1

接下来,我们更新当前值v。如果我们只阅读a 0,我们v就会成为0; 否则,我们要加v一。换句话说,v = (-b) * (v+1); 简化数学将使代码中的表达式更短。最后,我们打印v并循环。


3

Brain-Flak,60字节

([]){{}<>(())<>{{}<>({}({}))(<>)}{}([])}{}<>{({}[()]<>)<>}<>

在线尝试!

说明:

([]){  For each element in the input
    {}
    <>(())<>  Push a one to the other stack
    { If the element is one,
       {}<>({}({}))(<>)  Add the one to a copy of the previous number in the series
    }{}  Pop the element
([])}  End loop
{}<>   Pop extra zero
{({}[()]<>)<>}<>   And reverse the output stack, subtracting one from each element


3

C(gcc),57 52 51字节

f(a,l,c,i)int*a;{for(c=i=0;i<l;)a[i++]=c=a[i]*-~c;}

Arnauld的JavaScript答案端口,就地修改了数组。在这里在线尝试。


说这是K&R C会更准确吗?
蒂姆·塞吉恩

可能,但是很多答案都是如此。我不是专家,但是完全有可能甚至没有有效的K&RC。问题是,我们实际上并不关心此站点上的语言标准。如果gcc允许您将K&R C与更现代的东西混合使用,那么对于高尔夫而言,它是有效的C,因为gcc会对其进行编译。另请参阅:codegolf.stackexchange.com/questions/2203/tips-for-golfing-in-c
OOBalance

直到现在搜索,我才意识到C11仍然支持旧的标识符列表函数语法,所以不要紧。但是你的观点不管。
蒂姆·塞吉

1
建议f(a,l,c)int*a;{for(c=0;l--;)c=*a++*=c+1;}

3

莎士比亚,365字节

I.Ajax,.Ford,.Act I:.Scene I:.[enter Ajax and Ford]Ajax:Open mind!Scene V:.Ford:Am I nicer than the sum of a big red old cute hard cat a big red old cute joy?Ford:If so,you is the sum of thyself a son!Ford:If not,you is zero!Ford:Open heart!Ajax:you is a big red old cute hard cat.Ajax:Speak mind!Ajax:Open mind!Ford:Am I nicer than zero?Ajax:If so, let us Scene V.

在这里尝试

少打高尔夫球

I.Ajax,.Ford,.
Act I:.
Scene I:.
[enter Ajax and Ford]
Ajax:Open mind!
Scene V:.
Ford:Am I nicer than the sum of a big red old cute hard cat a big red old cute joy?     <- smallest way to 48 (ascii "0") I could think of
Ford:If so,you is the sum of thyself a son!
Ford:If not,you is zero!
Ford:Open heart!
Ajax:you is a big red old cute hard cat.    <- get value of 32 or space
Ajax:Speak mind!                            <- then output it
Ajax:Open mind!
Ford:Am I nicer than zero?
Ajax:If so, let us Scene V.                 <- loop through inputs

280个字节。请查看SPL技巧页面以获取高尔夫技巧。
乔·金

3

C ++,47个字节

[](int*a,int*b){for(int c=0;a!=b;)c=*a++*=1+c;}

给定开始和结束指针的lambda修改数组。


在线尝试!(需要Javascript)


通用版本为55个字节(适用于具有算术类型元素的任何容器):

[](auto a,auto b){for(auto c=*a-*a;a!=b;)c=*a++*=1+c;};
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.