# Consecutive 1-Bits are Incremented

Given a pattern (string or array format) of Bits : [0,1,1,1,0,1,1,0,0,0,1,1,1,1,1,1]

The tasks is to replace any number of consecutive 1-Bits with an ascending number sequence starting at 1.

Input

• Pattern (can be received as an string or array) Example:
• String: 1001011010110101001
• Array: [1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1]

Output

• Ascending number sequence (can be returned as an string or array) Example:
• String: 1 0 0 1 0 1 2 0 1 0 1 2 0 1 0 1 0 0 1
• Array: [1, 0, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1]

Rules

• (only apply for strings) Input wont contain spaces between 1 and 0
• Assume Input length > 0
• (only apply for strings) Output is separated by space (use any other separator if you need as long as is not a number or a letter from the alphabet)

Example:

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


Winning criteria: Codegolf

# 05AB1E, 4 bytes

γ€ƶ˜


Try it online! or as a Test Suit

Explanation

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

• Oof, better than mine. I would've never thought of this. – Magic Octopus Urn Jun 29 '18 at 13:25
• I'm not 100% familiar with codegolf byte-counting rules (and googling only found this post which didn't come to a conclusion). While your answer is 4 characters shouldn't it at least be 8 bytes (e.g., utf-16-be without BOM 03 B3 20 AC 01 B6 02 DC) or 9 bytes (utf-8: CE B3 E2 82 AC C6 B6 CB 9C) or 10 bytes (e.g., UTF-16 including the 2 byte BOM) in any non-toy encoding? (Yes, one could construct a toy 8-bit encoding similar to iso-8859 encodings with these 4 symbols represented as 1-byte, but that seems like cheating.) – dr jimbob Jun 30 '18 at 14:10
• @drjimbob Yes, good question. The code can actually be converted to a binary file using the 05AB1E code page. For example, γ€ƶ˜ would be represented as 04 80 8F 98. The code page primarily exists to make writing code easier. To run this 4-byte file, you would need to run the interpreter with the --osabie flag. – Adnan Jun 30 '18 at 19:00

scanl1$(*).succ  Try it online! ### Explanation/Ungolfed scanl1 iterates from left over a list using a function which takes the last result and the current element generating a new list with the results, leaving empty lists and singletons "unmodified". (*).succ is the equivalent of \x y-> (x+1)*y Using that function together with scanl1 only works because the increasing sequences (1,2,3,..) start with 1 and either have no preceding element (in which case it's the first element in the list which won't be "modified") or they have a leading 0. # Python 2, 36 bytes c=0 for i in input():c=c*i+i;print c  Try it online! # Husk, 5 4 3 bytes ṁ∫g  Try it online! ### Explanation ṁ∫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]  Edit history -1 byte by using scanl1 over zipWith -1 byte by porting Dennis's solution # APL (Dyalog Unicode), 5 bytes ⊥⍨¨,\  Try it online! ### How it works ⊥⍨¨,\ ,\ ⍝ Convert to lists of first n elements ⊥⍨¨ ⍝ Map "Count trailing ones" to each list  • Nice usage of the ⊥⍨ trick. – Zacharý Jun 30 '18 at 18:52 # JavaScript (ES6), 22 bytes Takes input as an array. a=>a.map(s=n=>s=n*-~s)  Try it online! The shorter a=>a.map(n=>a=n*-~a) (20 bytes) would unfortunately fail on [1] because of coercion of singleton arrays to the integer they're holding. # J, 4 bytes #.~\  A port of Bubbler's APL solution Try it online! # J, 8 bytes i.&0@|.\  ## How? It's simply the distance to the preceding 0  \ for each prefix |. reverse it @ and i.&0 find the index of the first 0  Try it online! # Python 2, 39 38 bytes -1 byte thanks to Erik the Outgolfer i=1 for x in input():i*=x;print i;i+=1  Try it online! • I don't think you need the ,. – Erik the Outgolfer Jun 29 '18 at 14:13 • @EriktheOutgolfer it looks prettier this way c: – Rod Jun 29 '18 at 14:20 • Sorry, but sometimes, in life, you have to make sacrifices. – Erik the Outgolfer Jun 29 '18 at 14:29 • RIP , you're not in the code anymore, but you will be forever in my heart – Rod Jun 29 '18 at 14:45 # Jelly, 4 bytes ‘×¥\  Try it online! ‘×¥\ \ 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  # K (oK), 11 8 bytes Solution: {y*1+x}\  Try it online! Explanation: Iterate over the list. Increment accumulator, multiply by current item (which resets accumulator if item is 0): {y*1+x}\ / the solution { }\ / iterate (\) over lambda function x / accumulator 1+ / add 1 y* / multiply by current item  # Jelly, 4 bytes ŒgÄF  Try it online! ### How it works ŒgÄF Main link. Argument: A (bit array) Œg Group adjacent, identical bits. Ä Accumulate; take the cumulative sum of each chunk. F Flatten.  • With the group runs quick Erik had suggested this would be three bytes! (If I understood what it would do correctly) – dylnan Jun 29 '18 at 15:02 • @dylnan The problem is that it's hard to decide on a behavior for such a quick. :( That's why the quick is still in hiatus. – Erik the Outgolfer Jun 29 '18 at 15:07 • There could be multiple quicks for the main possible implementations – dylnan Jun 29 '18 at 15:08 # R, 46 31 bytes function(a)sequence(rle(a)$l)*a


