Sort by

recency

|

956 Discussions

|

  • + 0 comments

    My java solution. Not all tests pass and i don't understand why. If i test them by hand they pass, but sending solution makes them not pass (for example test 2)

    class Result {

    /*
     * Complete the 'timeInWords' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts following parameters:
     *  1. INTEGER h
     *  2. INTEGER m
     */
    
     private static final String[] 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"
    };
    
    public static String timeInWords(int h, int m) {
    // Write your code here
    StringBuilder b = new StringBuilder();
    if(m > 0 && m <= 30) {
        System.out.print(NUMBERS[m] + " past " + NUMBERS[h]);
        b.append(NUMBERS[m] + " past " + NUMBERS[h]);
    } else if(m > 30) {
        System.out.print(NUMBERS[60-m] + " minutes to " + NUMBERS[h+1]);
        b.append(NUMBERS[60-m] + " minutes to " + NUMBERS[h+1]);
    } else {
        System.out.print(NUMBERS[h] + " o' clock");
        b.append(NUMBERS[h] + " o' clock");
    }
    return b.toString();
    }
    

    }

    Not all

  • + 0 comments

    it seems to be an easy problem, however, work it out took me a lot of time.

    There are too many details and corner case to handle.

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-the-time-in-words-problem-solution.html

  • + 1 comment

    Here is a section of code you can use just for converting the numbers 1-29 into words in python. Normally you could use :

    pip install num2words
    from num2words import num2words as n2w
    

    but the environment on hackerrank doesn't allow the "pip install num2words" command. So I put together this to use instead for this challenge.

    def n2w(n):
        words = {
            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: "fifteen",
            16: "sixteen",17: "seventeen",18: "eighteen",19: "nineteen"
        }
        if n <= 19:
            return words[n]
        base = "twenty"
        return base if n == 20 else base + " " + words[n - 20]
    
  • + 0 comments

    My Java solution with constant space and time complexity:

    private static final String[] NUM_WORDS = {
            "", "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"
        };
    
        public static String timeInWords(int h, int m) {
            if(m == 0){
                //use o' clock
                String hour = NUM_WORDS[h];
                return hour + " o' clock";
            }
            if(m <= 30){
                //use past
                String minutes;
                if(m == 1)
                    minutes = "one minute";
                 else if(m == 15)
                    minutes = "quarter";
                else if(m == 30)
                    minutes = "half";
                 else
                    minutes = NUM_WORDS[m] + " minutes";
                
                String hour = NUM_WORDS[h];
                return minutes + " past " + hour;
            }
            else{
                //use to
                String minutes;
                if(60 - m == 1)
                    minutes = "one minute";
                 else if(60 - m == 15)
                    minutes = "quarter";
                 else
                    minutes = NUM_WORDS[60 - m] + " minutes";
                    
                String hour = NUM_WORDS[(h < 12) ? h + 1 : 1];
                return minutes + " to " + hour;
            }
        }