36
5
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]
- String:
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]
- String:
Rules
- (only apply for strings) Input wont contain spaces between
1
and0
- 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
1Oof, better than mine. I would've never thought of this. – Magic Octopus Urn – 2018-06-29T13:25:24.627
3
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
– dr jimbob – 2018-06-30T14:10:43.58303 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.)6
@drjimbob Yes, good question. The code can actually be converted to a binary file using the 05AB1E code page. For example,
– Adnan – 2018-06-30T19:00:18.323γ€ƶ˜
would be represented as04 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.