The Time in Words HackerRank Solution in C, C++, Java, Python

Given the time in numerals we may convert it into words, as shown below:

At minutes = 0, use o’ clock. For 1<=minutes<=30, use past, and for 30<minutes use to. Note the space between the apostrophe and clock in o’ clock. Write a program which prints the time in words for the input given in the format described.

Function Description

Complete the timeInWords function in the editor below.

timeInWords has the following parameter(s):

int h: the hour of the day
int m: the minutes after the hour

Returns

string: a time string as described

Input Format

The first line contains , the hours portion The second line contains , the minutes portion

Sample Input 0

5
47

Sample Output 0

thirteen minutes to six
Sample Input 1

3
00

Sample Output 1

three o' clock
Sample Input 2

7
15

Sample Output 2

quarter past seven

 

The Time in Words HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

char words[][21] = {"o' clock", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};

int main() {

    int minutes, hours;
    int half = 0; //0 if minutes are less than 30, 1 if greater than 30
    scanf("%d %d", &hours, &minutes);
    
    if (minutes > 30) {
        minutes = 60-minutes; //to account for the "To"
        half = 1;
    }
    
    if (minutes < 21 && minutes != 15 && minutes != 0 && minutes != 1) {
        printf("%s minutes ", words[minutes]);
    }
    else if (minutes == 1) {
        printf("%s minute ", words[minutes]);
    }
    else if (minutes > 20 && minutes < 30) {
        printf("%s %s minutes ", words[20], words[minutes-20]);
    }
    else if (minutes == 15) {
        printf("quarter ");
    }
    else if (minutes == 30) {
        printf("half ");
    }
    
    if (half && minutes != 0) {
        printf("to %s", words[hours+1]);
    }
    else if (!half && minutes != 0){
        printf("past %s", words[hours]);
    }
    else {
        printf("%s %s", words[hours], words[0]);
    }
            
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

 

The Time in Words HackerRank Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string words[30] = {string("one"),string("two"),string("three"),string("four"),string("five"),string("six"),string("seven"),string("eight"),string("nine"),string("ten"),string("eleven"),string("twelve"),string("thirteen"),string("fourteen"),string("fifteen"),string("sixteen"),string("seventeen"),string("eighteen"),string("nineteen"),string("twenty"),string("twenty one"),string("twenty two"),string("twenty three"),string("twenty four"),string("twenty five"),string("twenty six"),string("twenty seven"),string("twenty eight"),string("twenty nine")};


int main() {
    int H,M;
    cin >> H >> M;
    if (M==0) {
        printf("%s o' clock\n",words[H-1].c_str());
    } else if (M==1) {
        printf("one minute past %s\n",words[H-1].c_str());
    } else if (M==15) {
        printf("quarter past %s\n",words[H-1].c_str());
    } else if (M<30) {
        printf("%s minutes past %s\n",words[M-1].c_str(),words[H-1].c_str());
    } else if (M==30) {
        printf("half past %s\n",words[H-1].c_str());
    } else if (M==45) {
        printf("quarter to %s\n",words[H%12].c_str());
    } else if (M==59) {
        printf("one minute to %s\n",words[H%12].c_str());
    } else {
        printf("%s minutes to %s\n",words[60-M-1].c_str(),words[H%12].c_str());
    }
    
    return 0;
}

 

The Time in Words HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        String[] numberWords = new String[] {
                "",
                "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"
        };
        
        Scanner in = new Scanner(System.in);
        int hour = in.nextInt();
        int minute = in.nextInt();
        
        int nextHour = (hour % 12) + 1;
        
        if(minute == 0) {
            System.out.printf("%s o' clock\n", numberWords[hour]);
        }
        
        else if(minute == 15) {
            System.out.printf("quarter past %s\n", numberWords[hour]);
        }
        
        else if(minute == 30) {
            System.out.printf("half past %s\n", numberWords[hour]);
        }
        
        else if(minute == 45) {
            System.out.printf("quarter to %s\n", numberWords[nextHour]);
        }
        
        else if(minute < 30) {
            System.out.printf("%s minutes past %s\n", numberWords[minute], numberWords[hour]);
        }
        
        else {
            System.out.printf("%s minutes to %s\n", numberWords[60 - minute], numberWords[nextHour]);
        }
    }
}

 

The Time in Words HackerRank Solution in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
H = int(raw_input())
M = int(raw_input())

def word(x):
    unit = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    dec = ["", "ten", "twenty", "thirty", "forty", "fifty"]
    if x < 20:
        return unit[x]
    return dec[x/10] + " " + unit[x%10]

def minu(x):
    w = word(x)
    if x == 1: return w + " minute"
    else: return w + " minutes"

if M == 0:
    print "{} o' clock".format(word(H))
elif M == 15:
    print "quarter past {}".format(word(H))
elif M == 45:
    print "quarter to {}".format(word(H+1))
elif M == 30:
    print "half past {}".format(word(H))
elif M < 30:
    print "{} past {}".format(minu(M), word(H))
else:
    print "{} to {}".format(minu(60-M), word(H+1))

 

The Time in Words HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
 static string[] numbers = {"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"};
        static void Main(String[] args)
        {
            int h = Int32.Parse(Console.ReadLine());
            int m = Int32.Parse(Console.ReadLine());

            if (m == 0)
                Console.WriteLine(numbers[h] + " o' clock");
            else if (m < 30)
            {
                if(m == 1)
                    Console.WriteLine(numbers[m] + " minute past " + numbers[h]);
                else if(m == 15)
                    Console.WriteLine("quarter" + " past " + numbers[h]);      
                else
                    Console.WriteLine(numbers[m] + " minutes past " + numbers[h]);
            }
            else if (m == 30)
            {
                Console.WriteLine("half past " + numbers[h]);
            }
            else if (m < 45)
            {
                if (h + 1 > 12)
                    h -= 12;
                Console.WriteLine(numbers[60 - m] + " minutes to " + numbers[h + 1]);
            }
            else if (m == 45)
            {
                if (h + 1 > 12)
                    h -= 12;
                Console.WriteLine("quarter to " + numbers[h + 1]);
            }
            else if (m < 60)
            {
                if (h + 1 > 12)
                    h -= 12;
                if (60 - m == 1)
                    Console.WriteLine(numbers[60 - m] + " minute to " + numbers[h + 1]);
                else
                    Console.WriteLine(numbers[60 - m] + " minutes to " + numbers[h + 1]);
            }
        }
}

 

Attempt The Time in Words HackerRank Challenge

Link –¬†https://www.hackerrank.com/challenges/the-time-in-words/

Next HackerRank Challenge Solution 

Link –¬†https://exploringbits.com/chocolate-feast-hackerrank-solution/

Leave a Comment