Hackerrank The Time in Words Solution

Hackerrank The Time in Words Solution

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

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

At , use o' clock.  For , use past, and for  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.  It should return a time string as described.

timeInWords has the following parameter(s):

  • h: an integer representing hour of the day
  • m: an integer representing minutes after the hour

Input Format.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

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

Constraints.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

Output Format.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

Print the time in words as described.

Sample Input 0.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} 547

Sample Output 0.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} thirteen minutes to six

Sample Input 1.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} 300

Sample Output 1.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} three o' clock

Sample Input 2.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} 715

Sample Output 2.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} quarter past seven

Solution in java8

Approach 1.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scanner s = new Scanner(System.in);
        int h = s.nextInt();
        int m = s.nextInt();
        String flag;
        String str[]={"o' clock","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"};
        if(m<=30)
        {
            if(m==0)
                flag=" ";
            else if(m==1)
                flag=" minute past ";
            else if(m==15 || m==30)
                flag=" past ";
            else
                 flag=" minutes past ";
        }
        else
        {
            m=60-m;
            if(m==0)
                flag=" ";
            else if(m==1)
                flag=" minute to ";
            else if(m==15 || m==30)
                flag=" to ";
            else
                 flag=" minutes to ";   
            h=h+1;    
        }
        if(m==0)
            System.out.println(str[h]+flag+str[0]);
        else
            System.out.println(str[m]+flag+str[h]);
    }
    }

Approach 2.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        int h=Integer.parseInt(br.readLine());
        
        int m=Integer.parseInt(br.readLine());
         
        if((h>=1 && h<=12) && (m>=0 && m<=59)) {
 
        String w[]={"", "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"};
          
         String b, a;
         if(m == 1 || m == 59)
            b = "minute";
         else
            b = "minutes";
         if(h==12)
            a = w[1]; 
         else
            a = w[h+1]; 
         if(m==0)
            System.out.println(w[h]+" o' clock");
         else if(m==15)
            System.out.println("quarter past "+w[h]);
         else if(m==30)
            System.out.println("half past "+w[h]);
         else if(m==45)
            System.out.println("quarter to "+a);
         else if(m<30) 
            System.out.println(w[m]+" "+b+" past "+w[h]);
         else 
            System.out.println(w[60-m]+" "+b+" to "+a);
        }
        else
            System.out.println("Invalid Input!"); 
    }
        
        
    
}

Approach 3.

import java.io.*;
import java.util.*;

public class Timeinwords {

    public static void main(String[] args) throws Exception{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        int h=Integer.parseInt(br.readLine());
        
        int m=Integer.parseInt(br.readLine());
         
        if((h>=1 && h<=12) && (m>=0 && m<=59)) {
 
        String words[]={"", "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"};
          
         String plu, a;
         if(m == 1 || m == 59)
            plu = "minute";
         else
            plu = "minutes";
         if(h==12)
            a = words[1]; 
         else
            a = words[h+1]; 
         if(m==0)
            System.out.println(words[h]+" o' clock");
         else if(m==15)
            System.out.println("quarter past "+words[h]);
         else if(m==30)
            System.out.println("half past "+words[h]);
         else if(m==45)
            System.out.println("quarter to "+a);
         else if(m<30) 
            System.out.println(words[m]+" "+plu+" past "+words[h]);
         else 
            System.out.println(words[60-m]+" "+plu+" to "+a);
        }
        else
            System.out.println("Invalid Input!"); 
    }
        
        
    
}

Solution in python3

Approach 1.

#!/bin/python3
ByOne=[0,'one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve',
       'thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','ninteen','twenty']
ByQua=["%s o' clock", "quarter past %s", "half past %s", "quarter to %s"]
for i in range(1,10):
    ByOne.append('twenty %s'%ByOne[i])

h,m=int(input()),int(input())
hour=ByOne[h] if (m<31) else ByOne[h+1]
if not m%15:
    print(ByQua[m//15] % hour)
elif m<30:
    print("%s minutes"%ByOne[m]+"s"*(m==1)+ " past %s"%hour)
else:
    print("%s minutes"%ByOne[60-m]+"s"*(m==59)+ " to %s"%hour)

Approach 2.

#!/bin/python3

import sys


h = int(input().strip())
m = int(input().strip())

numbers={0:"zero",1:"one",2:"two",3:"three",
         4:"four",5:"five",6:"six",7:"seven",
         8:"eight",9:"nine",10:"one",11:"eleven",
         12:"twelve",13:"thirteen",14:"fourteen",
         15:"fifteen",16:"sixteen",17:"seventeen",
         18:"eighteen",19:"nineteen",20:"twenty",
         21:"twenty one",22:"twenty two",23:"twenty three",
         24:"twenty four",25:"twenty five",26:"twenty six",
         27:"twenty seven",28:"twenty eight",29:"twenty nine"

         }
saat=["o' clock","past","half past","quarter past","quarter to"]
if m==00:
    print(numbers[h],saat[0])
elif m>30:
    if m==45:
        print(saat[4],numbers[h+1])
    else:
        print(numbers[60-m],"minutes to",numbers[h + 1])
elif m==30:
    print(saat[2],numbers[h])
else:
    if m == 15:
        print(saat[3], numbers[h])
    else:
        print(numbers[m], "minutes past", numbers[h])



Approach 3.

#!/bin/python3

import sys


h = int(input().strip())
m = int(input().strip())

numbers = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven',
    8: 'eight',
    9: 'nine',
    10: 'ten',
    11: 'eleven',
    12: 'twelve',
    13: 'thirteen',
    14: 'fourteen',
    15: 'quarter',
    16: 'sixteen',
    17: 'seventeen',
    18: 'eighteen',
    19: 'nineteen',
    20: 'twenty',
    21: 'twenty one',
    22: 'twenty two',
    23: 'twenty three',
    24: 'twenty four',
    25: 'twenty five',
    26: 'twenty six',
    27: 'twenty seven',
    28: 'twenty eight',
    29: 'twenty nine',
    30: 'half'
}

to_past = 'to' if m > 30 else 'past'
minutes = '' if m % 15 == 0 else 'minutes '
if m > 30:
    m = 60 - m
    h = 1 if h == 12 else h + 1
if m == 0:
    print("{} o' clock".format(numbers[h]))
elif m == 1 or m == 59:
    print('one minute {} {}'.format(to_past, numbers[h]))
else:
    print('{} {}{} {}'.format(numbers[m], minutes, to_past, numbers[h]))

Solution in cpp

Approach 1.

#include <iostream>

static const char *numbers[] = {
  "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"
};

static const char *minSuffix(int m) {
  switch (m) {
  case 1: case 21: case 39: case 59:
    return " minute";
  case 15: case 30: case 45:
    return "";
  }

  return " minutes";
}

int main(void) {
  int h, m;
  std::cin >> h >> m;

  if (m == 0) {
    std::cout << numbers[h-1] << " o' clock" << std::endl;
  } else {
    std::cout << (m <= 30 ? numbers[m-1] : numbers[59-m])
              << minSuffix(m)
              << (m <= 30 ? " past " : " to ")
              << (m <= 30 ? numbers[h-1] : numbers[h])
              << std::endl;
  }

  return 0;
}

Approach 2.

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int h,min;
    cin>>h>>min;
    string str1[30],str2,str3;
     str1[1]="one ";
     str1[2]="two ";
     str1[3]="three ";
     str1[4]="four ";
     str1[5]="five ";
     str1[6]="six ";
     str1[7]="seven ";
     str1[8]="eight ";
     str1[9]="nine ";
     str1[10]="ten ";
     str1[11]="eleven ";
     str1[12]="twelve ";
    str1[13]="thirteen ";
     str1[14]="fourteen ";
     str1[15]="fifteen ";
     str1[16]="sixteen ";
     str1[17]="seventeen ";
     str1[18]="eighteen ";
     str1[19]="nineteen ";
     str1[20]="twenty ";
     str1[21]="twenty one ";
     str1[22]="twenty two ";
     str1[23]="twenty three ";
     str1[24]="twenty four ";
     str1[25]="twenty five ";
     str1[26]="twenty six ";
     str1[27]="twenty seven ";
     str1[28]="twenty eight ";
     str1[29]="twenty nine ";
    
    
    int x=22;
  
    //if(min>30) m=30-min; else m=min;
    if(min==0) {str2="o' clock";str3=str1[h]+str2;}
    else if(min==30) {str2="half past ";str3=str2+str1[h];}
    else if(min==15) {str2="quarter past ";str3=str2+str1[h];}
    else if(min==45) {str2="quarter to ";str3=str2+str1[h+1];}
    else
    {if(min>30){str2=str1[60-min]+"minutes to ";str3=str2+str1[h+1];}
        if(min<30){str2=str1[min]+"minutes past ";str3=str2+str1[h];}
     
    }
    cout<<str3;
    return 0;
}

Approach 3.

#include<iostream>
using namespace std;
class numtoword
{
	public:
	string units(int i)
	{
		string a[]={ "zero", "one", "two", "three",
        "four", "five", "six", "seven", "sight", "nine", "ten", "eleven",
        "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen" };
        return a[i];
	}
	string tens(int i)
	{
		string b[]={ "", "", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety" };
        return b[i];
	}
};
int main()
{
 int a,b,c,g ;
 string d,e,f;
 numtoword x;
 cin>>a>>b;
 if(b<30)
 {
 
	 if(b<20)
	 {
	 	string d=x.units(b);
	 	string e=x.units(a);
	 	if(b==1)
	 	cout<<d<<" minute past "<< e;
        else if (b==0)
	 	{
	 		cout<<e<<" o' clock";
	 	}
         else if (b==15)
             {
             cout<<"quarter past "<<e;
         }
	 	else
	 	cout<< d<<" minutes past "<< e;
	 }
	 else 
	 {
	 	c=b/10;
	 	string d=x.tens(c);
	 	b=b%10;
	 	string e=x.units(b);
	 	string f=x.units(a);
	 	cout<< d<<" "<< e<<" minutes past "<< f;
	 	
	 }
     }
	 
 else if(b==30)
 {
 	d=x.units(a);
 	cout<<"half past "<<d;
 }	
 else
 {
 	c=60-b;
 	if(c<20)
	 {
	 	string d=x.units(c);
	 	string e=x.units(a+1);
	 	if(c==1)
	 	cout<<d<<" minute to "<< e;
        else if(c==15)
            {
            cout<<"quarter to "<<e;
        }
	 	else
	 	cout<< d<<" minutes to "<< e;
	 }
	 else 
	 {
	 	g=c/10;
	 	string d=x.tens(g);
	 	b=c%10;
	 	string e=x.units(b);
	 	string f=x.units(a+1);
	 	cout<< d<<" "<< e<<" minutes to "<< f;
	 }
 }  
 return 0;
}
 

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe