3
\$\begingroup\$

Through the bases

In this challenge, you will be taking a number through all the bases of the rainbow

Given an input, n, you will output n in every base from 9 to 2, and back up to 9. That's 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9.

Example:

>>> 16
17
20
22
24
31
100
121
10000
121
100
31
24
22
20
17

Rules:

  • You input can be a string or a number.
  • Your output may be separated by new lines, spaces, commas, or tabs.
  • You may use built in functions.
  • Shortest code wins!
\$\endgroup\$
  • \$\begingroup\$ Can the output be comma-separated? \$\endgroup\$ – Downgoat Jul 23 '15 at 23:21
  • \$\begingroup\$ @vihan1086 yes. \$\endgroup\$ – phase Jul 23 '15 at 23:21
  • \$\begingroup\$ I'm really late on this but can each line be surrounded by quotes? e.g. "17"\n"20" \$\endgroup\$ – Piccolo Aug 5 '15 at 5:05

13 Answers 13

8
\$\begingroup\$

CJam, 14 bytes

riF,f{7-z2+bN}

Try it online in the CJam interpreter.

How it works

ri             e# Read an integer I from STDIN.
  F,           e# Push [0 ... 14].
    f{       } e# For each B in that range, push I and B; then:
      7-z2+    e#   Compute abs(B - 7) + 2.
               e#   This maps [0 ... 14] to [9 ... 2 ... 9].
           b   e#   Perform base conversion.
            N  e#   Push a linefeed.
\$\endgroup\$
  • \$\begingroup\$ Just when I thought I had a chance at winning with Pyth. :P \$\endgroup\$ – Alex A. Jul 24 '15 at 0:03
  • 1
    \$\begingroup\$ @AlexA. I'm here to save your hope! \$\endgroup\$ – orlp Jul 24 '15 at 0:05
6
\$\begingroup\$

Pyth, 14 bytes

V+_Jr2TtJjkjQN

Needs more work, most code is spent generating 9..2..9.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 18 15 14 bytes

Saved 3 bytes thanks to Dennis and 1 thanks to Jakube!

V+r9 1r3TjkjQN

Explanation:

V                     For N in
 +r9 1r3T             the list [9,8,...3,2,3,...,8,9],
           jQN        convert the input to base N (returns a list),
         jk           join the list into a string, implicitly printed.

Try it online.

\$\endgroup\$
  • \$\begingroup\$ "" is just k, p already appends a linefeed and printing is implicit anyway, so FN+r9 1r3TjkjQN is 3 bytes shorter and looks a bit better. \$\endgroup\$ – Dennis Jul 23 '15 at 23:54
  • \$\begingroup\$ @Dennis You're the man. \$\endgroup\$ – Alex A. Jul 23 '15 at 23:57
3
\$\begingroup\$

Haskell, 59

0%_=0
k%b=div k b%b*10+mod k b
g n=map(n%)$[9,8..3]++[2..9]

First, recursively defines a binary operator % to do base conversion, representing the output as a decimal number rather than a string. The function g converts the input n to each base from 9 to 2 and back up.

There's probably a shorter approach, advice is welcome. Saved 2 chars on parens thanks to nimi.

\$\endgroup\$
  • 2
    \$\begingroup\$ Out of curiosity, why does [9,8...3] work but [9..3] doesn't? (the latter seems to return an empty list) \$\endgroup\$ – Sp3000 Jul 24 '15 at 0:57
  • 1
    \$\begingroup\$ @Sp3000 It defaults to a step size of +1, so [9..3] is empty because 9>3. Putting the second terms fixes the step size. It general, you can do range(a,b,s) as [a,a+s..b]. \$\endgroup\$ – xnor Jul 24 '15 at 1:27
2
\$\begingroup\$

Julia, 43 bytes

n->for i=[9:-1:2,3:9] println(base(i,n))end

This creates an unnamed function that accepts and integer and prints to STDOUT on separate lines. The base function takes two integers: a base (i) and a number (n) and returns the string representation of the number in the given base. Here we just loop through the required bases and print.

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

Bash + dc, 27

echo {{9..2},{3..9}}o$1p|dc
\$\endgroup\$
  • 1
    \$\begingroup\$ Slightly shorter: echo {{9..2},{3..9}}o$1p|dc \$\endgroup\$ – Dennis Jul 24 '15 at 4:45
  • \$\begingroup\$ @Dennis very good - I didn't know brace expansions could be nested like that! \$\endgroup\$ – Digital Trauma Jul 24 '15 at 4:49
2
\$\begingroup\$

JavaScript (ES6), 57 bytes

f=n=>[...`987654323456789`].map(v=>n.toString(v)).join` `

CoffeeScript, 57 bytes

Different approach:

