-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman.rb
345 lines (313 loc) · 6.07 KB
/
roman.rb
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
=begin
This commented part of the program was an exercise or two, but I decided to make this program more about roman numerals. see below
puts 'Do you want me to sing a song?'
song = gets.chomp.downcase
if song == 'yes'
n = 99
while n>1
puts n.to_s + ' bottles of beer on the wall,'
puts n.to_s + ' bottles of beer.'
puts 'take one down, pass it around'
if n > 2
puts (n-1).to_s + ' bottles of beer on the wall'
elsif n == 2
puts '1 bottle of beer on the wall'
end
n -= 1
end
puts '1 bottle of beer on the wall,'
puts '1 bottles of beer.'
puts 'take one down, pass it around'
puts '0 bottles of beer on the wall'
end
puts
puts 'Do you want to talk to grandma?'
var = gets.chomp.downcase
if var == 'yes'
puts 'ok, what do you want to say?'
words = gets.chomp
while words != 'BYE'
if words != words.upcase
puts 'HUH?! SPEAK UP SONNY!'
elsif words == words.upcase
puts "not since " + (rand(20) + 1930).to_s
end
words = gets.chomp
end
elsif var == 'no'
puts 'wise choice. she never can hear you and never stops talking'
end
=end
# practice with recursion. making a factorial method
class Integer
def factorial
if self < 0
return 'nonsense'
elsif self == 0 || self == 1
return 1
elsif self > 1
return self*((self-1).factorial)
end
end
end
# roman numeral methods
def old_school(num) # this changes a number into roman numerals, not using the IV notation for 4
m = 0 # 1000
d = 0 # 500
c = 0 # 100
l = 0 # 50
x = 0 # 10
v = 0 # 5
i = 0 # 1
num2 = num
if num >= 1000
m = num/1000
num -= 1000*m
end
if num >= 500
d = 1
num -= 500
end
if num >= 100
c = num/100
num -= 100*c
end
if num >= 50
l = 1
num -= 50
end
if num >= 10
x = num/10
num -= 10*x
end
if num >= 5
v = 1
num -= 5
end
i = num
puts "#{num2} in Roman Numerals is #{'M'*m + 'D'*d + 'C'*c + 'L'*l + 'X'*x + 'V'*v + 'I'*i}"
'M'*m + 'D'*d + 'C'*c + 'L'*l + 'X'*x + 'V'*v + 'I'*i
end
def modern(num) # this changes a number to roman numerals with the special notation IV, IX...etc
m = 0 # 1000
d = 0 # 500
c = 0 # 100
l = 0 # 50
x = 0 # 10
v = 0 # 5
i = 0 # 1
iv = 0 # 4
ix = 0 # 9
xl = 0 # 40
xc = 0 # 90
cd = 0 # 400
cm = 0 # 900
n = num
if n >= 1000
m = n/1000
n -= 1000*m
end
if n >= 500
d = 1
n -= 500
end
if n >= 100
c = n/100
n -= 100*c
end
if n >= 50
l = 1
n -= 50
end
if n >= 10
x = n/10
n -= 10*x
end
if n >= 5
v = 1
n -= 5
end
i = n
if num%10 == 4
i = 0
iv = 1
end
if num%10 == 9
v = 0
i = 0
ix = 1
end
if (num%100)/10 == 4
x = 0
xl = 1
end
if (num%100)/10 == 9
l = 0
x = 0
xc = 1
end
if (num%1000)/100 == 4
c = 0
cd = 1
end
if (num%1000)/100 == 9
c = 0
d = 0
cm = 1
end
puts "#{num} in Roman Numerals is #{'M'*m + 'D'*d + 'CD'*cd + 'CM'*cm + 'C'*c + 'L'*l + 'XL'*xl + 'XC'*xc + 'X'*x + 'V'*v + 'I'*i + 'IV'*iv + 'IX'*ix}"
'M'*m + 'D'*d + 'CD'*cd + 'CM'*cm + 'C'*c + 'L'*l + 'XL'*xl + 'XC'*xc + 'X'*x + 'V'*v + 'I'*i + 'IV'*iv + 'IX'*ix
end
# roman numerals to numbers within the String class, so it can be called with 'XIV'.to_num
class String
def to_num
n = 0
roman_key = {
'i' => 1,
'v' => 5,
'x' => 10,
'l' => 50,
'c' => 100,
'd' => 500,
'm' => 1000
}
roman = downcase.split(//)
k = 0
while k < roman.length
unless roman_key[roman[k]]
puts 'that is not a valid roman numeral'
return
end
k += 1
end
k = 0
while k < roman.length - 1
if roman_key[roman[k]] < roman_key[roman[k+1]]
puts 'that is not a valid roman numeral'
return
end
k += 1
end
roman.each do |y|
n += roman_key[y]
end
puts "#{self} represents #{n}"
end
end
# make the number to modern roman numerals method a true method on integers.
class Integer
def to_roman
m = 0 # 1000
d = 0 # 500
c = 0 # 100
l = 0 # 50
x = 0 # 10
v = 0 # 5
i = 0 # 1
iv = 0 # 4
ix = 0 # 9
xl = 0 # 40
xc = 0 # 90
cd = 0 # 400
cm = 0 # 900
n = self
if n >= 1000
m = n/1000
n 1000*m
end
if n >= 500
d = 1
n -= 500
end
if n >= 100
c = n/100
n -= 100*c
end
if n >= 50
l = 1
n -= 50
end
if n >= 10
x = n/10
n -= 10*x
end
if n >= 5
v = 1
n -= 5
end
i = n
if self%10 == 4
i = 0
iv = 1
end
if self%10 == 9
v = 0
i = 0
ix = 1
end
if (self%100)/10 == 4
x = 0
xl = 1
end
if (self%100)/10 == 9
l = 0
x = 0
xc = 1
end
if (self%1000)/100 == 4
c = 0
cd = 1
end
if (self%1000)/100 == 9
c = 0
d = 0
cm = 1
end
# puts "#{self} in Roman Numerals is #{'M'*m + 'D'*d + 'CD'*cd + 'CM'*cm + 'C'*c + 'L'*l + 'XL'*xl + 'XC'*xc + 'X'*x + 'V'*v + 'I'*i + 'IV'*iv + 'IX'*ix}"
'M'*m + 'D'*d + 'CD'*cd + 'CM'*cm + 'C'*c + 'L'*l + 'XL'*xl + 'XC'*xc + 'X'*x + 'V'*v + 'I'*i + 'IV'*iv + 'IX'*ix
end
end
=begin
while true
puts "ok, now we're going to convert number into roman numerals."
puts 'type old school or modern.'
reply = gets.chomp.downcase
if reply == 'old school'
puts 'What number would you like to write in old-school roman numerals?'
number = gets.chomp.to_i
puts old_school number
break
elsif reply == 'modern'
puts 'What number would you like to write in modern roman numerals?'
number = gets.chomp.to_i
puts modern number
break
else
puts 'please type \'old school\' or \'modern\''
end
end
=end
class Die
def initialize
roll
end
def roll
@num_shown = 1 + rand(6)
end
def set(num)
if num > 0 && num < 7
@num_shown = num
else
puts 'invalid number for a die'
end
end
def show
puts @num_shown
end
end
def time_counter(_block_description, &block)
start_time = Time.new
block.call
running_time = Time.new - start_time
puts "#{block} took #{running_time} seconds."
end