-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.rb
51 lines (45 loc) · 1.17 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
#roman numerals
@continue = true
def new_number
puts "Enter a number 1 - 999"
@number = gets.chomp
@digits_array = @number.to_s.split('')
@digits_length = @digits_array.length
@single_digit_roman = Hash[(1..9).to_a.zip(%w(I II III IV V VI VII VIII IX))]
@tens_roman = Hash[(1..9).to_a.zip(%w(X XX XXX XL L LX LXX LXXX XC))]
@hundreds_roman = Hash[(1..9).to_a.zip(%w(C CC CCC CD D DC DCC DCCC CM))]
end
def standardize
if @digits_length == 3
@digits_array
elsif @digits_length == 2
@digits_array.unshift(nil)
elsif @digits_length == 1
@digits_array.unshift(nil, nil)
end
end
def convert_digits
@roman_single = @single_digit_roman[@digits_array[2].to_i]
@roman_tens = @tens_roman[@digits_array[1].to_i]
@roman_hundreds = @hundreds_roman[@digits_array[0].to_i]
end
def recombine
roman = [@roman_hundreds, @roman_tens, @roman_single]
roman.compact!
puts "\n#{@number} in Roman Numerals is \n#{roman.join}\n "
end
def again
puts "Continue... y/n"
if gets.chomp.downcase.chars.first == 'y'
@continue = true
else
@continue = false
end
end
while @continue do
new_number()
standardize()
convert_digits()
recombine()
again()
end