Advertisement
ruhan008

Q10

Apr 21st, 2024
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # a)
  2.  
  3. # dict1 = {1 : "One", 2 :"Two", 3 : "Three", 4 : "Four", 5 : "Five", 6 : "Six", 7 : "Seven", 8 : "Eight", 9 : "Nine"
  4. #          , 10 : "Ten", 11 : "Eleven" , 12 : "Twelve" , 13 : "Thirteen", 14 : "Fourteen", 15 : "Fifteen",
  5. #            16 : "Sixteen", 17 : "Seventeen" , 18 : "Eighteen", 19 : "Nineteen",
  6. #            20 : "Twenty", 30 : "Thirty", 40 : "Fourty", 50 : "Fifty", 60 : "Sixty", 70 : "Seventy", 80 : "Eighty",
  7. #            90 : "Ninety"}
  8.  
  9. # for i in range(2,10):
  10. #     for j in range(1,10):
  11. #         num = (i*10)+j
  12. #         name = dict1[i*10] + ' ' + dict1[j]
  13. #         dict1.update({num : name})
  14.  
  15. # def hundredPart(s):
  16. #     n = len(s)
  17. #     if n <= 2:
  18. #         return dict1[int(s)]
  19. #     else:
  20. #         ans = ""
  21. #         if s[0] != '0':
  22. #             ans = ans + dict1[int(s[0])] + " Hundred "
  23. #         if int(s[1:3]) != 0:
  24. #             ans = ans + dict1[int(s[1:3])]
  25. #         return ans
  26.  
  27. # def thousandPart(s):
  28. #     if int(s) != 0:
  29. #         return hundredPart(s) + " Thousand"
  30. #     return ""
  31.  
  32. # def millionPart(s):
  33. #     if int(s) != 0:
  34. #         return hundredPart(s) + " Million"
  35. #     return ""
  36.  
  37. # def numberName(s):
  38. #     n = len(s)
  39.  
  40. #     if int(s) == 0:
  41. #         return "Zero"
  42.    
  43. #     if n<=3:
  44. #         return hundredPart(s)
  45. #     elif n<=6:
  46. #         return thousandPart(s[0:n-3]) + ' ' + hundredPart(s[n-3:n])
  47. #     elif n<=9:
  48. #         return millionPart(s[0:n-6]) + ' ' + thousandPart(s[n-6:n-3]) + ' ' + hundredPart(s[n-3:n])
  49.    
  50. #     return "Out of range!!!"
  51.        
  52. # num = input("Enter a number in the range[0,999999999]: ")
  53.    
  54. # print(numberName(num))
  55.  
  56. dict1 = {"Zero": 0,"One" : 1, "Two" : 2, "Three" : 3,"Four" : 4,"Five" : 5,"Six" : 6,"Seven" : 7,"Eight" : 8,"Nine" : 9,
  57.           "Ten" : 10,"Eleven" : 11,"Twelve" : 12,"Thirteen" : 13,"Fourteen" : 14,"Fifteen" : 15,"Sixteen" : 16,
  58.           "Seventeen" : 17, "Eighteen" : 18,"Nineteen" : 19,"Twentey" : 20,"Thirty" : 30,"Fourty" : 40,"Fifty" : 50,
  59.           "Sixty" : 60,"Seventy" : 70,"Eighty" : 80,"Ninety" : 90}
  60.  
  61. dict2 = {"Hundred" : 100,"Thousand" : 1000, "Million" : 1000000}
  62.  
  63. def wordNumber(s):
  64.     words = s.split();
  65.     num = 0
  66.     ans = 0
  67.     for word in words:
  68.         if word in dict1:
  69.             num = num + dict1[word]
  70.         elif word in dict2:
  71.             ans = ans + (num * dict2[word])
  72.             num = 0
  73.         else:
  74.             return None
  75.    
  76.     ans = ans + num
  77.     return ans
  78.  
  79. s = input("Enter a string: ")
  80. print(wordNumber(s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement