-
Notifications
You must be signed in to change notification settings - Fork 173
lesson_1 #1259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
lesson_1 #1259
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,9 @@ def check_1(lst_obj): | |
| Алгоритм 3: | ||
| Создать множество из списка | ||
|
|
||
| Сложность: !!!. | ||
| Сложность: O(1). | ||
| """ | ||
| lst_to_set = set(lst_obj) # !!! | ||
| lst_to_set = set(lst_obj) # O(1) | ||
| return lst_to_set | ||
|
|
||
|
|
||
|
|
@@ -41,12 +41,12 @@ def check_2(lst_obj): | |
| что такой элемент отстутствует | ||
| в оставшихся справа элементах | ||
|
|
||
| Сложность: !!!. | ||
| Сложность: O(n^2). | ||
| """ | ||
| for j in range(len(lst_obj)): # !!! | ||
| if lst_obj[j] in lst_obj[j+1:]: # !!! | ||
| return False # !!! | ||
| return True # !!! | ||
| for j in range(len(lst_obj)): # O(n^2) | ||
| if lst_obj[j] in lst_obj[j+1:]: # O(n) | ||
| return False # O(1) | ||
| return True # O(1) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
|
|
||
|
|
||
| ############################################################################################# | ||
|
|
@@ -57,14 +57,14 @@ def check_3(lst_obj): | |
| Вначале выполним для списка сортировку, далее, сравниваем элементы попарно | ||
| Если присутствуют дубли, они будут находиться рядом. | ||
|
|
||
| Сложность: !!! | ||
| Сложность: O(nLog(N)) | ||
| """ | ||
| lst_copy = list(lst_obj) # !!! | ||
| lst_copy.sort() # !!! | ||
| for i in range(len(lst_obj) - 1): # !!! | ||
| if lst_copy[i] == lst_copy[i+1]: # !!! | ||
| return False # !!! | ||
| return True # !!! | ||
| lst_copy = list(lst_obj) # O(n) | ||
| lst_copy.sort() # O(n*log(n) | ||
| for i in range(len(lst_obj) - 1): # O(n) | ||
| if lst_copy[i] == lst_copy[i+1]: # O(1) | ||
| return False # O(1) | ||
| return True # O(1) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
|
|
||
| ############################################################################################# | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,33 @@ | |
| Алгоритмизатор должен развивать мышление, а это прежде всего практика. | ||
| А без столкновения со сложностями его не развить. | ||
| """ | ||
|
|
||
|
|
||
| from random import randint | ||
| numbers = [randint(1,101) for _ in range(1,10)] | ||
| print(numbers) | ||
|
|
||
| #первый алгоритм | ||
| #вариант O(n) | ||
| print('проверочный вариант', min(numbers)) #автоматизированный вариант O(n) | ||
|
|
||
| def min_numb(numb): | ||
| res = numb[8] #можно указать любое число, так как он перебирает весь список | ||
| for i in numb: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ошибки стиля |
||
| if res>i: | ||
| res = i | ||
| return res | ||
| print('вариант O(n):', min_numb(numbers)) | ||
|
|
||
| #второе алгоритм | ||
| #вариант O(n^2) | ||
|
|
||
| def min_numb2(number:list): | ||
| min_numb = number[0] | ||
| for i in number: | ||
| for y in number: | ||
| if i < y and i < min_numb: | ||
| min_numb = i | ||
| return min_numb | ||
|
|
||
| print('вариант O(n^2):', min_numb2(numbers)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,54 @@ | |
| Реализуйте поиск трех компаний с наибольшей годовой прибылью. | ||
| Выведите результат. | ||
| """ | ||
|
|
||
|
|
||
| company = { | ||
| 'ПАО НЕФТЯНАЯ КОМПАНИЯ РОСНЕФТЬ': 155.8, | ||
| 'ПАО ГАЗПРОМ НЕФТЬ': 162.6, | ||
| 'ПАО ГОРНО-МЕТАЛЛУРГИЧЕСКАЯ КОМПАНИЯ НОРИЛЬСКИЙ НИКЕЛЬ': 300.1, | ||
| 'ПАО СЕВЕРСТАЛЬ': 114.9, | ||
| 'ПАО НОВОЛИПЕЦКИЙ МЕТАЛЛУРГИЧЕСКИЙ КОМБИНАТ': 61.1 | ||
| } | ||
|
|
||
| def sort_1(list_input): # вариант 1 : O(n) | ||
| input_max = {} | ||
| list_d = dict(list_input) | ||
| for i in range(3): | ||
| maximum = max(list_d.items(), key=lambda k_v: k_v[1]) | ||
| del list_d[maximum[0]] | ||
| input_max[maximum[0]] = maximum[1] | ||
| return input_max | ||
|
|
||
| print('\nвариант 1\n') | ||
| print(sort_1(company)) | ||
|
|
||
|
|
||
|
|
||
| def sort_2(random_list): # вариант 2: O(n ^ 2) | ||
| for i in range(len(random_list)): | ||
| low_value = i | ||
| for j in range(i + 1, len(random_list)): | ||
| if random_list[j][1] > random_list[low_value][1]: | ||
| low_value = j | ||
| random_list[i], random_list[low_value] = random_list[low_value], random_list[i] | ||
| return random_list[0:3] | ||
|
|
||
| print('\nвариант 2\n') | ||
|
|
||
| list_dict = list(company.items()) | ||
| for i in sort_2(list_dict): | ||
|
|
||
| print(i[0], ':', i[1], ' ') | ||
|
|
||
|
|
||
| print('\nвариант 3\n') | ||
|
|
||
| list_dict = list(company.items()) # Вариант 3: O(n log n) | ||
| list_dict.sort(key=lambda i: i[1], reverse=True) | ||
| for i in range(3): | ||
| print(list_dict[i][0], ':', list_dict[i][1] ) | ||
|
|
||
| #Быстрее вариант 1 : O(n) - проще и быстрее в данном примере (объёме). | ||
| #Вариант 2 и уступает O(n) на небольших объёмах и при увеличении вводимых данных уступает варианту 3 | ||
| #Вариант 3 O(n log n) масштабируемый и при бОльших объёмах будет одинакого оперативно выдавать результат | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,51 @@ | |
| Вам нужно доработать программу так, чтобы она могла выполнить проверку на палиндром | ||
| и в таких строках (включающих пробелы) | ||
| """ | ||
| # палиндром | ||
|
|
||
| class DequeClass: | ||
| def __init__(self): | ||
| self.elems = [] | ||
|
|
||
| def is_empty(self): | ||
| return self.elems == [] | ||
|
|
||
| def add_to_front(self, elem): | ||
| self.elems.append(elem) | ||
|
|
||
| def add_to_rear(self, elem): | ||
| self.elems.insert(0, elem) | ||
|
|
||
| def remove_from_front(self): | ||
| return self.elems.pop() | ||
|
|
||
| def remove_from_rear(self): | ||
| return self.elems.pop(0) | ||
|
|
||
| def size(self): | ||
| return len(self.elems) | ||
|
|
||
|
|
||
| def pal_checker(string): | ||
| dc_obj = DequeClass() | ||
| string = string.replace(' ', '').lower() #Только это нужно добавить + перевод в один регистр | ||
|
|
||
| for el in string: | ||
| dc_obj.add_to_rear(el) | ||
|
|
||
| still_equal = True | ||
|
|
||
| while dc_obj.size() > 1 and still_equal: | ||
| first = dc_obj.remove_from_front() | ||
| last = dc_obj.remove_from_rear() | ||
| if first != last: | ||
| still_equal = False | ||
|
|
||
| return still_equal | ||
|
|
||
|
|
||
| print(pal_checker("молоко делили ледоколом")) | ||
| print(pal_checker("Лёша на полке клопа нашёл")) | ||
| print(pal_checker("Аргентина манит негра")) | ||
| print(pal_checker('Молебен о коне Белом')) | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
неверно, здесь линейная, посмотрите пример