Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- import re
- def numeral_to_int(list_of_numerals, numeral_dict):
- number = 0
- for s in list_of_numerals:
- number += numeral_dict[s]
- return number
- def miles_to_km(miles):
- return int(miles*1.60934)
- def km_word(kilos):
- if ((kilos <= 20 and kilos >= 10) or kilos % 10 >= 5 or kilos % 10 == 0):
- return 'километров'
- if (kilos % 10 == 1):
- return 'километр'
- else:
- return 'километра'
- def int_to_kilos(kilos, integer_dict):
- first, second, third = '','',''
- hundreds = kilos % 100
- if (kilos < 20 and kilos > 10):
- return (integer_dict[kilos])
- if (kilos // 100 != 0):
- first = integer_dict[(kilos // 100) * 100] + ' '
- kilos = kilos % 100
- if (kilos // 10 != 0):
- second = integer_dict[(kilos // 10) * 10] + ' '
- kilos = kilos % 10
- if (kilos != 0):
- third = integer_dict[kilos]
- return first + second + third
- integer_dict = {100: 'сто',
- 200: 'двести',
- 300: 'триста',
- 400: 'четыреста',
- 500: 'пятьсот',
- 600: 'шестьсот',
- 700: 'семьсот',
- 800: 'восемьсот',
- 900: 'девятьсот',
- 10: 'десять',
- 11: 'одиннадцать',
- 12: 'двенадцать',
- 13: 'тринадцать',
- 14: 'четырнадцать',
- 15: 'пятнадцать',
- 16: 'шестнадцать',
- 17: 'семнадцать',
- 18: 'восемнадцать',
- 19: 'девятнадцать',
- 20: 'двадцать',
- 30: 'тридцать',
- 40: 'сорок',
- 50: 'пятьдесят',
- 60: 'шестьдесят',
- 70: 'семьдесят',
- 80: 'восемьдесят',
- 90: 'девяносто',
- 1: 'один',
- 2: 'два',
- 3: 'три',
- 4:'четыре',
- 5: 'пять',
- 6: 'шесть',
- 7: 'семь',
- 8: 'восемь',
- 9: 'девять'}
- numeral_dict = dict()
- for key, value in integer_dict.items():
- numeral_dict[value] = key
- numeral_dict['одна'] = 1
- numeral_dict['две'] = 2
- del numeral_dict['один']
- del numeral_dict['два']
- text = input()
- mileset = {'миля', 'мили', 'миль'}
- m = re.search(r'(мил[яиь])([^a-z])?', text)
- start = m.start()
- end = m.end()
- beginning = text[:start]
- ending = text[end:]
- words_begin = beginning.split()
- numbers = []
- count = 0
- for word in reversed(words_begin):
- if word in numeral_dict:
- numbers.append(word)
- count += 1
- else:
- break
- miles = numeral_to_int(numbers, numeral_dict)
- kilos = miles_to_km(miles)
- if m.group(2) is None:
- punct = ''
- else:
- punct = m.group(2)
- ans = " ".join(words_begin[:-count]) + " " + int_to_kilos(kilos, integer_dict) + " " + km_word(kilos) + punct + text[end:]
- print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement