Created
April 30, 2012 22:27
-
-
Save gose/2563248 to your computer and use it in GitHub Desktop.
Roman - First pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Roman | |
NUMERALS = { | |
1 => 'I', | |
5 => 'V', | |
10 => 'X', | |
50 => 'L', | |
100 => 'C', | |
500 => 'D', | |
1000 => 'M' | |
} | |
BUCKETS = [1000, 500, 100, 50, 10, 5, 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output showing the first pass lacks the deduction behavior.
1: I
2: II
3: III
4: IIII
5: V
6: VI
7: VII
8: VIII
9: VIIII
10: X
11: XI
12: XII
13: XIII
14: XIIII
15: XV
16: XVI
17: XVII
18: XVIII
19: XVIIII
20: XX
21: XXI
22: XXII
23: XXIII
24: XXIIII
25: XXV
26: XXVI
27: XXVII
28: XXVIII
29: XXVIIII
30: XXX
31: XXXI
32: XXXII
33: XXXIII
34: XXXIIII
35: XXXV
36: XXXVI
37: XXXVII
38: XXXVIII
39: XXXVIIII
40: XXXX
41: XXXXI
42: XXXXII
43: XXXXIII
44: XXXXIIII
45: XXXXV
46: XXXXVI
47: XXXXVII
48: XXXXVIII
49: XXXXVIIII
50: L