Sort by

recency

|

962 Discussions

|

  • + 0 comments

    Here is problem solution in python, java,c++, c and javascript programming - https://programmingoneonone.com/hackerrank-the-time-in-words-problem-solution.html

  • + 0 comments

    c++ solution:

    string encodeDigit(int n) {
        switch (n) {
            case 1:
                return "one";
            case 2:
                return "two";
            case 3:
                return "three";
            case 4:
                return "four";
            case 5:
                return "five";
            case 6:
                return "six";
            case 7:
                return "seven";
            case 8:
                return "eight";
            case 9:
                return "nine";
            default:
                return "";
        }
    }
    
    string encodePreTwenties(int n) {
        switch (n) {
            case 10:
                return "ten";
            case 11:
                return "eleven";
            case 12:
                return "twelve";
            case 13:
                return "thirteen";
            case 14:
                return "forteen";
            case 15:
                return "fifteen";
            case 16:
                return "sixteen";
            case 17:
                return "seventeen";
            case 18:
                return "eigteen";
            case 19:
                return "nineteen";
            default:
                return "";
        }
    }
    
    string encodeHour(int hour) {
        if (hour > 12) {
            hour = hour % 12;
        }
        
        if (hour < 10) {
            return encodeDigit(hour);
        }
        
        return encodePreTwenties(hour);
    }
     
    // excludes special cases like quarter
    // minutes < 30
    string encodeMinutes(int minutes) {
        if (minutes == 1) {
            return "one minute";    
        }
        
        if (minutes < 10) {
            return encodeDigit(minutes) + " minutes";
        }
        
        if (minutes < 20) {
            return encodePreTwenties(minutes) + " minutes";
        }
        
        if (minutes == 20) {
            return "twenty minutes";
        }
        
        return "twenty " + encodeDigit(minutes - 20) + " minutes";
    }
    
    string timeInWords(int h, int m) {
        if (m == 0) {
            return encodeHour(h) + " o' clock";  
        }
        
        if (m == 15) {
            return "quarter past " + encodeHour(h);  
        }
        
        if (m == 30) {
            return "half past " + encodeHour(h);  
        }
        
        if (m == 45) {
            return "quarter to " + encodeHour(h + 1);
        }
        
        if (m <= 30) {
            return encodeMinutes(m) + " past " + encodeHour(h);
        }
        
        // > 30 minutes
        return encodeMinutes(60 - m) +  " to " + encodeHour(h + 1);
    }
    
  • + 0 comments

    Live by the library, die by the library I guess.

    I was working with Swift, and Foundation has a spell out style number formatter. So I decided to use it.

    Passed all the "open" test cases, failed two of the locked ones. Made the example section into another batch of test cases, and low and behold I was generating "twenty-eight minutes past five" and the example had no dash.

    So anyone else deciding to save time by using the built in Foundation number formatter (hey if it were a real world job I would be upset at a PR trying to spell out numbers and not just using the libary without good reason!) should be aware they need to post process hyphens into spaces....

  • + 0 comments

    Live by the library, die by the library I guess.

    I was working with Swift, and Foundation has a spell out style number formatter. So I decided to use it.

    Passed all the "open" test cases, failed two of the locked ones. Made the example section into another batch of test cases, and low and behold I was generating "twenty-eight minutes past five" and the example had no dash.

    So anyone else deciding to save time by using the built in Foundation number formatter (hey if it were a real world job I would be upset at a PR trying to spell out numbers and not just using the libary without good reason!) should be aware they need to post process hyphens into spaces....

  • + 0 comments

    My python solution:

    def timeInWords(h, m):

    nums = ["one",'two','three','four','five','six','seven','eight','nine','ten','eleven','tweleve', 'thirteen', 'fourteen',15,'sixteen', 'seventeen', 'eighteen','nineteen','twenty','twenty one','twenty two','twenty three', 'twenty four','twenty five','twenty six','twenty seven','twenty eight', 'twenty nine']
    
    phrases = {0:"o' clock", 
           15:"quarter past", 30:'half past', 
           45:"quarter to", }
    if m==1 and h==1:
        return 'one minute past one'
    if m==0:
        return nums[h-1]+' '+phrases[m]
    elif m==45:
        return phrases[m]+' '+nums[h]
    elif m==15:
        return phrases[m]+' '+nums[h-1]
    elif m==30:
        return phrases[m]+' '+nums[h-1]
    elif 1<=m<=30:
        return nums[m-1]+' minutes past '+ nums[h-1]
    else:
        k=60-m
        return nums[k-1]+' minutes to '+nums[h]