Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

Input:

0 00 000 0000000000

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!

share|improve this question
5  
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? – isaacg yesterday
    
@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. – Mason Watmough yesterday
    
Can it be a function instead of a full program? – My Ham DJ yesterday
2  
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. – DenkerAffe yesterday
3  
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. – My Ham DJ yesterday

JavaScript (ES6), 76 bytes

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

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))
share|improve this answer
    
This is invalid, because it drops 0 in unary (2 consecutive spaces) – isaacg yesterday
    
@isaacg Should be solved now. – orlp yesterday
    
@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). – R. Kap yesterday
    
@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. – orlp yesterday
    
@orlp Well, running "111<2 spaces>11" in Python 3.5.1 returns the output (4 0 3) (4 0 3) (100 0 11) – R. Kap yesterday

Pyth, 12 bytes

mjLld[2T16)c

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.

share|improve this answer
    
This is invalid, because it drops 0 in unary (2 consecutive spaces) – isaacg yesterday
    
@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. – My Ham DJ yesterday

Pyke, 14 bytes

dcmlD
mb2Rmb16

Try it here

share|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 '.

share|improve this answer
    
This is invalid, because it drops 0 in unary (2 consecutive spaces) – isaacg yesterday
    
@isaacg I did not realize that 0 counts as an extra space. That's fixed now. – R. Kap yesterday
    
I was talking about the other way around - 1<space><space>11 should become 1 0 2 in decimal – isaacg yesterday
    
@isaacg How is that? Are you saying each extra space between characters counts as a zero? – R. Kap yesterday
    
@isaacg Okay, I get it now. Edit now in progress... – R. Kap yesterday

Python 2: 85 bytes

i=input().split()
for b in "dxb":print" ".join(("{0:%s}"%b).format(len(x))for x in i)

Note that input must be typed in with " surrounding.

I'm really disappointed that the % operator doesn't support binary, because it would allow this shorter version:

i=input().split()
for b in "dxb":print"print' '.join('%%%s}'%%len(x)for x in i)"%b
share|improve this answer
    
This is invalid, because it drops 0 in unary (2 consecutive spaces) – isaacg yesterday

Ruby, 89 bytes

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

->s{s=s.scan(/[^ ]* ?/).map{|n|n.strip.size}
s.pop
[2,10,16].map{|b|s.map{|n|n.to_s(b)}}}

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

->s{[2,10,16].map{|b|s.split.map{|n|n.size.to_s(b)}}}
share|improve this answer
    
Why not go with your second code changing .split.split(/ /)? Would handle 0 and would still be shorter than your first code. – manatwork 3 hours ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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