32
\$\begingroup\$

This was inspired by Problem 13 - Non-Repeating Binary of HP CodeWars' recent competition.

Let's take a random decimal number, say

727429805944311

and look at its binary representation:

10100101011001011111110011001011101010110111110111

Now split that binary representation into subsequences where the digits 0 and 1 alternate.

1010 010101 10 0101 1 1 1 1 1 10 01 10 0101 1 1010101 101 1 1 1 101 1 1

And convert each subsequence back into decimal.

10 21 2 5 1 1 1 1 1 2 1 2 5 1 85 5 1 1 1 5 1 1

The Task

Take a single, positive integer as input and output the sequence of positive integers obtained by the above process.

Details

  • Input and output must be in decimal or unary.
  • Numbers in the output must be separated in a sensible, human-readable fashion, and they must be in decimal or unary. No restriction on white space. Valid output styles: [1,2,3], 1 2 3, 1\n2\n3 where \n are literal newlines, etc.

Test cases

 Input | Output
     0 | 0
     1 | 1
     2 | 2
     3 | 1 1
     4 | 2 0
     5 | 5
     6 | 1 2
     7 | 1 1 1
     8 | 2 0 0
     9 | 2 1
    10 | 10
    50 | 1 2 2
   100 | 1 2 2 0
  1000 | 1 1 1 1 10 0 0
 10000 | 2 1 1 2 0 2 0 0 0
 12914 | 1 2 2 1 1 2 2
371017 | 5 42 10 2 1

Additional note: all numbers in the output should be of the form (2^k-1)/3 or 2*(2^k-1)/3. That is, 0 1 2 5 10 21, 42, 85, 170, ..., which is A000975 in the OEIS.

\$\endgroup\$
3
  • \$\begingroup\$ @DigitalTrauma: Hmmm......no, I don't think that's within the spirit of the challenge. \$\endgroup\$ Commented Mar 12, 2016 at 4:32
  • \$\begingroup\$ Ok. |tac will remain in my answer then :) \$\endgroup\$ Commented Mar 13, 2016 at 22:43
  • \$\begingroup\$ is it reversible ? I think so ... \$\endgroup\$
    – Setop
    Commented Jan 25, 2023 at 10:37

32 Answers 32

1
2
0
\$\begingroup\$

Retina, 60

+`(1+)\1
$1a
a1
1
(?<=(.))(?=\1)
¶
+`1(a*)\b
a$.1$*1;
a

;
1

Try it online! Or try a slightly modified version for all test cases (with decimal I/O).

Unfortunately, zero length matches seem to have two "sides", causing duplication when used with the regex from the third stage. Only costs one byte though.

Takes input as unary, outputs as unary. Not really sure about using different in/out unary values, but that would save 4 bytes.

\$\endgroup\$
0
\$\begingroup\$

Factor + math.unicode, 45 bytes

[ >bin [ ≠ ] monotonic-split [ bin> ] map ]

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.