six
\$\begingroup\$

The Horrible Histories song "Learn Your Hieroglyphics" mentions a fanmade number system. Numbers are written as a sum of 10's ("hoops") and 1's ("eyes"), for example 99 is

 hoop hoop hoop hoop hoop hoop hoop hoop hoop eye eye eye eye eye eye eye eye eye

. Then, I thought, why not turn this into some sort of binary code?

Your programs should take a nonnegative integer as input.

Then, it should create a string of alternating 1's and 0's, with each group of the same digit corresponding to a digit in the number, in bijective decimal. This is so every number has a unique representation.

Examples:

 0 -> empty string 1 -> 1 2 -> 11 9 -> 111111111 10 -> 1111111111 11 -> 10 19 -> 1000000000 20 -> 10000000000 99 -> 111111111000000000 100 -> 1111111110000000000 110 -> 11111111110000000000 111 -> 101 123 -> 100111 4567 -> 1111000001111110000000

Then, the strings are interpreted as a binary number, then that number is outputted.

Examples:

 0 -> 0 1 -> 1 2 -> 3 9 -> 511 10 -> 1023 11 -> 2 19 -> 512 20 -> 1024 99 -> 261632 100 -> 523264 110 -> 1047552 111 -> 5 123 -> 39 4567 -> 3940224

This is , so fewest bytes wins!

\$\endgroup\$
two
  • seven
    \$\begingroup\$ In its current form, this challenge looks like a chameleon challenge to me, as the trickiest part may actually be to perform the bijective decimal conversion for languages that don't have a built-in. You should probably emphasize and explain bijective decimal . \$\endgroup\$
    –  Arnauld
    Commented May 20 at 20:57
  • \$\begingroup\$ Related \$\endgroup\$
    –  Arnauld
    Commented May 20 at 20:58

4 Answers four

Reset to default
three
\$\begingroup\$

Charcoal , 31 bytes

 NθWθ«⊞υ∨﹪θχχ≔÷⊖θχθ»I⍘⭆⮌υק10κι²

Try it online! Link is to verbose version of code. Explanation:

 NθWθ«⊞υ∨﹪θχχ≔÷⊖θχθ»

Input the number and convert it to little-endian bijective base ten .

 I⍘⭆⮌υק10κι²

Create a string alternating between one s and zero s and convert that from base two to decimal.

\$\endgroup\$
two
  • \$\begingroup\$ This unfortunately outputs nothing instead of zero for edge case \$n=0\$. \$\endgroup\$ Commented May 21 at 8:35
  • one
    \$\begingroup\$ @KevinCruijssen Ugh, that's a bug in Charcoal; it's crashing in BaseString looking for a sign. Here's a workaround for until I get around to patching it. \$\endgroup\$
    –  Neil
    Commented May 21 at 9:28
three
\$\begingroup\$

JavaScript (Node.js) , 41 bytes

 f=x=>x? (f(--x/10|0)+(e^=1)<<1+x%10)-e:e=0

Try it online!

\$\endgroup\$
one
\$\begingroup\$

Jelly , 7 bytes

 ḃ⁵Jx$ḂḄ

A monadic Link that accepts a non-negative integer and yields a non-negative integer.

Try it online! Or see the test-suite .

How?

 ḃ⁵Jx$ḂḄ - Link: non-negative integer, N ⁵      - ten ḃ       - convert {N} to bijective base {ten} $   - last two links as a monad - f(B=that): J     -   indices {B} -> [1,2,...,#digits] x    -    {B} times {that}    -> [1,1,...,2,2,...,3,3,...,...] Ḃ  - least significant bit -> [1,1,...,0,0,...,1,1,...,...] Ḅ - convert {that} from binary
\$\endgroup\$
one
\$\begingroup\$

05AB1E , twenty-one eighteen bytes

 [D_#<T‰`ˆ}TÞ¯R>×JC

Port of @Neil 's Charcoal answer , so make sure to upvote that answer as well!
-3 bytes thanks to @Neil

For a language that's literally called 'Base' (when interpret as hexadecimal and converted to base-64), its lack of a bijective-base builtin is a bit disappointing for this challenge.. So things are done manually using the same approach as the Charcoal answer.

Try it online or verify all test cases .

Explanation:

 [          # Start an infinite loop: D         #  Duplicate the current integer #  (which will use the implicit input-integer in the first iteration) _        #   Pop the copy, and check whether it's 0 #       #   Pop and if it's 0: stop the infinite loop <      #   Decrease the integer by 1 T‰    #  Divmod by 10: [n//10,n%10] `   #   Pop and push both values separated to the stack ˆ  #  Pop the top n%10 and add it to the global array }T         # After the infinite loop: push 10 Þ        # Convert it to an infinitely cycling digit-list: [1,0,1,0,1,0,...] ¯       # Push the global array R      # Reverse it >     # Increase each inner digit by 1 ×    # Repeat the [1,0,1,0,1,0,...] those amount of times J   # Join it together C  # Convert it from a binary-string to a base-10 integer # (which is output implicitly as result)
\$\endgroup\$
two
  • one
    \$\begingroup\$ 18 bytes . (I could also have used this approach in my Charcoal answer; it has the same byte count there though because it wastes bytes to decrement and modulo the value in separate statements.) \$\endgroup\$
    –  Neil
    Commented May 21 at 9:34
  • one
    \$\begingroup\$ By the way, I appreciated the Base trivia; I don't remember seeing that before. \$\endgroup\$
    –  Neil
    Commented May 21 at 10:33

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 .