Reversing base numbers

-5

Where input is binary, convert it to a decimal number (base 10), reverse the decimal number and output it. Vice versa for decimal (base 10) input: reverse it, convert it to binary and output the binary number.

Sample Input:

00000001
00000010
255
00011000

Sample Output

1
2
1000101000
42

other Sample Input

78
35
01011001
231

other Sample Output

01010111
00110101
98
10000100  

skylamer

Posted 2011-06-06T16:38:17.033

Reputation: 5

Question was closed 2014-06-19T12:56:49.917

6

Welcome to CodeGolf.SE! You question seem to be underspecified (by which I mean I am not parsing "decimal reversed equivalent" at all), and you have not stated what contest is being proposed. If you mean this to be a code golf it should be tagged as such. Please consider having your questions reviewed at the meta SandBox or in the puzzle lab chat where we would be more than happy to help you hammer out these kinds of details before your task goes live.

– dmckee --- ex-moderator kitten – 2011-06-06T17:40:55.880

11What happens if the input is a number which has an ambiguous base, e.g. 10000000 which could be interpreted as binary (32768 decimal) or decimal (1 million) ? – Paul R – 2011-06-06T17:53:58.870

yes, may be we have to add some thing to recognize when is binary and when decimal :) some thing like d or b after the number idk :) – skylamer – 2011-06-07T04:51:51.197

What about those leading zeroes actually? – pimvdb – 2011-06-07T20:15:01.973

In C and other languages a 0123 means an octal number and a 0x123 a hex number. Maybe leading zero (or two) can specify it. – Martin Ueding – 2011-06-07T20:53:36.357

@pimvdb: They are needed so you always have four-digit numbers. @queue: Obviously this is math, not C, so octal doesn't apply. – Joey – 2011-06-07T21:47:48.120

7The problem remains inadequately specified. – dmckee --- ex-moderator kitten – 2011-06-08T00:04:14.183

There needs to be a spec for what <s>makes</s> defines a binary number. While it makes the control logic ludicrously short, I agree with @skylamer that the "b" flag should be added to the end of binary cases. 00000010b = 2 for instance. – arrdem – 2011-06-08T00:28:37.170

Answers

4

Ruby, 68 characters

puts$<.map{|i|i[/[^01\s]/]?"%08b"%i.reverse: i.to_i(2).to_s.reverse}

Uses stdin/stdout, interprets lines that consist only of 1s, 0s and whitespace as binary numbers, everything else as decimal.

Ventero

Posted 2011-06-06T16:38:17.033

Reputation: 9 842

this is working , but its just conversing the number from bin to dec... not reversing it :) – skylamer – 2011-06-07T05:55:28.683

2@skylamer: Fixed, I wasn't sure what exactly you meant with "reversing" before. A more detailed problem description with more details for the input/ouput specification would help preventing such misunderstandings. – Ventero – 2011-06-07T16:37:18.613

So for now, pimvdb@js and ventero@ruby is the closer to the answer z – skylamer – 2011-06-08T16:24:32.037

2

JavaScript, 140 199 bytes

(function(x){s=x.split("\n"),r=b='';for(i in s)r+='\n'+(s[i].replace(/[01]*/g,b)?
(s[i].split(b).reverse().join(b)-0).toString(2):(parseInt(s[i],2)+"")
.split(b).reverse().join(b));return r.trim()})('1\n10\n255\n11000')

Excluding input. It requires, however, that there are no numbers present in the input which are base 10 and only have 1s / 0s in them.

pimvdb

Posted 2011-06-06T16:38:17.033

Reputation: 821

may be , cuz when the input is dec... goes reversed in same and then out like a binary :) so 255 552 and 1000101000 – skylamer – 2011-06-07T04:55:33.797

1@primvdb: Apparently this is what the OP means by the confusing phrase "decimal reversed equivalent". It looks like all decimal input is to be text reversed before converting, and all binary after converting should be text reversed as well. – mellamokb – 2011-06-07T18:17:39.310

@mellamokb: Oh I see, thanks. It now passes the testcases. – pimvdb – 2011-06-07T20:25:38.107

So for now, pimvdb@js and ventero@ruby is the closer to the answer z – skylamer – 2011-06-08T16:24:16.187

@skylamer: I don't have the leading zeroes yet, as I still don't see the way you'd like them to appear - should a binary number's length always be a multiple of 4? – pimvdb – 2011-06-08T16:59:33.597

2

perl - 90 89

map{print reverse(/^[01]+$/?unpack"N",pack"B*",sprintf"%032d",$_:sprintf"%b",$_).$/}@ARGV

Any number containing only 1's and 0's is considered binary.

Funny how so much more code is required for bin->dec vs. dec->bin...

swilliams

Posted 2011-06-06T16:38:17.033

Reputation: 476

1

Python - 117 characters

while 1:
 x=raw_input()
 if(not(False in map(lambda x:x in ("1","0"),s))):print int(x,2)
 else:print bin(int(x))[2:]

This golf relies heavily on the fact that python counts a single space as a full indentation. Note that this golf IS NOT ERROR-PROOF. While functional, it will throw an EOFError when there is no input. Protecting against this requires an extra 30 characters.

Changelog

v0 - 120 chars

v1 - 117 chars - changes "While True" to "While 1"

arrdem

Posted 2011-06-06T16:38:17.033

Reputation: 805

1

Quite frankly, i still don't get the testcase for 255, i'll mend my solution when the OP corrects/expands the description.

My Solution is based of rmckenzie

Python - 84 bytes

while 1:
 x=raw_input()
 print all(i<"2"for i in x)and int(x,2)or bin(int(x))[2::]

st0le

Posted 2011-06-06T16:38:17.033

Reputation: 2 002

1all(i<"2"for...) – gnibbler – 2011-06-08T12:00:47.267

@gnibbler, doh, so obvious... – st0le – 2011-06-08T12:32:37.467

0

bash 67

b(){ [[ $1 =~ ^[01]*$ ]]&& s="i"||s="o";echo $s"base=2;$1"|bc|rev;}

user unknown

Posted 2011-06-06T16:38:17.033

Reputation: 4 210

0

J, 60 characters

((1&":@#:@(10&#.@|.)`(|.@":@#.))@.(0=[:+/1<[))".;' ',.1!:1[1

Gareth

Posted 2011-06-06T16:38:17.033

Reputation: 11 678