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] []
[]
if the input is[0]
\$\endgroup\$