Write a program that accepts input (through stdin) as a string of unary numbers delimited by spaces, and prints the equivalent string of numbers in binary, decimal, and hexadecimal. These unary symbols are allowed:

  • stroke, '/' or '\'
  • Decimal 1
  • ASCII 'I'
  • ASCII '-'

Your program does not have to support all symbols, but it must use at least one. Zero should be represented as simply two spaces (the lack of unary symbols)

e.g. if the input is

Test Cases:


1 11 111 1111111111

Example Output:

1 2 3 10

1 2 3 A

1 10 11 1010

Output must be to standard output. If no standard output is available, use whatever output logging stream is available.

It does not have to print the converted strings in any particular order; it must print the binary, hexadecimal, and decimal string equivalents, but it does not have to print in that specific order.

This is code-golf, so shortest program wins!

  • 5
    \$\begingroup\$ Hi Mason, welcome to PPCG. Your challenge looks good over all. A few questions: Is the number 0 in unary an allowable input? Do solutions need to accept all of the specified symbols as inputs, or the one of their choice? How flexible is the output format? Can output bases be in any order? Do they need to space + newline separated? \$\endgroup\$ – isaacg Apr 1 '16 at 5:57
  • \$\begingroup\$ @isaacg 0 in unary is simply the lack of any unary symbols; a null value. The representation for zero (for this program) should be an extra space. Symbols are of the programmer's choice. Output format is simply print the string of numbers in one base, then another, then the last. Only spaces are delimeters. \$\endgroup\$ – Mason Watmough Apr 1 '16 at 6:10
  • \$\begingroup\$ Can it be a function instead of a full program? \$\endgroup\$ – James Apr 1 '16 at 6:52
  • 2
    \$\begingroup\$ Please specify all rules in the challenge, comments are not guarenteed to stick around forever and are therefore not a suitable place to do that. Also you should add some testcases, especially for the edge cases. \$\endgroup\$ – Denker Apr 1 '16 at 8:28
  • 3
    \$\begingroup\$ Hey Mason, I'm voting to close this challenge as "Unclear what you're asking" until you add some more details, and some test cases. Right now, it's not really sure what we're supposed to do with extra spaces and what kind of inputs we will have to handle. If you make that clear, I will happily retract my close vote. \$\endgroup\$ – James Apr 1 '16 at 14:43

Python 3, 68 bytes

i=input().split(" ")
for f in[bin,str,hex]:print(*map(f,map(len,i)))

Outputs like such:

0b1 0b10 0b11
1 2 3
0x1 0x2 0x3

If the 0b and 0x are not allowed then that can be solved at the cost of 17 extra bytes by changing the second line to:

for f in[bin,str,hex]:print(*(f(len(n))[2*(f!=str):]for n in i))

Or using this version (minified version of My Ham DJ's answer):

i=input().split(" ")
for b in"dxb":print(*("{:{}}".format(len(x),b)for x in i))
| improve this answer | |
  • \$\begingroup\$ This is invalid, because it drops 0 in unary (2 consecutive spaces) \$\endgroup\$ – isaacg Apr 1 '16 at 7:48
  • \$\begingroup\$ @isaacg Should be solved now. \$\endgroup\$ – orlp Apr 1 '16 at 8:26
  • \$\begingroup\$ @orlp In 111<space><space>11, your code counts the last number ("11") as a literal 11 in decimal. It should instead be 2 (since its length is 2). I recommend using regex for this (if you can). \$\endgroup\$ – R. Kap Apr 1 '16 at 8:35
  • \$\begingroup\$ @R.Kap No it doesn't? I have no clue what you're talking about. Make sure you're running in Python 3, not Python 2. \$\endgroup\$ – orlp Apr 1 '16 at 8:38
  • \$\begingroup\$ @orlp Well, running "111<2 spaces>11" in Python 3.5.1 returns the output (4 0 3) (4 0 3) (100 0 11) \$\endgroup\$ – R. Kap Apr 1 '16 at 8:43

JavaScript (ES6), 76 bytes

s=>[2,10,16].map(b=>s.split` `.map(n=>n.length.toString(b)).join` `).join`
| improve this answer | |

Pyth, 12 bytes


Try it here!

Output is a list of lists with each sublist containing the binary, decimal and hex representation of the corrosponding input number as a list of digits.

| improve this answer | |
  • \$\begingroup\$ This is invalid, because it drops 0 in unary (2 consecutive spaces) \$\endgroup\$ – isaacg Apr 1 '16 at 7:49
  • \$\begingroup\$ @isaacg As far as the original post is concerned this is valid. Right now it's hard to tell without digging through the comments. I think we should instead focus on making the post better, and then I'll fix or delete my answer once the spec is clear. \$\endgroup\$ – James Apr 1 '16 at 7:55

Pyke, 14 bytes


Try it here

| improve this answer | |

Python 3.5, 61 bytes:

(+17 since zeroes count as an extra space)

lambda g:[(bin(len(f)),hex(len(f)),len(f))for f in g.split(" ")]

Pretty much speaks for itself. It is a lambda function, so you must provide input. Then, the function takes "g", which is your input, and outputs the correct values. If, for instance, the input is "111 11", output is as follows:

[('0b11', '0x3', 3), ('0b10', '0x2', 2)]

I honestly think that including the 0b and 0x is a better choice than removing them, since then this way, you can differentiate between those numbers that are hexadecimal, those which are binary, and those which are just plain denary (decimal).

And now, if the input is, for instance, "011", it counts the 0 as an extra space, and as a result outputs [('0b10', '0x2', 2)].

EDIT: AS per @isaacg's input, I have now fixed by program, and it should work as it should. The code now has the ability for extra spaces between ones to become zeroes. Now, if the input is, for instance, 11<space><space>1, the output is [('0b1', '0x1', 1), ('0b0', '0x0', '0'), ('0b10', '0x2', 2)] counting the extra space as a '0 '.

| improve this answer | |
  • \$\begingroup\$ This is invalid, because it drops 0 in unary (2 consecutive spaces) \$\endgroup\$ – isaacg Apr 1 '16 at 7:49
  • \$\begingroup\$ @isaacg I did not realize that 0 counts as an extra space. That's fixed now. \$\endgroup\$ – R. Kap Apr 1 '16 at 7:55
  • \$\begingroup\$ I was talking about the other way around - 1<space><space>11 should become 1 0 2 in decimal \$\endgroup\$ – isaacg Apr 1 '16 at 7:56
  • \$\begingroup\$ @isaacg How is that? Are you saying each extra space between characters counts as a zero? \$\endgroup\$ – R. Kap Apr 1 '16 at 7:58
  • \$\begingroup\$ @isaacg Okay, I get it now. Edit now in progress... \$\endgroup\$ – R. Kap Apr 1 '16 at 8:03

Java, 136 bytes

s->{String[]l=s.split(" ",-1);s="";for(int i=2;i<17;i+=i<3?8:6){for(String n:l)s+=Integer.toString(n.length(),i)+" ";s+="\n";}return s;}

Try it online!

This is an anonymous function (lambda) that takes a String and returns a String representing the output as described.

Full explanation will come later today.

| improve this answer | |
  • \$\begingroup\$ Full program with STDIN coming \$\endgroup\$ – CAD97 Apr 2 '16 at 17:12

Ruby, 89 58 bytes

Anonymous function; Returns a list of lists where each sublist contains binary, decimal, and hex representations, and accounts for extra spaces.

@manatwork is a Ruby wizard

->s{[2,10,16].map{|b|s.split(/ /).map{|n|n.size.to_s(b)}}}

Without the need to check for the spare spaces meaning 0, it drops to 53 bytes:

| improve this answer | |
  • 1
    \$\begingroup\$ Why not go with your second code changing .split.split(/ /)? Would handle 0 and would still be shorter than your first code. \$\endgroup\$ – manatwork Apr 2 '16 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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