Avatar of remcopeereboom

remcopeereboom's solution

to Hexadecimal in the Ruby Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

Convert a hexadecimal number, represented as a string (e.g. "10af8c"), to its decimal equivalent using first principles (i.e. no, you may not use built-in or external libraries to accomplish the conversion).

On the web we use hexadecimal to represent colors, e.g. green: 008000, teal: 008080, navy: 000080).

The program should handle invalid hexadecimal strings.


For installation and learning resources, refer to the exercism help 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 hexadecimal_test.rb

To include color from the command line:

ruby -r minitest/pride hexadecimal_test.rb

Source

All of Computer Science http://www.wolframalpha.com/examples/NumberBases.html

Submitting Incomplete Solutions

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

hexadecimal_test.rb

require 'minitest/autorun'
require_relative 'hexadecimal'

class HexadecimalTest < Minitest::Test
  def test_hex_1_is_decimal_1
    assert_equal 1, Hexadecimal.new('1').to_decimal
  end

  def test_hex_c_is_decimal_12
    skip
    assert_equal 12, Hexadecimal.new('c').to_decimal
  end

  def test_hex_10_is_decimal_16
    skip
    assert_equal 16, Hexadecimal.new('10').to_decimal
  end

  def test_hex_af_is_decimal_175
    skip
    assert_equal 175, Hexadecimal.new('af').to_decimal
  end

  def test_hex_100_is_decimal_256
    skip
    assert_equal 256, Hexadecimal.new('100').to_decimal
  end

  def test_hex_19ace_is_decimal_105166
    skip
    assert_equal 105_166, Hexadecimal.new('19ace').to_decimal
  end

  def test_invalid_hex_is_decimal_0
    skip
    assert_equal 0, Hexadecimal.new('carrot').to_decimal
  end

  def test_black
    skip
    assert_equal 0, Hexadecimal.new('000000').to_decimal
  end

  def test_white
    skip
    assert_equal 16_777_215, Hexadecimal.new('ffffff').to_decimal
  end

  def test_yellow
    skip
    assert_equal 16_776_960, Hexadecimal.new('ffff00').to_decimal
  end
end
class Hexadecimal
  def initialize(hex_number)
    @sign, @hex_number = parse(hex_number)
  end

  def to_decimal
    decimals = digits.reverse.map.with_index { |hex, pos| hex * 16**pos }
    decimal = decimals.inject(0, :+)

    positive? ? decimal : 0 - decimal
  end

  private

  def positive?
    @sign == '+'
  end

  def negative?
    !positive?
  end

  def parse(hex_number)
    /^(?<sign>[+-]?)\s*(?<digits>[\da-fA-F]+)$/ =~ hex_number
    sign = '+' unless sign == '+'
    [sign, digits || '0']
  end

  def digits
    @hex_number.chars.map { |h| to_digit(h) }
  end

  def to_digit(hex)
    case hex
    when /\d/ then hex.to_i
    when /a/i then 10
    when /b/i then 11
    when /c/i then 12
    when /d/i then 13
    when /e/i then 14
    when /f/i then 15
    else fail ArgumentError,
              "ArgumentError: not a hexadecimal (#{hex} for 0..9, a-f, A-F)"
    end
  end
end

Community comments

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

I added some sign handling which was not in the tests. That made it trivially more difficult. I suppose I could replace the case statement with a hash table. I don't really need the else statement there since I can be certain that I will never pass a non-hex digit, but adding one doesn't really hurt.

Avatar of taylorzane

Not bad. I like the extra sign checking. One nit would be that you can actually remove the 0 from #inject, as it is implied if no memo is given. So you could write the expression as 8: decimal = decimals.inject(:+) with no consequence.

I also prefer using a Hash over a method with case statements. Especially since then you wouldn't necessarily have to convert the characters to integers prior to iterating over them on line 7.

Avatar of remcopeereboom

@tzglaeser

Thanks for the feedback. I'm not sure why I have the default argument in inject. I think I used a string before or something. To long ago to say for certain. I will remove.

A hash is nice, but then I would have to manually add the strings for 0-9. Another great reason I went for the case statement is that I could use regular expressions which were much easier to type than the '' on my windows laptop... (shame shame).

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?