f=(n)->[-7..7].map((v)->n.toString 2+Math.abs v).join ' '

JavaScript (ES5), 97 bytes

function f(n){return'987654323456789'.split('').map(function(v){return n.toString(v)}).join(' ')}

Demo

function f(n) {
  return '987654323456789'.split('').map(function(v) {
    return n.toString(v)
  }).join(' ')
}

document.body.innerHTML = f(16)

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

Python 2, 79

g=lambda b,k=input():k and g(b,k/b)*10+k%b
c=7;exec"print g(abs(c)+2);c-=1;"*15

The function g converts a number k to a base b. We can take k as the program input because Python initializes default arguments when the function is defined, not each time it's called. We get the desired sequence of bases by taking the range [-7, -6, ..., 6, 7], taking the absolute value, and adding 2.

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

Pip, 14 = 13 + 1 bytes

Uses the -n flag.

aTB2+AB(-7,8)

GitHub repository for Pip

Takes the number as a command-line argument.

        -7,8   Range containing integers from -7 through 7
     AB(    )  Absolute value of each
   2+          Add 2 to each
aTB            Convert command-line arg to each base
               Print resulting list, newline-separated (-n flag)

It's amazing how all the golfing languages are getting the same score here.

\$\endgroup\$
  • \$\begingroup\$ Those list formatting switches look really useful. \$\endgroup\$ – Dennis Aug 5 '15 at 5:25
  • \$\begingroup\$ @Dennis Yes, the main reason I thought of them was so I wouldn't have to wrap the whole program in (...)Jn (4 bytes vs. 1 with -n) when the program consisted of a map expression (parentheses needed since Map is lower precedence than Join). \$\endgroup\$ – DLosc Aug 5 '15 at 5:56
1
\$\begingroup\$

JavaScript (ES5), 69 66 65 bytes

function f(o){for(i=15;i--;)console.log(o.toString(i>7?i-5:9-i))}
\$\endgroup\$
1
\$\begingroup\$

JavaScript SpiderMonkey 31 (1.8), 47 bytes

n=>[for(i of`987654323456789`)n.toString(i)]+''

It doesn't get simpler than this.

\$\endgroup\$
  • \$\begingroup\$ Nice, but array comprehensions have been dropped from ES7. \$\endgroup\$ – Chiru Aug 5 '15 at 5:26
  • \$\begingroup\$ @Chiru Hm, I'll update it \$\endgroup\$ – Downgoat Aug 5 '15 at 5:30
0
\$\begingroup\$

APL, 52 bytes

I'll admit, this could use more golfing.

{n←⍵⋄⊃{⍺,' ',⍵}/{⊃,/⍕¨(~∧\0=e)/e←(10⍴⍵)⊤n}¨2+|8-⍨⍳15}

it is limited to at most 10 digits output

suggestions for improvement welcome

output for 16 looks like

17 20 22 24 31 100 121 10000 121 100 31 24 22 20 17
\$\endgroup\$
0
\$\begingroup\$

Ruby, 44 bytes

a=->n{(0..14).map{|j|p n.to_s((j-7).abs+2)}}

Call the lambda with a.call(number). This prints out with quotes around each line because p is shorter than puts. I'm assuming this is allowed.

\$\endgroup\$
  • \$\begingroup\$ but n=ARGV[0].to_i;(0..14).map{|j|puts n.to_s((j-7).abs+2)} is 56 bytes and is complete (could be 54 if used p instead of puts \$\endgroup\$ – rdnewman Aug 5 '15 at 16:39
  • \$\begingroup\$ Oh, gotcha, thanks, I'll fix that. Edit: Fixed. Also, defining a function is slightly shorter than getting the parameters passed to the code @rdnewman \$\endgroup\$ – Piccolo Aug 5 '15 at 16:53
  • \$\begingroup\$ But not sure that requiring a.call(number) in IRB would be considered a complete program by others. If it is, then I'd agree with you. Actually, might be able to just do (0..14)map{|j|p ARGV[0]((j-7).abs+2)} and remove the n altogether (didn't test). \$\endgroup\$ – rdnewman Aug 5 '15 at 18:31
  • \$\begingroup\$ @rdnewman I saw a lambda solution like this on an answer with 6 upvotes here so I assumed it was ok. In fact, they didn't even assign it to a variable and just put ->s.. \$\endgroup\$ – Piccolo Aug 5 '15 at 18:33
  • 1
    \$\begingroup\$ Ah, ok. Here's the corrected version of the last one I tried: (0..14).map{|j|p ARGV[0].to_i.to_s((j-7).abs+2)}; will run, but 48 bytes to your 44, so if they'll accept it, yours is better. \$\endgroup\$ – rdnewman Aug 5 '15 at 18:36

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.