-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeInWords.rb
73 lines (62 loc) · 1.33 KB
/
timeInWords.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
#link: https://www.hackerrank.com/challenges/the-time-in-words/problem
#!/bin/ruby
require 'json'
require 'stringio'
def timeInWords(h, m)
if m == 0
numberToName(h) + " o' clock"
elsif m <= 30
minutesInWords(m) + " past " + numberToName(h)
else
minutesInWords(60 - m) + " to " + numberToName(h + 1)
end
end
def minutesInWords(minutes)
case minutes
when 15
"quarter"
when 30
"half"
else
numberToName(minutes) + " minute" + (minutes > 1 ? "s" : "")
end
end
def numberToName(num)
numbersToName = {
50 => "fifty",
40 => "forty",
30 => "thirty",
20 => "twenty",
19 => "nineteen",
18 => "eighteen",
17 => "seventeen",
16 => "sixteen",
15 => "fifteen",
14 => "fourteen",
13 => "thirteen",
12 => "twelve",
11 => "eleven",
10 => "ten",
9 => "nine",
8 => "eight",
7 => "seven",
6 => "six",
5 => "five",
4 => "four",
3 => "three",
2 => "two",
1 => "one"
}
if num <= 20
numbersToName[num]
else
numbersToName[20] + " " + numbersToName[num - 20]
end
end
fptr = File.open(ENV['OUTPUT_PATH'], 'w')
h = gets.to_i
m = gets.to_i
result = timeInWords h, m
fptr.write result
fptr.write "\n"
fptr.close()