vesso8

Numbers_converter

Jan 14th, 2022
1,119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. text = 'oneoneminustwotwo'
  2. converter = {
  3.     'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9,
  4.     'plus':'+', 'minus':'-'
  5. }
  6.  
  7. def words_split(num):
  8.     numbers_given = []
  9.     n = ''
  10.     for i in range(len(num)):
  11.         n += num[i]
  12.         if n in converter:
  13.             numbers_given.append(n)
  14.             n = ''
  15.     return numbers_given
  16.  
  17. def nums_convert(storage, converter_nums):
  18.     n = ''
  19.     for i in range(len(storage)):
  20.         n += str(converter_nums[storage[i]])
  21.     return n
  22.  
  23. def numbers_splitting_by_sides(number):
  24.     result = ''
  25.     if "+" in number:
  26.         num = number.split("+")
  27.         left_side = int(num[0])
  28.         right_side = int(num[1])
  29.         result = str(left_side + right_side)
  30.     elif "-" in number:
  31.         num = number.split("-")
  32.         left_side = int(num[0])
  33.         right_side = int(num[1])
  34.         result = str(left_side - right_side)
  35.     return result
  36.  
  37. def get_key(val):
  38.     for key, value in converter.items():
  39.         if val == value:
  40.             return key
  41.     return "key doesn't exist"
  42.  
  43. def calculating_number(n):
  44.     final_result = ''
  45.     for el in n:
  46.         if el.startswith("-"):
  47.             final_result += "negative"
  48.             continue
  49.         final_result += get_key(int(el))
  50.     return final_result
  51.  
  52. numbers_storage = words_split(text)
  53. converted_numbers = nums_convert(numbers_storage, converter)
  54. splitted_numbers= numbers_splitting_by_sides(converted_numbers)
  55. result = calculating_number(splitted_numbers)
  56. print(result)
  57.  
RAW Paste Data