func.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # def sum_num(s):
  2. # summa = 0
  3. # for i in s:
  4. # if i.isdigit():
  5. # summa += int(i)
  6. # print(summa)
  7. # sum_num('asd12312asdf')
  8. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. # def get_body_mass_index(weight, height):
  10. # index = weight/((height*0.01)**2)
  11. # if index < 18.5:
  12. # print('Недостаточная масса тела')
  13. # elif 18.5 <= index <= 25.0:
  14. # print('Норма')
  15. # else:
  16. # print('Избыточная масса тела')
  17. # get_body_mass_index(70, 170)
  18. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. # def check_password(psw):
  20. # f1 = False
  21. # digit_cnt = 0
  22. # cap = False
  23. # sim = False
  24. # for i in psw:
  25. # if i.isdigit():
  26. # digit_cnt += 1
  27. # if i.istitle():
  28. # cap = True
  29. # if i in "!@#$%":
  30. # sim = True
  31. # if (digit_cnt >= 3) and cap == True and sim == True and len(psw) >= 10:
  32. # print('Perfect password')
  33. # else:
  34. # print('Easy peasy')
  35. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. # def count_letters(s):
  37. # cap_cnt = 0
  38. # uncap_cnt = 0
  39. # for i in s:
  40. # if i.isalpha():
  41. # if i.istitle():
  42. # cap_cnt += 1
  43. # else:
  44. # uncap_cnt += 1
  45. # print('Количество заглавных символов:', cap_cnt)
  46. # print('Количество строчных символов:', uncap_cnt)
  47. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. # def foo():
  49. # print('function foo')
  50. # a = foo()
  51. # print(a)
  52. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  53. # def find_duplicate(lst):
  54. # my_list = []
  55. # new_l = []
  56. # for i in lst:
  57. # if i not in my_list:
  58. # my_list.append(i)
  59. # for i in my_list:
  60. # if lst.count(i) > 1:
  61. # new_l.append(i)
  62. # return new_l
  63. # print(find_duplicate([1, 1, 1, 1, 1, 2, 2, 2, 2]))
  64. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. def first_unique_char(s):
  66. s.lower()
  67. for i in range(len(s)):
  68. if s.count(s[i]) == 1:
  69. return i
  70. else:
  71. return -1
  72. # print(first_unique_char('aasssddddddddq'))
  73. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. def format_name_list(names: list):
  75. length = len(names)
  76. s = ""
  77. if length == 0:
  78. return ""
  79. elif length == 1:
  80. return names[0].get("name")
  81. elif length == 2:
  82. return names[0].get("name") + ' и ' + names[1].get("name")
  83. else:
  84. for i in range(len(names) - 1):
  85. s += names[i].get("name") + ', '
  86. return s[:-2] + " и " + names[-1].get("name")
  87. # print(format_name_list([{'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'},
  88. # {'name': 'Homer'}, {'name': 'Marge'}]))
  89. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. def get_domain_name(url : str):
  91. my_list = []
  92. if url.find("//") != -1:
  93. my_list = url.split("//")
  94. if my_list[1].find("www") != -1:
  95. my_list = my_list[1].split(".")
  96. return my_list[1]
  97. else:
  98. my_list = my_list[1].split(".")
  99. return my_list[0]
  100. else:
  101. my_list = url.split(".")
  102. return my_list[1]
  103. # assert get_domain_name("http://google.com") == "google"
  104. # assert get_domain_name("http://google.co.jp") == "google"
  105. # assert get_domain_name("www.xakep.ru") == "xakep"
  106. # assert get_domain_name("https://youtube.com") == "youtube"
  107. # assert get_domain_name("http://github.com/carbonfive/raygun") =='github'
  108. # assert get_domain_name("http://www.zombie-bites.com") == 'zombie-bites'
  109. # assert get_domain_name("https://www.siemens.com") == 'siemens'
  110. # assert get_domain_name("https://www.whatsapp.com") == 'whatsapp'
  111. # assert get_domain_name("https://www.mywww.com") == 'mywww'
  112. # print('Проверки пройдены')
  113. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. def factorial(n):
  115. fact = 1
  116. for num in range(2, n + 1):
  117. fact *= num
  118. return fact
  119. def trailing_zeros(n):
  120. s = str(factorial(n))[::-1]
  121. l = []
  122. for i in range(len(s)):
  123. if s[i] != '0':
  124. break
  125. else:
  126. l.append(s[i])
  127. return len(l)
  128. # assert trailing_zeros(0) == 0
  129. # assert trailing_zeros(6) == 1
  130. # assert trailing_zeros(30) == 7
  131. # assert trailing_zeros(12) == 2
  132. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. def count_AGTC(dna):
  134. return dna.count('A'), dna.count('G'), dna.count('T'), dna.count('C')
  135. # assert count_AGTC('AGGTC') == (1, 2, 1, 1)
  136. # assert count_AGTC('AAAATTT') == (4, 0, 3, 0)
  137. # assert count_AGTC('AGTTTTT') == (1, 1, 5, 0)
  138. # assert count_AGTC('CCT') == (0, 0, 1, 2)
  139. # print('Проверки пройдены')
  140. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  141. def foo():
  142. "Эта функция делает что-то"
  143. pass
  144. # print(foo.__doc__)
  145. class Model:
  146. """
  147. This is class model
  148. """
  149. pass
  150. # help(Model)
  151. # print(Model.__doc__)
  152. def get_even(lst, number):
  153. """_summary_
  154. Args:
  155. lst (_type_): _description_
  156. number (_type_): _description_
  157. """
  158. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. # first : int = 100
  160. def add_numbers(a: int, b: int | float | str) -> int:
  161. return a + b
  162. # print(add_numbers.__annotations__)
  163. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  164. from typing import List, Dict, Tuple, Optional, Any, Union
  165. def list_upper(lst: List[str]):
  166. for elem in lst:
  167. print(elem.upper())
  168. # e: Any = 12
  169. # e = "sdfsdf"
  170. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171. def first_repeated_word(s: str):
  172. """Находит первый дубль в строке
  173. Args:
  174. s (str): строка
  175. Returns:
  176. str: первое повторяющееся слово
  177. """
  178. l = s.split()
  179. d_index = dict()
  180. for i in range(len(l)):
  181. if (l.count(l[i]) > 1) and l[i] not in d_index.values():
  182. d_index.update({l.index(l[i], i+1, len(l)) : l[i]})
  183. if len(d_index) == 0:
  184. return None
  185. m = len(l)
  186. if m == 0:
  187. return None
  188. for key,value in d_index.items():
  189. if key < m:
  190. m = key
  191. return d_index.get(m)
  192. assert first_repeated_word("ab ca bc ab") == "ab"
  193. assert first_repeated_word("ab ca bc Ab cA aB bc") == "bc"
  194. assert first_repeated_word("ab ca bc ca ab bc") == "ca"
  195. assert first_repeated_word("hello hi hello") == "hello"
  196. print(first_repeated_word.__doc__)