docstring_1.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from typing import List, Dict, Tuple, Set
  2. from typing import Union
  3. from typing import Optional
  4. '''
  5. Docstring - строка документирования.
  6. Вызывается:
  7. help(abs)
  8. abs.__doc__
  9. В docstring попадет только первая строка из начала функции.
  10. Слабая и сильная (строгая) типизация.
  11. В языках со строгой типизация (Python) с объектами определенных
  12. типов можно производить только ограниченный набор действий.
  13. В Python динамическая строгая типизация.
  14. Аннотация типов в Python
  15. foo: float
  16. foo: int = 100
  17. В функции содержится атрибут __annotations__ (содержит словарь аннотаций)
  18. '''
  19. def get_even(lst) -> list:
  20. 'Функция возвращает список из четных чисел списка lst'
  21. even_lst = []
  22. for elem in lst:
  23. if elem % 2 == 0:
  24. even_lst.append(elem)
  25. return even_lst
  26. def func_1():
  27. numbers: List[int] = [1, 2, 3]
  28. languages: Dict = {}
  29. temperature: Tuple = (1, 2, 3)
  30. letters: Set[str] = set('qwerty')
  31. param_1: Union[int, float, bool]
  32. param_2: int | float | str
  33. def get_first_repeated_word(words: List[str]) -> Optional[str]:
  34. 'Находит первый дубль в списке'
  35. foo = set()
  36. for w in words:
  37. if w in foo:
  38. return w
  39. else:
  40. foo.add(w)
  41. return None
  42. def rotate(lst: list[int | float], shift: int = 1) -> list[int | float] :
  43. 'Функция выполняет циклический сдвиг списка на shift позиций вправо(shift>0) или влево(shift<0)'
  44. shifted_list = []
  45. if abs(shift) <= len(lst) or shift%len(lst) == 0:
  46. foo = shift
  47. else:
  48. if shift < 0:
  49. foo = abs(shift)%len(lst)
  50. foo *= -1
  51. else:
  52. foo = shift%len(lst)
  53. if foo < 0:
  54. shifted_list.extend(lst[-foo:])
  55. shifted_list.extend(lst[:-foo])
  56. elif foo > 0:
  57. shifted_list.extend(lst[-foo:])
  58. shifted_list.extend(lst[:-foo])
  59. return shifted_list
  60. def ref_rotate(tpl: tuple[int | float, ...], shift: int = 1) -> tuple[int | float, ...] :
  61. 'Функция выполняет циклический сдвиг кортежа на shift позиций вправо (shift>0) или влево (shift<0)'
  62. shifted_tuple = tuple()
  63. if abs(shift) <= len(tpl) or shift%len(tpl) == 0:
  64. foo = shift
  65. else:
  66. if shift < 0:
  67. foo = abs(shift)%len(tpl)
  68. foo *= -1
  69. else:
  70. foo = shift%len(tpl)
  71. if foo < 0:
  72. shifted_tuple = tpl[-foo:] + tpl[:-foo]
  73. elif foo > 0:
  74. shifted_tuple = tpl[-foo:] + tpl[:-foo]
  75. return shifted_tuple
  76. def rotate_letter(letter: str, shift: int) -> str :
  77. 'Функция сдвигает символ letter на shift позиций'
  78. code = ord(letter) - 97
  79. if shift >= 0:
  80. shift = shift - 26*(shift//26)
  81. if (code + 97 + shift) >= 122:
  82. return chr(code + 97 + shift - 26)
  83. else:
  84. return chr(code + 97 + shift)
  85. else:
  86. shift *= -1
  87. shift = shift - 26*(shift//26)
  88. if (code + 97 - shift) < 97:
  89. return chr(code + 97 + 26 - shift)
  90. else:
  91. return chr(code + 97 - shift)
  92. def caesar_cipher(phrase: str, key: int) -> str:
  93. 'Шифр Цезаря'
  94. my_str = ""
  95. for i in phrase:
  96. if i.isalpha():
  97. my_str += rotate_letter(i, key)
  98. else :
  99. my_str += i
  100. return my_str
  101. def main():
  102. # print(get_even.__doc__)
  103. # print(get_first_repeated_word.__doc__)
  104. # print(get_first_repeated_word.__annotations__)
  105. # print(get_first_repeated_word(['ab', 'ca', 'bc', 'ca', 'ab', 'bc']))
  106. # print(rotate([1, 2, 3, 4, 5, 6], 2))
  107. # print(rotate([1, 2, 3, 4, 5, 6], -10))
  108. # print(rotate([1, 2, 3, 4, 5, 6, 7], 21))
  109. # print(rotate([1, 2, 3, 4, 5, 6], -3))
  110. # assert rotate([1, 2, 3, 4, 5, 6], 2) == [5, 6, 1, 2, 3, 4]
  111. # assert rotate([1, 2, 3, 4, 5, 6], -3) == [4, 5, 6, 1, 2, 3]
  112. # assert rotate([1, 2, 3, 4, 5, 6], -10) == [5, 6, 1, 2, 3, 4]
  113. # assert rotate([1, 2, 3, 4, 5, 6, 7], 21) == [1, 2, 3, 4, 5, 6, 7]
  114. # print(rotate_letter('a', 3))
  115. # print(rotate_letter('d', -2))
  116. # print(rotate_letter('w', -26))
  117. print(caesar_cipher('leave out all the rest', -4))
  118. print('Good')
  119. if __name__ == '__main__':
  120. main()