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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #17: Number to Words
  4. Discussions

Project Euler #17: Number to Words

Contest ends in
Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • s_mostafa_a
    3 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy