-5
\$\begingroup\$

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  
\$\endgroup\$
8
  • 6
    \$\begingroup\$ 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. \$\endgroup\$ – dmckee --- ex-moderator kitten Jun 6 '11 at 17:40
  • 11
    \$\begingroup\$ What 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) ? \$\endgroup\$ – Paul R Jun 6 '11 at 17:53
  • \$\begingroup\$ 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 :) \$\endgroup\$ – skylamer Jun 7 '11 at 4:51
  • \$\begingroup\$ What about those leading zeroes actually? \$\endgroup\$ – pimvdb Jun 7 '11 at 20:15
  • 7
    \$\begingroup\$ The problem remains inadequately specified. \$\endgroup\$ – dmckee --- ex-moderator kitten Jun 8 '11 at 0:04
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ this is working , but its just conversing the number from bin to dec... not reversing it :) \$\endgroup\$ – skylamer Jun 7 '11 at 5:55
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Ventero Jun 7 '11 at 16:37
  • \$\begingroup\$ So for now, pimvdb@js and ventero@ruby is the closer to the answer z \$\endgroup\$ – skylamer Jun 8 '11 at 16:24
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ may be , cuz when the input is dec... goes reversed in same and then out like a binary :) so 255 552 and 1000101000 \$\endgroup\$ – skylamer Jun 7 '11 at 4:55
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – mellamokb Jun 7 '11 at 18:17
  • \$\begingroup\$ @mellamokb: Oh I see, thanks. It now passes the testcases. \$\endgroup\$ – pimvdb Jun 7 '11 at 20:25
  • \$\begingroup\$ So for now, pimvdb@js and ventero@ruby is the closer to the answer z \$\endgroup\$ – skylamer Jun 8 '11 at 16:24
  • \$\begingroup\$ @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? \$\endgroup\$ – pimvdb Jun 8 '11 at 16:59
1
\$\begingroup\$

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"

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

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::]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ all(i<"2"for...) \$\endgroup\$ – gnibbler Jun 8 '11 at 12:00
  • \$\begingroup\$ @gnibbler, doh, so obvious... \$\endgroup\$ – st0le Jun 8 '11 at 12:32
0
\$\begingroup\$

bash 67

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

J, 60 characters

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

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