Sort by

recency

|

957 Discussions

|

  • + 0 comments

    Nice — solving the same problem in five languages is a great way to compare idioms. Below I’ll give concise, actionable feedback you can apply to each implementation (correctness, edge cases, style, tests), plus language-specific tips. 11xplay Sign Up

  • + 1 comment

    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]