2
\$\begingroup\$

The problem

Given a binary number from 0 to 111111 convert it to trinary. (Base 3). Then print the result.

Rules

No using a list of conversions.

Don't cheat

You must use a string as the number

No compressing the input string As I will be testing the code with several different numbers I can't use compressed strings

Notes

By trinary I mean counting in base 3. So 0,1,2,10,11,12,20... ect

Leading zeros will be present 001101

Most important number is on the left.

Example

String input = "110111"
String a = code(input);
print(a);

To win

This is code golf!

Do it correctly and in the smallest size.

\$\endgroup\$
  • 1
    \$\begingroup\$ You must use a string as the number I'd recommend allowing other formats such as integer etc. \$\endgroup\$ – Erik the Outgolfer Dec 18 '16 at 19:39
  • 1
    \$\begingroup\$ Base 3 is more commonly referred to as ternary. \$\endgroup\$ – Digital Trauma Dec 18 '16 at 19:47
  • \$\begingroup\$ I'm not sure what No using a list of conversions. means. Are you disallowing base conversion built-ins? \$\endgroup\$ – Dennis Dec 18 '16 at 19:48
  • \$\begingroup\$ It's even less clear what "no compression" is supposed to refer to. \$\endgroup\$ – DepressedDaniel Dec 18 '16 at 19:49
  • 1
    \$\begingroup\$ We have a sandbox where you can have feedback and help from the community before posting a challenge. Please use it. \$\endgroup\$ – Dada Dec 18 '16 at 20:06
1
\$\begingroup\$

Bash 25 bytes

Golfed

bc<<<"ibase=2;obase=3;$1"
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 26 bytes

s=>(+`0b${s}`).toString(3)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 97 87 bytes

f=->a{x=0
a.chars.map{|i|x+=x+i.ord%2}
o=x>0?"":?0
(o=(48+x%3).chr+o
x/=3)while x>0
o}

Old version:

def f a
x=0
a.chars.map{|i|x=x+x+i.ord%2}
o=x>0?"":"0"
while x>0
o=(48+x%3).chr+o
x/=3
end
o
end
\$\endgroup\$
  • \$\begingroup\$ Unless you were purposefully avoiding builtins, I think you're overthinking this. 21 bytes: ->s{s.to_i(2).to_s 3}. Also you might find this useful: codegolf.stackexchange.com/q/363/11261 \$\endgroup\$ – Jordan Dec 18 '16 at 20:11
  • \$\begingroup\$ @Jordan Indeed I was avoiding built-in base conversion functions. \$\endgroup\$ – DepressedDaniel Dec 18 '16 at 21:08
  • \$\begingroup\$ @Jordan Thanks for the tip list I have applied some of them and trimmed off 10 bytes. \$\endgroup\$ – DepressedDaniel Dec 19 '16 at 7:00

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