12
Background
Most people on here should be familiar with several base systems: decimal, binary, hexadecimal, octal. E.g. in the hexadecimal system, the number 1234516 would represent
1*16^4 + 2*16^3 + 3*16^2 + 4*16^1 + 5*16^0
Note that we're usually not expecting the base (here, 16
) to change from digit to digit.
A generalisation of these usual positional systems allows you to use a different numerical base for each digit. E.g. if we were alternating between decimal and binary system (starting with base 10 in the least significant digit), the number 190315[2,10] would represent
1*10*2*10*2*10 + 9*2*10*2*10 + 0*10*2*10 + 3*2*10 + 1*10 + 5 = 7675
We denote this base as [2,10]
. The right-most base corresponds to the least significant digit. Then you go through the bases (to the left) as you go through the digits (to the left), wrapping around if there are more digits than bases.
For further reading, see Wikipedia.
The Challenge
Write a program or function which, given a list of digits D
an input base I
and an output base O
, converts the integer represented by D
from base I
to base O
. You may take input via STDIN, ARGV or function argument and either return the result or print it to STDOUT.
You may assume:
- that the numbers in
I
andO
are all greater than1
. - the
I
andO
are non-empty. - that the input number is valid in the given base (i.e., no digit larger than its base).
D
could be empty (representing 0
) or could have leading zeroes. Your output should not contain leading zeroes. In particular, a result representing 0
should be returned as an empty list.
You must not use any built-in or 3rd-party base conversion functions.
This is code golf, the shortest answer (in bytes) wins.
Examples
D I O Result
[1,0,0] [10] [2] [1,1,0,0,1,0,0]
[1,0,0] [2] [10] [4]
[1,9,0,3,1,5] [2,10] [10] [7,6,7,5]
[1,9,0,3,1,5] [2,10] [4,3,2] [2,0,1,1,0,1,3,0,1]
[52,0,0,0,0] [100,7,24,60,60] [10] [3,1,4,4,9,6,0,0]
[0,2,10] [2,4,8,16] [42] [1,0]
[] [123,456] [13] []
[0,0] [123,456] [13] []
May I require an infinite list as a base description, or I have to infinitify it myself? – John Dvorak – 10 years ago
@JanDvorak You mean if you can expect the base lists to already have a sufficient number of repetitions to cover all digits? No, you'll have to do the wrapping around or repeating yourself. – Martin Ender – 10 years ago
I assume getting an empty list as a base is UB, but may we assume that list of digits is non-empty? Also, what's the policy on trailing zeroes? – John Dvorak – 10 years ago
Namely, I don't mind an empty list on input, but I'd like to produce
[]
if the input is[0]
– John Dvorak – 10 years agoMay I request and produce a list of digits in the reverse order (LSD first)? – John Dvorak – 10 years ago
I've added some clarification, assuming you meant "leading digits". – Martin Ender – 10 years ago
@JanDvorak no. MSD first. – Martin Ender – 10 years ago
Hmmmm, so
⊥⊤
in APL is banned :-( – Howard – 10 years ago@Howard yes, algorithmshark was so kind to point out in the sandbox that those exist ;) – Martin Ender – 10 years ago
Does it matter in which order we read the arrays, i.e., does it have to be
D I O
or wouldO I D
be OK as well? – Dennis – 10 years ago@Dennis That's fine, as long you don't change the order within the arrays. – Martin Ender – 10 years ago
Just to clarify: Output
[0]
is forbidden since it has leading zeros? – Dennis – 10 years ago@Dennis yes. I'll clarify that in the post. – Martin Ender – 10 years ago