exam1.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import random
  2. def game_guesse():
  3. x = random.randint(1, 101)
  4. print(x)
  5. print("Добро пожаловать в числовую угадайку")
  6. number = int(input())
  7. trys = 1
  8. while number:
  9. if is_valid(number) == False:
  10. print('А может быть все-таки введем целое число от 1 до 100?')
  11. number = int(input())
  12. trys += 1
  13. continue
  14. if number > x:
  15. print('Ваше число больше загаданного, попробуйте еще разок')
  16. elif number < x:
  17. print('Ваше число меньше загаданного, попробуйте еще разок')
  18. else:
  19. print('Вы угадали, поздравляем!')
  20. print(f'Число попыток: {trys}')
  21. print('Спасибо, что играли в числовую угадайку. Еще увидимся...')
  22. break
  23. number = int(input())
  24. trys += 1
  25. def is_valid(num):
  26. return 1 <= num <= 100
  27. # game_guesse()
  28. # Шифр Цезаря. Очень плохая реализация.
  29. def caesar_cipher_1(data: str, rot: int):
  30. RU_LEN = 32
  31. chipher_date = ''
  32. a = ord('а')
  33. A = ord('А')
  34. low_case_ch = [chr(x) for x in range(a, a + RU_LEN)]
  35. low_case = [x for x in range(a, a + RU_LEN)]
  36. up_case_ch = [chr(x) for x in range(A, A + RU_LEN)]
  37. up_case = [x for x in range(A, A + RU_LEN)]
  38. print(''.join(low_case_ch))
  39. # print(up_case, len(up_case))
  40. if rot > 31:
  41. rot = rot%31
  42. for i in data:
  43. code = ord(i)
  44. if code in low_case:
  45. if rot > (low_case[-1] - code):
  46. chipher_date += chr(low_case[0] + rot - (low_case[-1] - code) - 1)
  47. else:
  48. chipher_date += chr(code + rot)
  49. elif code in up_case:
  50. if rot > (up_case[-1] - code):
  51. chipher_date += chr(up_case[0] + rot - (up_case[-1] - code) - 1)
  52. else:
  53. chipher_date += chr(code + rot)
  54. else:
  55. chipher_date += i
  56. return chipher_date
  57. # Шифр Цезаря. Нормальная реализация.
  58. def caesar_cipher_2(data: str, rot: int, lang='ru'):
  59. RU_LEN = 31
  60. EN_LEN = 25
  61. chipher_date = ''
  62. d = [chr(x) for x in range(ord('а'), ord('я') + 1)]
  63. ru_low_case = ''.join(d) * 2
  64. d = [chr(x) for x in range(ord('А'), ord('Я') + 1)]
  65. ru_up_case = ''.join(d) * 2
  66. d = [chr(x) for x in range(ord('a'), ord('z') + 1)]
  67. en_low_case = ''.join(d) * 2
  68. d = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
  69. en_up_case = ''.join(d) * 2
  70. if lang == 'ru':
  71. if rot > RU_LEN:
  72. rot = rot%RU_LEN
  73. for i in data:
  74. if i in ru_low_case:
  75. chipher_date += ru_low_case[ru_low_case.index(i) + rot]
  76. elif i in ru_up_case:
  77. chipher_date += ru_up_case[ru_up_case.index(i) + rot]
  78. else:
  79. chipher_date += i
  80. elif lang == 'en':
  81. if rot > EN_LEN:
  82. rot = rot%EN_LEN
  83. for i in data:
  84. if i in en_low_case:
  85. chipher_date += en_low_case[en_low_case.index(i) + rot]
  86. elif i in en_up_case:
  87. chipher_date += en_up_case[en_up_case.index(i) + rot]
  88. else:
  89. chipher_date += i
  90. else:
  91. return '-1'
  92. return chipher_date
  93. # Шифр Цезаря. Нормальная реализация.
  94. def caesar_recipher_2(data: str, rot: int, lang='ru'):
  95. RU_LEN = 31
  96. EN_LEN = 25
  97. chipher_date = ''
  98. d = [chr(x) for x in range(ord('а'), ord('я') + 1)]
  99. ru_low_case = ''.join(d) * 2
  100. d = [chr(x) for x in range(ord('А'), ord('Я') + 1)]
  101. ru_up_case = ''.join(d) * 2
  102. d = [chr(x) for x in range(ord('a'), ord('z') + 1)]
  103. en_low_case = ''.join(d) * 2
  104. d = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
  105. en_up_case = ''.join(d) * 2
  106. if lang == 'ru':
  107. if rot > RU_LEN:
  108. rot = rot%RU_LEN
  109. for i in data:
  110. if i in ru_low_case:
  111. chipher_date += ru_low_case[ru_low_case.index(i) - rot]
  112. elif i in ru_up_case:
  113. chipher_date += ru_up_case[ru_up_case.index(i) - rot]
  114. else:
  115. chipher_date += i
  116. elif lang == 'en':
  117. if rot > EN_LEN:
  118. rot = rot%EN_LEN
  119. for i in data:
  120. if i in en_low_case:
  121. chipher_date += en_low_case[en_low_case.index(i) - rot]
  122. elif i in en_up_case:
  123. chipher_date += en_up_case[en_up_case.index(i) - rot]
  124. else:
  125. chipher_date += i
  126. else:
  127. return '-1'
  128. return chipher_date
  129. # print(caesar_cipher_2("Блажен, кто верует, тепло ему на свете!", 10, 'ru'))
  130. # print(caesar_recipher_2("Лхкрпч, фьш мпъэпь, ьпщхш пцэ чк ымпьп!", 10, 'ru'))
  131. # print(caesar_cipher_2("To be, or not to be, that is the question!", 17, 'en'))
  132. def length(data: str):
  133. ret = 0
  134. for i in data:
  135. if i.isalpha():
  136. ret += 1
  137. return len([x for x in data if x.isalpha()])
  138. def caesar_cipher_test(data: str):
  139. l = data.split()
  140. ret = ''
  141. for sim in l:
  142. rot = length(sim)
  143. for i in sim:
  144. ret += caesar_cipher_2(i, rot, 'en')
  145. ret += ' '
  146. return ret
  147. # print(caesar_cipher_test("my name is Python!"))
  148. # print(caesar_cipher_test('Day, mice. "Year" is a mistake!'))
  149. def test(num):
  150. bin_num = bin(num) # 0b1111111
  151. oct_num = oct(num) # 0o177
  152. hex_num = hex(num) # 0x7f
  153. print(bin_num[2:]) # 1111111
  154. print(oct_num[2:]) # 177
  155. print(hex_num[2:]) # 7f
  156. # test(124)
  157. def my_range_gen(*args):
  158. if len(args) == 1:
  159. cnt = 0
  160. while cnt != args[0]:
  161. yield cnt
  162. cnt += 1
  163. elif len(args) == 2:
  164. cnt = args[0]
  165. while cnt < args[1]:
  166. yield cnt
  167. cnt += 1
  168. elif len(args) == 3:
  169. if args[2] > 0:
  170. cnt = args[0]
  171. while cnt < args[1]:
  172. yield cnt
  173. cnt += args[2]
  174. elif args[2] == 0:
  175. return
  176. else:
  177. cnt = args[0]
  178. while cnt > args[1]:
  179. yield cnt
  180. cnt += args[2]
  181. for i in my_range_gen(8):
  182. print(i)
  183. # for i in my_range_gen(8, 5, -1):
  184. # print(i)
  185. # for i in my_range_gen(10, 30, 3):
  186. # print(i)
  187. # for i in my_range_gen(30, 1, 0):
  188. # print(i)
  189. # for i in my_range_gen(30, 1, -5):
  190. # print(i)
  191. # for i in my_range_gen(5):
  192. # print(i)
  193. # s = my_range_gen(5)
  194. # print(next(s))
  195. # print(next(s))
  196. # print(next(s))
  197. # print(next(s))
  198. # print(next(s))
  199. # print(next(s))
  200. # def f(*args, **kwargs):
  201. # print(args, kwargs)
  202. # f(5, 4, 5, 6, 1, a = 1, b = 5, c = 6, name = 123)