Skip to content

Instantly share code, notes, and snippets.

@ahimmelstoss
Created October 14, 2013 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahimmelstoss/6983798 to your computer and use it in GitHub Desktop.
Save ahimmelstoss/6983798 to your computer and use it in GitHub Desktop.
# # 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).
# Write a function to convert from normal numbers to Roman Numerals: e.g.
# ```
# 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
#I (1), V(5), X(10), L(50), C(100), D(500), M(1000)
class Integer
NUMERALS =
{
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"
}
def initialize(number)
@number = number
end
def lower_key(number)
numbers_array = NUMERALS.collect {|k,v| k} #make an array of all of the numbers
numbers_array << number
numbers_array.uniq!
numbers_array.sort! #put the number in there if not there and sort
index = numbers_array.find_index(number) #find index of that number
return numbers_array[index - 1] #lower_key(number) returns the number below the argument number
end
def to_roman
number = self
roman_numeral= ""
while number > 0
if NUMERALS.has_key?(number) #if NUMERAL includes the number, represented as key
roman_numeral << NUMERALS[number] #push that numeral into the roman_numeral string
return roman_numeral # return that
else
roman_numeral += NUMERALS[lower_key(number)] #else first add the return of lower_key
number -= lower_key(number) #the remainder of the above line, to then get looped through again
end
end
end
end
RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :progress # :progress, :html, :textmate
end
describe Integer, '#to_roman' do
it 'converts 1' do
1.to_roman.should eq 'I'
end
it 'converts 2' do
2.to_roman.should eq 'II'
end
it 'converts 3' do
3.to_roman.should eq 'III'
end
it 'converts 4' do
4.to_roman.should eq 'IV'
end
it 'converts 5' do
5.to_roman.should eq 'V'
end
it 'converts 6' do
6.to_roman.should eq 'VI'
end
it 'converts 9' do
9.to_roman.should eq 'IX'
end
it 'converts 27' do
27.to_roman.should eq 'XXVII'
end
it 'converts 48' do
48.to_roman.should eq 'XLVIII'
end
it 'converts 59' do
59.to_roman.should eq 'LIX'
end
it 'converts 93' do
93.to_roman.should eq 'XCIII'
end
it 'converts 141' do
141.to_roman.should eq 'CXLI'
end
it 'converts 163' do
163.to_roman.should eq 'CLXIII'
end
it 'converts 402' do
402.to_roman.should eq 'CDII'
end
it 'converts 575' do
575.to_roman.should eq 'DLXXV'
end
it 'converts 911' do
911.to_roman.should eq 'CMXI'
end
it 'converts 1024' do
1024.to_roman.should eq 'MXXIV'
end
it 'converts 3000' do
3000.to_roman.should eq 'MMM'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment