In this HackerRank The Time in Words problem you have Given the time in numerals we may convert it into words.
Problem solution in Python programming.
minutes = ['zero','one','two','three','four','five', 'six','seven','eight','nine','ten', 'eleven','twelve','thirteen','fourteen', 'fifteen','sixteen','seventeen','eighteen', 'nineteen','twenty','twenty one', 'twenty two', 'twenty three','twenty four','twenty five', 'twenty six','twenty seven','twenty eight', 'twenty nine', 'thirty'] hours = ['zero','one','two','three','four','five', 'six','seven','eight','nine','ten','eleven','twelve'] H = int(input()) M = int(input()) if M == 0: print(hours[H],"o' clock") elif M == 15: print("quarter past",hours[H]) elif M == 30: print("half past",hours[H]) elif M == 45: if H == 12: print("quarter to",hours[1]) else: print("quarter to",hours[H+1]) elif M > 0 and M < 30: print(minutes[M],"minutes past",hours[H]) elif M > 30 and M < 60: if H == 12: print(minutes[60-M],"minutes to",hours[1]) else: print(minutes[60-M],"minutes to",hours[H+1])
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Integer hour = Integer.parseInt(sc.nextLine()); Integer min = Integer.parseInt(sc.nextLine()); String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; String[] tensNames = { "", " ten", " twenty", " thirty", " fourty", " fifty", " sixty", " seventy", " eighty", " ninety" }; String sepString1 = " minutes past "; String sepString2 = " minutes to "; String minString = ""; if (min == 0) System.out.println(numNames[hour].trim() + " o' clock"); else if (min == 15) System.out.println("quarter past " + numNames[hour].trim()); else if (min < 30) { if (min == 1) sepString1 = " minute past "; if (min < 20) minString = numNames[min % 20]; else { minString = numNames[min % 10]; min /= 10; minString = tensNames[min % 10] + minString; } System.out.println(minString.trim() + sepString1 + numNames[hour].trim()); } else if (min == 30) System.out.println("half past " + numNames[hour].trim()); else if (min == 45) System.out.println("quarter to " + numNames[hour + 1 < 12 ? hour + 1 : 1].trim()); else if (min > 30) { min = 60 - min; if (min == 1) sepString2 = " minute to "; if (min < 20) minString = numNames[min % 20]; else { minString = numNames[min % 10]; min /= 10; minString = tensNames[min % 10] + minString; } System.out.println(minString.trim() + sepString2 + numNames[hour + 1 <= 12 ? hour + 1 : 1].trim()); } } }
Problem solution in C++ programming.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; string nums[30] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine", "half" }; int main() { int H, M; cin >> H >> M; string joiner; if (M == 0){ cout << nums[H-1] << " o' clock"; return 0; } else if (M > 30) { joiner = "to"; M = 60 - M; H %= 12; H ++; } else { joiner = "past"; } string min = " minutes "; if (M == 1) min = " minute "; if (M == 15 || M == 30) { min = " "; } cout << nums[M-1] << min << joiner << " " << nums[H-1]; /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }
Problem solution in C programming.
#include <stdio.h> const char *min[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty one", "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty senen", "twenty eight", "twenty nine", "half past", }; //#define stMin (sizeof(min) / sizeof(const char *)) int main() { int H, M; //int T; //scanf(" %d", &T); //while(T--) { scanf(" %d %d", &H, &M); //printf("n%d:%d = ", H, M); if(M <= 30) { if(M == 0) printf("%s o' clock", min[H]); else if(M == 1) printf("%s minute past %s", min[M], min[H]); else if(M == 15) printf("quarter past %s", min[H]); else if(M == 30) printf("half past %s", min[H]); else printf("%s minutes past %s", min[M], min[H]); } else { M = 60 - M; if(++H == 13) H = 1; if(M == 1) printf("%s minute to %s", min[M], min[H]); else if(M == 15) printf("quarter to %s", min[H]); else printf("%s minutes to %s", min[M], min[H]); } //printf("n"); //} return 0; }
Problem solution in JavaScript programming.
function getInt(number) { return parseInt(number, 10); } function hTranslator(number) { var words; number = getInt(number); switch (number) { case 1: words = 'one'; break; case 2: words = 'two'; break; case 3: words = 'three'; break; case 4: words = 'four'; break; case 5: words = 'five'; break; case 6: words = 'six'; break; case 7: words = 'seven'; break; case 8: words = 'eight'; break; case 9: words = 'nine'; break; case 10: words = 'ten'; break; case 11: words = 'eleven'; break; case 12: words = 'twelve'; break; } return words; } function mTranslator(number) { var words = []; number = getInt(number); if (number === 30) { words.push('half'); } else if (number > 19) { words.push('twenty'); number -= 20; } else if (number > 9) { switch (number) { case 10: words.push('ten'); break; case 11: words.push('eleven'); break; case 12: words.push('twelve'); break; case 13: words.push('thirteen'); break; case 14: words.push('fourteen'); break; case 15: words.push('quarter'); break; case 16: words.push('sixteen'); break; case 17: words.push('seventeen'); break; case 18: words.push('eightteen'); break; case 19: words.push('nineteen'); break; } } if (number > 19 || number < 10) { switch (number) { case 1: words.push('one'); break; case 2: words.push('two'); break; case 3: words.push('three'); break; case 4: words.push('four'); break; case 5: words.push('five'); break; case 6: words.push('six'); break; case 7: words.push('seven'); break; case 8: words.push('eight'); break; case 9: words.push('nine'); break; } } return words.join(' '); } function processData(input) { var data = input.split(/n/), hours = getInt(data[0].trim()), minutes = getInt(data[1].trim()), middle, timeString = []; if (minutes == 0) { timeString = hTranslator(hours) + ' o' clock'; } else { if (minutes > 30) { if (hours === 12) { hours = 1; } else { ++hours; } minutes = 60 - minutes; middle = 'to'; //timeString = 'to ' + hTranslator(hours); } else { middle = 'past'; //timeString = 'past ' + hTranslator(hours); } if ([15, 30].indexOf(minutes) === -1) { //timeString = ' minute' + (minutes === 1 ? ' ' : 's ') + timeString; } timeString.push(mTranslator(minutes)); if ([15, 30].indexOf(minutes) === -1) { timeString.push('minute' + (minutes === 1 ? '' : 's')); } timeString.push(middle); timeString.push(hTranslator(hours)); timeString = timeString.join(' '); } process.stdout.write(timeString); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });