🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of remcopeereboom

remcopeereboom's solution

to Roman Numerals in the Ruby Track

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

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

 1  => I
10  => X
 7  => VII

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

See also: http://www.novaroma.org/via_romana/numbers.html


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 roman_numerals_test.rb

To include color from the command line:

ruby -r minitest/pride roman_numerals_test.rb

Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

Submitting Incomplete Solutions

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

roman_numerals_test.rb

require 'minitest/autorun'
require_relative 'roman_numerals'

# Common test data version: 1.0.0 070e8d5
class RomanNumeralsTest < Minitest::Test
  def test_1
    # skip
    assert_equal 'I', 1.to_roman
  end

  def test_2
    skip
    assert_equal 'II', 2.to_roman
  end

  def test_3
    skip
    assert_equal 'III', 3.to_roman
  end

  def test_4
    skip
    assert_equal 'IV', 4.to_roman
  end

  def test_5
    skip
    assert_equal 'V', 5.to_roman
  end

  def test_6
    skip
    assert_equal 'VI', 6.to_roman
  end

  def test_9
    skip
    assert_equal 'IX', 9.to_roman
  end

  def test_27
    skip
    assert_equal 'XXVII', 27.to_roman
  end

  def test_48
    skip
    assert_equal 'XLVIII', 48.to_roman
  end

  def test_59
    skip
    assert_equal 'LIX', 59.to_roman
  end

  def test_93
    skip
    assert_equal 'XCIII', 93.to_roman
  end

  def test_141
    skip
    assert_equal 'CXLI', 141.to_roman
  end

  def test_163
    skip
    assert_equal 'CLXIII', 163.to_roman
  end

  def test_402
    skip
    assert_equal 'CDII', 402.to_roman
  end

  def test_575
    skip
    assert_equal 'DLXXV', 575.to_roman
  end

  def test_911
    skip
    assert_equal 'CMXI', 911.to_roman
  end

  def test_1024
    skip
    assert_equal 'MXXIV', 1024.to_roman
  end

  def test_3000
    skip
    assert_equal 'MMM', 3000.to_roman
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 2, BookKeeping::VERSION
  end
end
class Fixnum
  # Returns a string containing the roman numeral representation.
  #
  # @return [String] The roman numeral as a string.
  #   The empty string if self == 0.
  # @raise [RangeError] if called on a negative number.
  def to_roman
    fail RangeError "Romans didn't use negative numbers #{self}" if self < 0

    x = self
    roman_digits = [[1, 'I'], [4, 'IV'], [5, 'V'], [9, 'IX'],
                    [10, 'X'], [40, 'XL'], [50, 'L'], [90, 'XC'],
                    [100, 'C'], [400, 'CD'], [500, 'D'], [900, 'CM'],
                    [1000, 'M']].reverse

    roman_digits.inject('') do |acc, (decimal, roman)|
      factor, x = x.divmod(decimal)
      acc << roman * factor
    end
  end
end

Community comments

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

Now using divmod to get rid of the while loop.

I also removed the constant. Fixnum is used everywhere so we want to make as few changes to it as possible.

Maybe the code should be extracted to its own module, but I'm fine with this for now.

Avatar of kosh-jelly

Nice point about avoiding adding the data within the method instead of as a constant because we're monkey patching. I hadn't thought about that

Avatar of remcopeereboom

@kosh-jelly Yeah, it is not a biggy, but I noticed I was suggesting it to others, so I went ahead and changed it in my own code.

Avatar of dustinleblanc

I love your documentation at the top of your method. I don't see a lot of this in Ruby and it is a welcome addition. Makes me feel like I'm reading well documented PHP or Java!

I handled keeping the constant outside Fixnum by declaring a class directly above called RomanNumeral and just defined the constant there. You could also use a hash instead of the 2D array.

Avatar of remcopeereboom

@dustinleblanc

I love your documentation at the top of your method. I don't see a lot of this in Ruby and it is a welcome addition. Makes me feel like I'm reading well documented PHP or Java!

Thanks, of course, ruby code is a lot more self explanatory than most PHP and JAva code!

I handled keeping the constant outside Fixnum by declaring a class directly above called RomanNumeral and just defined the constant there

Yeah, an extra class would be a good way to get rid of the constant as well. But a module would be even better.

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?