Try it online!

sequence, which "mainly exists in reverence to the very early history of R", is quite handy here.

function(a)                       # function, taking a vector as argument

### Explanation:

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

• I got the same basic approach down to 16 bytes: *.map(($+=1)*=*). This solution has the proviso that the state variable $ persists across calls to the function, so if the final element passed to one call and the first element passed to the next call are both nonzero, then the counting will start with the wrong number. – Sean Oct 31 '18 at 23:31
• @Sean, Yeah I remember struggling with that when I originally answered. Fortunately I've learned a way around that since then – Jo King Nov 1 '18 at 0:15
• You can knock one more byte off: *.map:{...}. – Sean Nov 1 '18 at 0:19

# Jelly, 5 bytes

ṛȧ+ɗ\


Try it online!

FOR i=1TO LEN(s$) b=MID$(s$,i)>="1 v=-b*v-b ?v NEXT  Takes the input as a string; gives the output as numbers separated by newlines. ### Explanation We read the string s$ and loop i from 1 up to its length.

MID$(s$,i) gets the substring from character i (1-indexed) to the end of the string. If this starts with a 1, it will be lexicographically >= the string "1"; if it starts with a 0, it will not be. So b gets 0 if the character at index i is 0, or -1 if the character is 1.

Next, we update the current value v. If we just read a 0, we want v to become 0; otherwise, we want to increment v by one. In other words, v = (-b) * (v+1); simplifying the math gives the shorter expression seen in the code. Finally, we print v and loop.

# Brain-Flak, 60 bytes

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


Try it online!

# Explanation:

([]){  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


## Retina, 14 bytes

rv0|(1)+
\$#1¶


Try it online!

# C (gcc), 5752 51 bytes

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


Port of Arnauld's JavaScript answer, modifies the array in-place. Try it online here.

• Wouldn't it be more accurate to say this is K&R C? – Tim Seguine Jul 1 '18 at 13:14
• Possibly, but that would be true of a lot of answers. I'm no expert, but it's entirely possible it's not even valid K&R C. The thing is, we don't really care about the language standards on this site. If gcc allows you to mix K&R C with more modern stuff, then it's valid C for the purposes of golfing because gcc will compile it. See also: codegolf.stackexchange.com/questions/2203/tips-for-golfing-in-c – O.O.Balance Jul 1 '18 at 14:09
• I didn't realize until searching just now that C11 still supports the old identifier list function syntax, so nevermind. But your point holds regardless. – Tim Seguine Jul 2 '18 at 12:30
• Suggest f(a,l,c)int*a;{for(c=0;l--;)c=*a++*=c+1;} – user77406 Nov 1 '18 at 11:05

## Shakespeare, 365 bytes

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.


try it here

## less golfed version

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


# C++, 47 bytes

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


A lambda that modifies an array in place, given start and end pointers.

Try it online! (requires Javascript)

Generic version at 55 bytes (this works for any container with elements of arithmetic type):

[](auto a,auto b){for(auto c=*a-*a;a!=b;)c=*a++*=1+c;};
`