Through the bases

3

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!

phase

Posted 2015-07-23T23:12:00.777

Reputation: 2 540

Can the output be comma-separated? – Downgoat – 2015-07-23T23:21:08.310

@vihan1086 yes. – phase – 2015-07-23T23:21:29.500

I'm really late on this but can each line be surrounded by quotes? e.g. "17"\n"20" – Piccolo – 2015-08-05T05:05:15.503

Answers

8

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.

Dennis

Posted 2015-07-23T23:12:00.777

Reputation: 196 637

Just when I thought I had a chance at winning with Pyth. :P – Alex A. – 2015-07-24T00:03:44.190

1@AlexA. I'm here to save your hope! – orlp – 2015-07-24T00:05:57.740

6

Pyth, 14 bytes

V+_Jr2TtJjkjQN

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

orlp

Posted 2015-07-23T23:12:00.777

Reputation: 37 067

3

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.

Alex A.

Posted 2015-07-23T23:12:00.777

Reputation: 23 761

"" 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. – Dennis – 2015-07-23T23:54:39.890

@Dennis You're the man. – Alex A. – 2015-07-23T23:57:45.780

3

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.

xnor

Posted 2015-07-23T23:12:00.777

Reputation: 115 687

2Out of curiosity, why does [9,8...3] work but [9..3] doesn't? (the latter seems to return an empty list) – Sp3000 – 2015-07-24T00:57:21.937

1@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]. – xnor – 2015-07-24T01:27:55.543

2

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.

Alex A.

Posted 2015-07-23T23:12:00.777

Reputation: 23 761

2

Bash + dc, 27

echo {{9..2},{3..9}}o$1p|dc

Digital Trauma

Posted 2015-07-23T23:12:00.777

Reputation: 64 644

1Slightly shorter: echo {{9..2},{3..9}}o$1p|dc – Dennis – 2015-07-24T04:45:26.550

@Dennis very good - I didn't know brace expansions could be nested like that! – Digital Trauma – 2015-07-24T04:49:06.787

2

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)

rink.attendant.6

Posted 2015-07-23T23:12:00.777

Reputation: 2 776

1

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.

xnor

Posted 2015-07-23T23:12:00.777

Reputation: 115 687

1

JavaScript SpiderMonkey 31 (1.8), 47 bytes

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

It doesn't get simpler than this.

Downgoat

Posted 2015-07-23T23:12:00.777

Reputation: 27 116

Nice, but array comprehensions have been dropped from ES7.

– Chiru – 2015-08-05T05:26:44.290

@Chiru Hm, I'll update it – Downgoat – 2015-08-05T05:30:39.187

1

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.

DLosc

Posted 2015-07-23T23:12:00.777

Reputation: 21 213

Those list formatting switches look really useful. – Dennis – 2015-08-05T05:25:33.150

@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). – DLosc – 2015-08-05T05:56:10.253

1

JavaScript (ES5), 69 66 65 bytes

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

Lyrkan

Posted 2015-07-23T23:12:00.777

Reputation: 111

0

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.

Piccolo

Posted 2015-07-23T23:12:00.777

Reputation: 261

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 – rdnewman – 2015-08-05T16:39:17.003

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 – Piccolo – 2015-08-05T16:53:21.863

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). – rdnewman – 2015-08-05T18:31:34.570

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

– Piccolo – 2015-08-05T18:33:18.570

1Ah, 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. – rdnewman – 2015-08-05T18:36:17.000

0

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

protist

Posted 2015-07-23T23:12:00.777

Reputation: 570