Skip to content

Instantly share code, notes, and snippets.

@gose
Created April 30, 2012 22:30
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 gose/2563263 to your computer and use it in GitHub Desktop.
Save gose/2563263 to your computer and use it in GitHub Desktop.
Roman - Second pass
#!/usr/bin/env ruby
class Roman
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'
}
BUCKETS = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
def self.to_roman(i)
output = ''
for bucket in BUCKETS
while (i >= bucket)
output += NUMERALS[bucket]
i -= bucket
end
end
return output
end
end
for i in 1...100
puts i.to_s + ": " + Roman.to_roman(i)
end
@gose
Copy link
Author

gose commented Apr 30, 2012

Second pass with those special deduction cases sprinkled in.

1: I
2: II
3: III
4: IV
5: V
6: VI
7: VII
8: VIII
9: IX
10: X
11: XI
12: XII
13: XIII
14: XIV
15: XV
16: XVI
17: XVII
18: XVIII
19: XIX
20: XX
21: XXI
22: XXII
23: XXIII
24: XXIV
25: XXV
26: XXVI
27: XXVII
28: XXVIII
29: XXIX
30: XXX
31: XXXI
32: XXXII
33: XXXIII
34: XXXIV
35: XXXV
36: XXXVI
37: XXXVII
38: XXXVIII
39: XXXIX
40: XL
41: XLI
42: XLII
43: XLIII
44: XLIV
45: XLV
46: XLVI
47: XLVII
48: XLVIII
49: XLIX
50: L

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment