15
\$\begingroup\$

The title says it all; Given a number in the binary (base-2) number system, output the same number expressed in unary (base-1).

You should take the binary number as a string (optionally with a separator), list, or equivalent structure of digits, using any number or digit you like for the digits. The unary output can use any aforementioned format, and it should use a single-digit throughout the number but does not have to be consistent for different input values. You may choose to assume that the binary number will have no more than 8 bits, and/or that it will always be left-padded with zero bits to a certain length.

One method you may use to achieve this task is documented at this esolangs.org article:

  1. Replace all 1s with 0* (Using * as the unary digit; you may use any character.)
  2. Replace all *0s with 0**.
  3. Remove all 0s.

Here are some examples, using 0 and 1 for the binary digits and * for the unary digit:

  • 1001*********
  • 1010**********
  • 00111111***************************************************************
  • 00000 → (empty output)

This is , so the shortest answer in each language wins.

\$\endgroup\$
13
  • 3
    \$\begingroup\$ @KevinCruijssen I’m going to say no, since it doesn’t really feel like base-1 at that point. \$\endgroup\$ Commented Aug 11, 2023 at 20:05
  • 1
    \$\begingroup\$ any reasonable input & output format I'm guessing taking the input as an integer is not acceptable, but it would be good to state it explicitly. \$\endgroup\$ Commented Aug 11, 2023 at 22:26
  • 1
    \$\begingroup\$ In other words, a binary integer in a register is a normal way for a function to accept binary numbers as input, and the most obvious / simple / efficient. If you mean a base 2 string representation of a number, say that. I agree with @LevelRiverSt on this. Your examples are string-like, but that could be assumed to be catering to languages that don't natively have binary integer types. (e.g. Javascript, where numbers are floating-point, not binary integer.) Also, a string of 1-bit digits can be packed into a register (or C unsigned int); that's a good way to store binary! \$\endgroup\$ Commented Aug 12, 2023 at 19:29
  • 1
    \$\begingroup\$ Could I suggest a test case of 0000 to result in an empty output? \$\endgroup\$ Commented Aug 14, 2023 at 8:02
  • 1
    \$\begingroup\$ @PeterCordes Thanks for explaining, now I understand I didn’t quite account for that interpretation. Clarifying now. \$\endgroup\$ Commented Aug 14, 2023 at 13:13

44 Answers 44

1
2
1
\$\begingroup\$

sed 4.2.2, 28

: s/1/0u/ s/u0/0uu/ t s/0//g 

Implementation algorithm as per the question.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

dc, 17

[q]sz2iA?^9/d0=zn 

Try it online!

Explanation

[ ] # define a macro to... q # quit (i.e. print the empty string) sz # save the macro to register z 2i # set input radix to 2 (binary) A # pop 10 ? # read input ^ # exponentiate 10 to the power of the input 9/ # divide by 9. This gives a number consisting of n 1s, or 0 d # duplicate top of stack 0=z # if equal to 0, call macro stored in z register to print the empty string n # otherwise print the unary number 
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 24 bytes

~x="*"^parse(Int,"0b"*x) 

Try it online!

Generally, any base other than 10 needs to be specified: parse(Int,x,base=2). Two bytes are saved by appending a 0b integer prefix instead.

\$\endgroup\$
1
\$\begingroup\$

Brain-Flak, 42 bytes

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

Try it online!

The code works by accumulating the values of partial sums on the second stack, then it expands the second stack from the single value n, to n copies of 1.

([]) Push the stack height { While the stack is not empty {} pop the stack height ( {} pop the top of the stack <> Jump to the second stack ({}){} Add the top element of the second stack twice, popping it ) Push the sum <> ([]) Push new height } <> On the second stack: { While n>0 ({} Pop n [(()) Push 1 ] add -1 ) Push n-1 }{} Pop the zero 
\$\endgroup\$
1
\$\begingroup\$

Thue, 46 bytes

a::=::: 1::=0* *0::=0** *>::=!> !::=~* ::= a> 

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Swift 5.9, 48 bytes

let f={{String.init}()("*",Int($0+"",radix:2)!)} 
\$\endgroup\$
1
\$\begingroup\$

YASEPL, 65 61 49 47 45 41 39 bytes

=i=1'=l®1+`1--!a¥i,1=r$2^l*a;a,r~!+!l+[ 

1 is the unary digit, prompts you for input

YASEPL, 37 35 bytes

this one exits with an error but works the same.

=i=1'=l®1-i-!a¥i,1=r$2^l*a;a,r~!+?6 
\$\endgroup\$
1
\$\begingroup\$

BrainChild, 73 bytes

include*;int j=0;gets()(function(int c)j=j*2+(c-48))while(j--)putchar(65) 

Readable

include io.bc; int j=0; gets()(function(int c){ j=j*2; j=j+(c-48) }) while(j--) putchar('A') 

Try It Online!

\$\endgroup\$
1
\$\begingroup\$

AWK, 48 bytes

{gsub(1,"0*");while(sub(/*0/,"0**"));gsub(0,X)}1 

Try it online!

Another version:

{for(;i++<=NF;)for(k=1;$i&&k++<=2^(NF-i);)printf "*"} 
\$\endgroup\$
1
\$\begingroup\$

Tcl, 36 bytes

proc U b {string repe * [expr 0b$b]} 

Try it online!

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

Lua, 33 bytes

print(('*'):rep(tonumber(...,2))) 

Try it online!

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

Scala 3, 25 bytes

"*"*Integer.parseInt(_,2) 

Attempt This Online!

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

brainfuck, 78 bytes

+>>,[>+++++++[-<------->]<+<<[->+>>++<<<]<++++++[->++++++<]>>>[-<[-<.>]>]>>>,] 

Attempt This Online!

Someone already did it in brainfuck and got a better score here, but I thought I'd try it out myself.

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

Japt, 2 bytes

Takes input as a binary string, outputs a string of spaces.

Íç 

Try it (Footer replaces spaces with 1s)

Íç :Implicit input of binary string Í :Convert to decimal ç :Repeat a space that many times 
\$\endgroup\$
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.