We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #17: Number to Words
  4. Discussions

Project Euler #17: Number to Words

Problem
Submissions
Leaderboard
Discussions

Sort 73 Discussions, By:

recency

Please Login in order to post a comment

  • mdasif9a
    7 months ago+ 0 comments

    Check Number not Greater 100 trillion

    ones = ["", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "]
    elevens = ["Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen "]
    tens = ["", "", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "]
    others = ["", "Thousand ", "Million ", "Billion ", "Trillion ", "Hundred "]
    
    def number_places(num, places=""):
        n1 = int(num[0])
        n2 = int(num[1])
        n3 = int(num[2])
        if n1 == 0:
            if n2 == 1:
                return f"{elevens[n3]}{places}"
            elif n2 == 0 and n3 == 0:
                return ""
            else:
                return f"{tens[n2]}{ones[n3]}{places}"
        elif n2 == 1:
            return f"{ones[n1]}{others[-1]}{elevens[n3]}{places}"
        else:
            return f"{ones[n1]}{others[-1]}{tens[n2]}{ones[n3]}{places}"
    
    
    t = int(input())
    for a0 in range(t):
        number = input()
    
        if int(number) > 0:
            length = len(number)
            if length%3 == 1:
                number = "00"+number
            elif length%3 == 2:
                number = "0"+number
            b = 0 #take starting position in others list
            c = -1 #iterate the others list
            result = "" #final result after loop
            length = len(number)
            for i in range(length):
                if (length-i)%3 == 0:
                    c += 1
                    if length == 15:
                        b = others[4-c]
                    elif length == 12:
                        b = others[3-c]
                    elif length == 9:
                        b = others[2-c]
                    elif length == 6:
                        b = others[1-c]
                    elif length == 3:
                        b = ""
                    num1 = number[i] + number[i+1] + number[i+2]
                    num_word = number_places(num=num1, places=b)
                    result += num_word
            print(result)
        else:
            print("Zero")
    
    0|
    Permalink
  • Bhavye_Goel
    1 year ago+ 0 comments

    Hello guys My test case #5 is failing cant't find the error pls help

    0|
    Permalink
  • s_mostafa_a
    2 years ago+ 0 comments

    solution in python 3

    one_digit = {'0': '', '1': 'One', '2': 'Two', '3': 'Three', '4': 'Four',
                 '5': 'Five', '6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine'}
    two_digit = {'10': 'Ten', '11': 'Eleven', '12': 'Twelve', '13': 'Thirteen', '14': 'Fourteen', '15': 'Fifteen', '16': 'Sixteen', '17': 'Seventeen',
                 '18': 'Eighteen', '19': 'Nineteen', '2': 'Twenty', '3': 'Thirty', '4': 'Forty', '5': 'Fifty', '6': 'Sixty', '7': 'Seventy', '8': 'Eighty', '9': 'Ninety'}
    arzesh = {0: '', 1: 'Thousand', 2: 'Million', 3: 'Billion', 4: "Trillion"}
    
    
    def give_three_digit(s):
        # s = string containing 3 digits
        ans = ''
        if s[0] != '0':
            ans += one_digit[s[0]]+' '+'Hundred'+' '
        if s[1] != '0' and s[1] != '1':
            ans += two_digit[s[1]]+' '
        if s[1] == '1':
            ans += two_digit[s[1:]]+' '
        if s[1] != '1' and s[2] != '0':
            ans += one_digit[s[2]]+' '
        if s[1] != '1' and s[2] == '0':
            ans += one_digit[s[2]]
        return(ans)
    
    
    def solve(n):
        if n == '0':
            print('Zero')
            return
        num = []
        ans = ''
        i = 0
        num.append(n[:len(n) % 3])
        i += (len(n) % 3)
        while i < len(n):
            num.append(n[i:i+3])
            i += 3
        if num[0] == '':
            del(num[0])
        num[0] = '0'*(3-len(num[0]))+num[0]
    
        N = len(num)
        for i in range(N):
            if num[i] == '000':
                continue
            ans += give_three_digit(num[i])
            ans += arzesh[N-i-1]+' '
        print(ans[:len(ans)-1])
    
    
    for _ in range(int(input())):
        solve(input())
    
    1|
    Permalink
  • thanhtan210197
    2 years ago+ 0 comments

    can someone please help whats wrong with below code ?

    t = int(input()) for i in range(t): n = str(input()) thousands = ["", "Thousand", "Million", "Billion", "Trillion"] units = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] teens = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]

    lisn = []
    for i in range(len(n), 0, -3):
        lisn += [n[i - 3:i]]
    if len(n) % 3 != 0:
        lisn[len(n) // 3] = n[:len(n) - (len(n) // 3) * 3]
    
    for i in range(len(lisn)):
        lisn[i] = str(int(lisn[i]))
    for i in range(len(lisn)):
        if len(lisn[i]) == 1:
            lisn[i] = units[int(lisn[i])]
        elif len(lisn[i]) == 2:
            if int(lisn[i]) < 20:
                lisn[i] = teens[int(lisn[i][1])]
            else:
                if int(lisn[i][1]) == 0:
                    lisn[i] = tens[int(lisn[i])] + ' ' + 'Zero'
                elif int(lisn[i][1]) != 0:
                    lisn[i] = tens[int(lisn[i][0])] + ' ' + units[int(lisn[i][1])]
        elif len(lisn[i]) == 3:
            if lisn[i][1] == '0':
                if lisn[i][2] == '0':
                    lisn[i] = units[int(lisn[i][0])] + ' ' + 'Hundred'
                elif lisn[i][2] != '0':
                    lisn[i] = units[int(lisn[i][0])] + ' ' + 'Hundred' + ' ' + units[int(lisn[i][2])]
            else:
                lis = units[int(lisn[i][0])] + ' ' + 'Hundred' + ' '
                str_ = int(lisn[i][1:])
                if str_ < 10:
                    lis += units[str_]
                if 10 <= str_ < 20:
                    lis += teens[int(str(str_)[1])]
                elif str_ > 20 and lisn[i][2] == 0:
                    lis += tens[str_]
                elif str_ > 20 and lisn[i][2] != 0:
                    lis += tens[int(str(str_)[0])] + ' ' + units[int(str(str_)[1])]
                lisn[i] = lis
    for i in range(len(lisn)):
        if lisn[i] != '':
            lisn[i] += " " + thousands[i]
    lisn.reverse()
    i = 0
    while i < len(lisn):
        if lisn[i] == '':
            lisn.remove(lisn[i])
            i += -1
        i += 1
    if len(lisn) == 0:
        print('Zero')
    else:
        print(*lisn)
    
    0|
    Permalink
  • DemosthenesLocke
    2 years ago+ 0 comments

    Java makes sense. English, however...

    -1|
    Permalink
Load more conversations

Need Help?


View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature