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-newroman.rb
More file actions
89 lines (70 loc) · 2.05 KB
/
Copy pathch9-newroman.rb
File metadata and controls
89 lines (70 loc) · 2.05 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# asks for an input number between 1 and 3000, and outputs it in NEW 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
digits = [1000,500,100,50,10,5,1]
letters = ['M','D','C','L','X','V','I']
# 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 current roman letter
x, y, z = nil
a = remainder
else # need to evaluate for current roman letter
if remainder.to_s[/\d/] == '9'
x = z = 1
y = nil
a = remainder % (digit/5)
elsif remainder.to_s[/\d/] == '4'
x = nil
y = z = 1
a = remainder % digit
else # does not begin with 4 or 9, and can be processed normally
x, z = nil
y = remainder / digit
a = remainder % digit
end
end
[x,y,z,a]
# x = number of times to print next roman letter
# y = number of times to print current roman letter
# z = number of times to print previous roman letter
# a = remainder for next call
end
output = [] # stores arrays of x,y,z,a output from roman
i = 0
puts
puts 'Your new-roman number equivalent is:'
# run method for each value in digits []
while i <=6
output [i] = roman remain, digits [i]
remain = output [i][3]
i = i + 1
end
# print {next * x + current * y + previous * z } for each iteration of letter
print = []
temp = []
i=0
while i <=6 # for each roman letter
if i == 6
temp [0] = nil
else
temp[0] = letters[i+1] * output [i][0].to_i
end
temp[1] = letters[i] * output [i][1].to_i
if i == 0
temp [2] = nil
else
temp[2] = letters[i-1] * output [i][2].to_i
end
print[i] = temp.join
i = i + 1
end
puts print.join