This repository was archived by the owner on Jul 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch9-oldroman.rb
More file actions
74 lines (57 loc) · 1.63 KB
/
ch9-oldroman.rb
File metadata and controls
74 lines (57 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# asks for an input number between 1 and 3000, and outputs it in OLD roman numerals
puts
puts 'Please enter a number between 1 and 3000 (inclusive):'
while true
number = (gets.chomp).to_i
if (number > 0) && (number <= 3000)
break
end
puts 'Your number must be between 1 and 3000. Please enter a new number.'
end
remain = number # input does not get altered below
# inputs are; the current number remaining
# and the digit '1000', '50' etc you are up translating to a roman letter
def roman (remainder, digit)
if remainder < digit # ie you do not have any of this roman letter
x = nil.to_i
else
x = remainder / digit
end
y = remainder % digit
return [x,y]
end
#new efficient method
digits = [1000,500,100,50,10,5]
letters = ['M','D','C','L','X','V','I']
output = []
i = 0
while i < 6
output [i], remain = roman remain, digits [i]
i = i + 1
end
output [6] = remain
puts
puts 'Your old-roman number equivalent is:'
print = []
i=0
while i <=6
print[i] = letters[i] * output [i]
i = i + 1
end
puts print.join
# old method - sloppier
#thou = roman number, 1000
#fivehund = roman thou[1], 500
#hund = roman fivehund[1], 100
#fifties = roman hund[1], 50
#tens = roman fifties[1], 10
#fives = roman tens[1], 5
#ones = fives [1]
#puts
#puts 'Your old-roman number equivalent is:'
#puts 'M' *thou[0] + 'D' * fivehund[0] +
# 'C' * hund[0] + 'L' * fifties[0] +
# 'X' * tens[0] + 'V' * fives[0] + 'I' * ones
#puts 'M' *output[0] + 'D' * output[1] +
# 'C' * output[2] + 'L' * output[3] +
# 'X' * output[4] + 'V' * output[5] + 'I' * output[6]