Created
January 24, 2017 05:58
-
-
Save vireshas/fb7b8efc419f561bdbb149876c326eb0 to your computer and use it in GitHub Desktop.
problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given the time in numerals we may convert it into words, as shown below: | |
5:00 => five o' clock | |
7:29 => twenty nine minutes past seven | |
5:01 => one minute past five | |
5:10 => ten minutes past five | |
5:30 => half past five | |
5:45 => quarter to six | |
5:47 => thirteen minutes to six | |
Write a program which prints the time in words for the input given in the format mentioned above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
hh = gets.strip.to_i | |
mm = gets.strip.to_i | |
def in_words(int) | |
numbers_to_name = { | |
60 => "sixty", | |
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" | |
} | |
str = "" | |
numbers_to_name.each do |num, name| | |
if int == 0 | |
return str | |
elsif int.to_s.length == 1 && int/num > 0 | |
return str + "#{name}" | |
elsif int < 100 && int/num > 0 | |
return str + "#{name}" if int%num == 0 | |
return str + "#{name} " + in_words(int%num) | |
elsif int/num > 0 | |
return str + in_words(int/num) + " #{name} " + in_words(int%num) | |
end | |
end | |
end | |
#puts hh | |
#puts mm | |
if mm == 0 | |
puts in_words(hh) + " o' clock" | |
elsif mm == 15 | |
puts "quarter past " + in_words(hh) | |
elsif mm == 45 | |
puts "quarter to " + in_words(hh + 1) | |
elsif mm == 30 | |
puts "half past " + in_words(hh) | |
elsif mm == 1 | |
puts "one minute past " + in_words(hh) | |
elsif mm > 1 and mm <= 29 | |
puts in_words(mm) + " minutes past " + in_words(hh) | |
elsif mm >= 31 and mm <= 59 | |
puts in_words(60 - mm) + " minutes to " + in_words(hh + 1) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment