misc2.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Сортировка строки. В каждом слове содержится число.
  2. # "is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
  3. # "4of Fo1r pe6ople g3ood th5e the2" --> "Fo1r the2 g3ood 4of th5e pe6ople"
  4. from operator import itemgetter
  5. from numpy import block
  6. def order(sentence):
  7. my_list = sentence.split(' ')
  8. new_list = []
  9. print(my_list)
  10. for i in range(len(my_list)):
  11. for j in range(len(my_list)):
  12. if my_list[j].rfind(str(i+1)) != -1:
  13. new_list.append(my_list[j])
  14. return (' '.join(new_list))
  15. #
  16. # def order(sentence):
  17. # return " ".join(sorted(sentence.split(), key=lambda x: int(filter(str.isdigit, x))))
  18. #
  19. # def order(words):
  20. # return ' '.join(sorted(words.split(), key=lambda w:sorted(w)))
  21. # print(order("is2 Thi1s T4est 3a"))
  22. # ------------------------------------------------------------------------------- #
  23. #
  24. def find_even_index(arr):
  25. sum_left = 0
  26. sum_right = 0
  27. sum = []
  28. for i in range(len(arr)):
  29. for j in range(i):
  30. sum_left += arr[j]
  31. for k in range(len(arr) - 1,i ,-1):
  32. sum_right += arr[k]
  33. if sum_left == sum_right:
  34. sum.append(i)
  35. # print(f"Sum let: {sum_left} and sun right: {sum_right}")
  36. sum_left = sum_right = 0
  37. if len(sum) == 0:
  38. return -1
  39. else:
  40. sum.sort()
  41. return(sum[0])
  42. '''
  43. # Лучшее решение
  44. def find_even_index(arr):
  45. for i in range(len(arr)):
  46. if sum(arr[:i]) == sum(arr[i+1:]):
  47. return i
  48. return -1
  49. '''
  50. # print(find_even_index([1,2,3,4,3,2,1]))
  51. # print(find_even_index([1,2,3,4,5,6]))
  52. # ------------------------------------------------------------------------------- #
  53. # Задачка на работу с boxplot
  54. # Пока не завершил.
  55. class StatisticalSummary(object):
  56. BOXPLOT = 0
  57. BOX_AND_WHISKER = 1
  58. BOX_AND_DECILE_WHISKER = 2
  59. TUKEY_BOX_AND_WHISKER = 3
  60. def __init__(self, seq):
  61. self.seq = seq
  62. self.le = 0
  63. self.eu = 0
  64. self.median = 0
  65. self.q1 = 0
  66. self.q3 = 0
  67. def boxplot(self, plot=BOXPLOT, precision=None):
  68. result = []
  69. temp_list = []
  70. number = 0
  71. if type(self.seq[0]) == tuple:
  72. print("Пока не обрабатываем картежи")
  73. else:
  74. temp_list = self.clear_list(self.seq)
  75. temp_list.sort()
  76. self.calculation(temp_list)
  77. result.append(('Sample', number))
  78. return result
  79. def calculation(self, data):
  80. self.le = min(data)
  81. self.eu = max(data)
  82. self.median = self.calculate_median(data)
  83. self.calculate_quartile(data)
  84. def clear_list(self, data):
  85. cleared_list = []
  86. for i in data:
  87. if type(i) == int or type(i) == float:
  88. cleared_list.append(i)
  89. return cleared_list
  90. def calculate_median(self, data):
  91. data_len = len(data)
  92. if data_len%2 == 0:
  93. return ((data[int(data_len/2) - 1] + data[int(data_len/2)]))/2
  94. else:
  95. return data[data_len//2]
  96. def calculate_quartile(self, data):
  97. data_len = len(data)
  98. if data_len%4 == 0:
  99. index = int(data_len/4)
  100. q1 = (data[index] - data[index - 1])*0.25 + data[index - 1]
  101. index = int(data_len/4*3)
  102. q3 = (data[index] - data[index - 1])*0.75 + data[index - 1]
  103. print(data)
  104. print(q1)
  105. print(q3)
  106. return q1, q3
  107. # data = list(range(1, 33)) + list(range(12, 21)) + list(range(12, 21)) + list(range(12, 21)) + [16]
  108. # d1 = list(range(1, 33)) + list(range(12, 21)) + list(range(12, 21)) + list(range(12, 21)) + [16]
  109. # print(data)
  110. # d1 = range(1, 33) + range(12, 21) + range(12, 21) + range(12, 21) + [16]
  111. # d2 = [d - 2 for d in d1[:len(data)//2]] + [d + 2 for d in d1[len(data)//2:]]
  112. # data2 = [("A", n) for n in d1] + [("B", n) for n in d2]
  113. # print(data2)
  114. # print(type(data2))
  115. # print(d2)
  116. # test_list = [1, 2, 5, 2, 3, 'asdfasd', 2, 3, 12, 45, 3, 6, 25, 54, 32, 12, 14, 2]
  117. # summary1 = StatisticalSummary(test_list)
  118. # summary1.boxplot()
  119. # ------------------------------------------------------------------------------- #
  120. def is_valid_walk(walk):
  121. horizontl = 0
  122. vertical = 0
  123. for i in walk:
  124. if i == 'n':
  125. vertical += 1
  126. elif i == 's':
  127. vertical -= 1
  128. elif i == 'w':
  129. horizontl -= 1
  130. elif i == 'e':
  131. horizontl += 1
  132. return vertical == 0 and horizontl == 0 and len(walk) == 10
  133. # Решение через метод count
  134. # def isValidWalk(walk):
  135. # return len(walk) == 10 and walk.count('n') == walk.count('s') and walk.count('e') == walk.count('w')
  136. # print(is_valid_walk(['w','e','w','e','w','e','w','e','w','e','w','e']))
  137. # ------------------------------------------------------------------------------- #
  138. # На входе одно число из массив четное или нечетно. Нужно его найти и вернуть.
  139. def find_outlier(integers):
  140. types = []
  141. def is_even(value):
  142. return value%2 == 0
  143. for i in integers:
  144. types.append(is_even(i))
  145. if types.count(True) > types.count(False):
  146. return integers[types.index(False)]
  147. else:
  148. return integers[types.index(True)]
  149. # Более короткое решение
  150. # def find_outlier(int):
  151. # odds = [x for x in int if x%2!=0]
  152. # evens= [x for x in int if x%2==0]
  153. # return odds[0] if len(odds)<len(evens) else evens[0]
  154. # print(find_outlier([2, 4, 0, 100, 4, 11, 2602, 36]))
  155. # ------------------------------------------------------------------------------- #
  156. # В переданной строке должно быть только 4 или 6 цифр
  157. def validate_pin(pin):
  158. return len(pin) in (4, 6) and pin.isdigit()
  159. # print(validate_pin('-234'))