ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# David-Roark's solution

## to All Your Base in the Ruby Track

Published at Apr 30 2021 · 0 comments
Instructions
Test suite
Solution

Convert a number, represented as a sequence of digits in one base, to any other base.

Implement general base conversion. Given a number in base a, represented as a sequence of digits, convert it to base b.

## Note

• Try to implement the conversion yourself. Do not use something else to perform the conversion for you.

In positional notation, a number in base b can be understood as a linear combination of powers of b.

The number 42, in base 10, means:

(4 * 10^1) + (2 * 10^0)

The number 101010, in base 2, means:

(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)

The number 1120, in base 3, means:

(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)

I think you got the idea!

Yes. Those three numbers above are exactly the same. Congratulations!

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby all_your_base_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride all_your_base_test.rb
``````

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### all_your_base_test.rb

``````require 'minitest/autorun'
require_relative 'all_your_base'

# Common test data version: 2.3.0 c21ffd7
class AllYourBaseTest < Minitest::Test
def test_single_bit_one_to_decimal
# skip
digits = [1]
input_base = 2
output_base = 10
expected = [1]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 2, output base 10. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_binary_to_single_decimal
skip
digits = [1, 0, 1]
input_base = 2
output_base = 10
expected = [5]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 2, output base 10. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_single_decimal_to_binary
skip
digits = [5]
input_base = 10
output_base = 2
expected = [1, 0, 1]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 10, output base 2. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_binary_to_multiple_decimal
skip
digits = [1, 0, 1, 0, 1, 0]
input_base = 2
output_base = 10
expected = [4, 2]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 2, output base 10. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_decimal_to_binary
skip
digits = [4, 2]
input_base = 10
output_base = 2
expected = [1, 0, 1, 0, 1, 0]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 10, output base 2. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

skip
digits = [1, 1, 2, 0]
input_base = 3
output_base = 16
expected = [2, 10]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 3, output base 16. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

skip
digits = [2, 10]
input_base = 16
output_base = 3
expected = [1, 1, 2, 0]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 16, output base 3. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_15_bit_integer
skip
digits = [3, 46, 60]
input_base = 97
output_base = 73
expected = [6, 10, 45]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 97, output base 73. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_empty_list
skip
digits = []
input_base = 2
output_base = 10
expected = [0]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 2, output base 10. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_single_zero
skip
digits = [0]
input_base = 10
output_base = 2
expected = [0]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 10, output base 2. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_multiple_zeros
skip
digits = [0, 0, 0]
input_base = 10
output_base = 2
expected = [0]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 10, output base 2. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

skip
digits = [0, 6, 0]
input_base = 7
output_base = 10
expected = [4, 2]

converted = BaseConverter.convert(input_base, digits, output_base)

hint = "Input base: 7, output base 10. " +
"Expected #{expected} but got #{converted}."

assert_equal expected, converted, hint
end

def test_input_base_is_one
skip
digits = [0]
input_base = 1
output_base = 10
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_input_base_is_zero
skip
digits = []
input_base = 0
output_base = 10
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_input_base_is_negative
skip
digits = [1]
input_base = -2
output_base = 10
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_negative_digit
skip
digits = [1, -1, 1, 0, 1, 0]
input_base = 2
output_base = 10
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_invalid_positive_digit
skip
digits = [1, 2, 1, 0, 1, 0]
input_base = 2
output_base = 10
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_output_base_is_one
skip
digits = [1, 0, 1, 0, 1, 0]
input_base = 2
output_base = 1
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_output_base_is_zero
skip
digits = [7]
input_base = 10
output_base = 0
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_output_base_is_negative
skip
digits = [1]
input_base = 2
output_base = -7
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end

def test_both_bases_are_negative
skip
digits = [1]
input_base = -2
output_base = -7
assert_raises(ArgumentError) do
BaseConverter.convert(input_base, digits, output_base)
end
end
end``````
``````class BaseConverter
def self.convert(inp_base, digits, out_base)
valid?(inp_base, digits, out_base)

base_10_to(to_base_10(inp_base, digits), out_base)
end

def self.to_base_10(inp_base, digits)
digits.reverse.map.with_index { |n, i| n*inp_base**i } .inject(0, :+)
end

def self.base_10_to(n, out_base)
return [0] if n == 0
result = []
while n > 0
result << n % out_base
n = n / out_base
end

result.reverse
end

def self.valid?(inp_base, digits, out_base)
raise ArgumentError unless inp_base.is_a?(Integer) &&
out_base.is_a?(Integer) &&
digits.is_a?(Array) &&
inp_base > 1 &&
out_base > 1 &&
digits.all? { |n| n >=0 && n < inp_base }
end
end``````