Avatar of paulfioravanti

paulfioravanti's solution

to All Your Base in the Ruby Track

Published at May 09 2019 · 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.

About Positional Notation

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

  def test_trinary_to_hexadecimal
    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

  def test_hexadecimal_to_trinary
    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

  def test_leading_zeros
    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
module BaseConverter
  INITIAL_SUM = 0
  private_constant :INITIAL_SUM
  MINIMUM_BASE = 2
  private_constant :MINIMUM_BASE

  module_function

  def convert(input_base, digits, output_base)
    raise ArgumentError if
      invalid_bases?([input_base, output_base]) ||
      invalid_digits?(digits, input_base)

    total = sum_input(digits, input_base)
    convert_to_output_base(total, output_base)
  end

  def invalid_bases?(bases)
    bases.any? { |base| base < MINIMUM_BASE }
  end
  private_class_method :invalid_bases?

  def invalid_digits?(digits, input_base)
    digits.any? { |digit| digit.negative? || digit >= input_base }
  end
  private_class_method :invalid_digits?

  def sum_input(digits, input_base)
    digits
      .reverse
      .each
      .with_index
      .with_object(input_base)
      .reduce(INITIAL_SUM, &method(:add_power))
  end
  private_class_method :sum_input

  def add_power(acc, ((digit, index), input_base))
    acc + digit * input_base**index
  end
  private_class_method :add_power

  def convert_to_output_base(total, output_base)
    [].tap do |conversion|
      loop do
        conversion.append(total % output_base)
        break if total < output_base

        total /= output_base
      end
    end.reverse
  end
  private_class_method :convert_to_output_base
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?