Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. import string
  2. import re
  3.  
  4. def numeral_to_int(list_of_numerals, numeral_dict):
  5.     number = 0
  6.     for s in list_of_numerals:
  7.         number += numeral_dict[s]
  8.     return number
  9.  
  10. def miles_to_km(miles):
  11.     return int(miles*1.60934)
  12.  
  13. def km_word(kilos):
  14.     if ((kilos <= 20 and kilos >= 10) or kilos % 10 >= 5 or kilos % 10 == 0):
  15.         return 'километров'
  16.     if (kilos % 10 == 1):
  17.         return 'километр'
  18.     else:
  19.         return 'километра'
  20.    
  21. def int_to_kilos(kilos, integer_dict):
  22.     first, second, third = '','',''
  23.     hundreds = kilos % 100
  24.     if (kilos < 20 and kilos > 10):
  25.         return (integer_dict[kilos])
  26.     if (kilos // 100 != 0):
  27.         first = integer_dict[(kilos // 100) * 100] + ' '
  28.         kilos = kilos % 100
  29.     if (kilos // 10 != 0):
  30.         second = integer_dict[(kilos // 10) * 10] + ' '
  31.         kilos = kilos % 10
  32.     if (kilos != 0):
  33.         third = integer_dict[kilos]
  34.     return first + second + third
  35.  
  36.  
  37. integer_dict = {100: 'сто',
  38. 200: 'двести',
  39. 300: 'триста',
  40. 400: 'четыреста',
  41. 500: 'пятьсот',
  42. 600: 'шестьсот',
  43. 700: 'семьсот',
  44. 800: 'восемьсот',
  45. 900: 'девятьсот',
  46. 10: 'десять',
  47. 11: 'одиннадцать',
  48. 12: 'двенадцать',
  49. 13: 'тринадцать',
  50. 14: 'четырнадцать',
  51. 15: 'пятнадцать',
  52. 16: 'шестнадцать',
  53. 17: 'семнадцать',
  54. 18: 'восемнадцать',
  55. 19: 'девятнадцать',
  56. 20: 'двадцать',
  57. 30: 'тридцать',
  58. 40: 'сорок',
  59. 50: 'пятьдесят',
  60. 60: 'шестьдесят',
  61. 70: 'семьдесят',
  62. 80: 'восемьдесят',
  63. 90: 'девяносто',
  64. 1: 'один',
  65. 2: 'два',
  66. 3: 'три',
  67. 4:'четыре',
  68. 5: 'пять',
  69. 6: 'шесть',
  70. 7: 'семь',
  71. 8: 'восемь',
  72. 9: 'девять'}
  73.  
  74. numeral_dict = dict()
  75. for key, value in integer_dict.items():
  76.     numeral_dict[value] = key
  77. numeral_dict['одна'] = 1
  78. numeral_dict['две'] = 2
  79. del numeral_dict['один']
  80. del numeral_dict['два']
  81.  
  82. text = input()
  83. mileset = {'миля', 'мили', 'миль'}
  84.  
  85. m = re.search(r'(мил[яиь])([^a-z])?', text)
  86.  
  87. start = m.start()
  88. end = m.end()
  89.  
  90. beginning = text[:start]
  91. ending = text[end:]
  92.  
  93. words_begin = beginning.split()
  94.  
  95. numbers = []
  96. count = 0
  97. for word in reversed(words_begin):
  98.     if word in numeral_dict:
  99.         numbers.append(word)
  100.         count += 1
  101.     else:
  102.         break
  103.  
  104. miles = numeral_to_int(numbers, numeral_dict)
  105. kilos = miles_to_km(miles)
  106. if m.group(2) is None:
  107.     punct = ''
  108. else:
  109.     punct = m.group(2)
  110. ans = " ".join(words_begin[:-count]) + " " + int_to_kilos(kilos, integer_dict) + " " + km_word(kilos) + punct + text[end:]
  111. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement