Sort by

recency

|

941 Discussions

|

  • + 0 comments

    C++ Solution

    const map<int, string> nums{
        {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, "thirty"}};
        
    string timeInWords(int h, int m) {
      
      if (m == 0) {
        return nums.at(h) + " o' clock";
      } else
      if(m > 30) {
        m = 60 - m;
        
        return nums.at(m) + 
          (m != 15 ? ((m == 1 || m == 21) ? " minute" : " minutes") : "") +
          " to " + nums.at((h+1 < 12) ? h+1 : 1);
      } else 
      if (m == 30) {
        return "half past " + nums.at(h);
      } else {
        return nums.at(m) + 
          (m != 15 ? ((m == 1 || m == 21) ? " minute" : " minutes") : "") +
          " past " + nums.at(h);
      }
    }
    
  • + 0 comments
    def timeInWords(h, m):
        num_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: "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"
        }
        
        if m == 0:
            return f"{num_words[h]} o' clock"
        
        if m == 15 or m == 30:
            return f"{num_words[m]} past {num_words[h]}"
        
        if m == 45:
            next_hour = h + 1 if h < 12 else 1
            return f"quarter to {num_words[next_hour]}"
        
        if m < 30:
            minute_word = "minute" if m == 1 else "minutes"
            return f"{num_words[m]} {minute_word} past {num_words[h]}"
        
        next_hour = h + 1 if h < 12 else 1
        minute_word = "minute" if (60 - m) == 1 else "minutes"
        return f"{num_words[60 - m]} {minute_word} to {num_words[next_hour]}"
    
  • + 0 comments
        Hours= {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve'}
        Minutes= {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'}
    
        if m==1:
            ans= f'{Minutes[m]} minute past {Hours[h]}'
        elif m==59:
            ans= f'{Minutes[60-m]} minute to {Hours[h+1]}'
        elif m<=30 and m>1:
            if m not in [15,30]:
                ans= f'{Minutes[m]} minutes past {Hours[h]}'
            else:
                ans=f'{Minutes[m]} past {Hours[h]}'
        elif m==0:
            ans=f'{Hours[h]} o\' clock'
        elif m>30:
            if m not in [45,30]:
                ans= f'{Minutes[60-m]} minutes to {Hours[h+1]}'
            else:
                ans=f'{Minutes[60-m]} to {Hours[h+1]}'
        return ans
    
  • + 0 comments

    I decided just to keep it simple and just use a list:

    def timeInWords(h: int, m: int):
        words = [
            "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",
        ]
        
        assert len(words) == 31, "There's one number missing or something"
        
        hour = words[h]
        next_hour = 0
        if m == 0:
            return hour + " " + words[m]
        
        connector: str = ' minute'
        magic_word = ' '
        
        if 1 <= m <= 30:
            magic_word += 'past '
        else:
            magic_word += 'to '
            m = 60 - m
            next_hour = 1
            
        if m > 1:
            connector += 's'
            
        hour = words[h+next_hour]
        minutes = words[m]
            
        # Handle 15 and 30
        if m in (15, 30):
            return minutes + magic_word + hour
        
        return minutes + connector + magic_word + hour
    
  • + 1 comment

    Hello What is wrong in my code ? I have 2 uses cases which fail (7 + 29 and 6 + 35) while the output seems correct to me...

    string num2word(int n) {    
        string s;
        int t = n/10;
        int u = n%10;
        
        switch (t) {
            case 0:
                if (u == 0) return "zero";
                break;
            case 1: 
                switch (u) {
                    case 0: s = "ten";       break;
                    case 1: s = "eleven";    break;
                    case 2: s = "twelve";    break;
                    case 3: s = "thirteen";  break;
                    case 4: s = "fourteen";  break;
                    case 5: s = "quarter";   break;
                    case 6: s = "sixteen";   break;
                    case 7: s = "seventeen"; break;
                    case 8: s = "eighteen";  break;
                    case 9: s = "nineteen";  break; 
                }
                return s;
                break;
            case 2: s = "tweenty";   break;
        }
        switch (u) {
            case  1: s += " one";   break;
            case  2: s += " two";   break;
            case  3: s += " three"; break;
            case  4: s += " four";  break;
            case  5: s += " five";  break;
            case  6: s += " six";   break;
            case  7: s += " seven"; break;
            case  8: s += " eight"; break;
            case  9: s += " nine";  break;
        }
        
        return ltrim(s);
    }
    
    string timeInWords(int h, int m) {
      string s;
      
     if (h == 0 && m == 0)  return "midnight";
     if (h == 12 && m == 0) return "noon";
     if (m == 30)           return "half past " + num2word(h);
     if (m == 15)           return "quarter past " + num2word(h);
     if (m == 45)           return "quarter to " + num2word(h+1);
     if (m == 0)            return num2word(h) + " o' clock";
     
      switch (m) {
          case 1:  s = " minute";   break;
          default: s = " minutes";  break;
      }
      
      if (m > 30)
        return num2word(60 - m) + s + " to " + num2word(h+1);
      return num2word(m) + s + " past " + num2word(h);  
    }