-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson07 #7
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?
Lesson07 #7
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Задача №1 | ||
| # Отсортируйте по убыванию методом пузырька одномерный целочисленный массив, заданный случайными | ||
| # числами на промежутке [-100; 100). Выведите на экран исходный и отсортированный массивы. | ||
| # Примечания: | ||
| # ● алгоритм сортировки должен быть в виде функции, которая принимает на вход массив данных, | ||
| # ● постарайтесь сделать алгоритм умнее, но помните, что у вас должна остаться сортировка пузырьком. | ||
| # Улучшенные версии сортировки, например, расчёской, шейкерная и другие в зачёт не идут. | ||
|
|
||
|
|
||
| def sort_(array): | ||
| n = 1 | ||
| while n < len(array): | ||
| for i in range(len(array) - n): | ||
| if array[i] > array[i + 1]: | ||
| array[i], array[i + 1] = array[i + 1], array[i] | ||
| n += 1 | ||
| #print(array) | ||
|
|
||
|
|
||
| import random | ||
| array = [random.randint(-100, 100) for _ in range(10)] | ||
|
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. "на промежутке [-100; 100)" |
||
| print("Неотсортированный массив", array) | ||
| sort_(array) | ||
| print("Отсортированный массив", array) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Задание №2 | ||
| # Отсортируйте по возрастанию методом слияния одномерный вещественный массив, заданный случайными | ||
| # числами на промежутке [0; 50). Выведите на экран исходный и отсортированный массивы. | ||
|
|
||
| import random | ||
|
|
||
|
|
||
| array = [float('%.2f'%(random.uniform(0, 49))) for _ in range(10)] | ||
|
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.
|
||
| #array = [6, 8, 3, 0, 9, 1, 2, 7, 4, 5] | ||
|
|
||
|
|
||
| def merge(a, left, mid, right): | ||
| it1 = 0 | ||
| it2 = 0 | ||
| result = [0 for _ in range(right-left)] | ||
| while left + it1 < mid and mid + it2 < right: | ||
| if a[left + it1] < a[mid + it2]: | ||
| result[it1 + it2] = a[left + it1] | ||
| it1 += 1 | ||
| else: | ||
| result[it1 + it2] = a[mid + it2] | ||
| it2 += 1 | ||
|
|
||
| while left + it1 < mid: | ||
| result[it1 + it2] = a[left + it1] | ||
| it1 += 1 | ||
|
|
||
| while mid + it2 < right: | ||
| result[it1 + it2] = a[mid + it2] | ||
| it2 += 1 | ||
|
|
||
| for i in range(it1 + it2): | ||
| a[left + i] = result[i] | ||
|
|
||
| def mergeSort(a): | ||
|
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. Забавно, когда студент внезапно забывает Python Style и именно в этой задачи начинает использовать |
||
| i = 1 | ||
| while i <= len(a): | ||
| j = 0 | ||
| while j <= len(a) - i: | ||
| merge(a,j,j+i,min(j+2*i,len(a))) | ||
| j += 2 * i | ||
| i *= 2 | ||
|
|
||
| print('Неотсортированный:', array) | ||
| mergeSort(array) | ||
| print('Отсортированный:', array) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Задание №3 | ||
| # Массив размером 2m + 1, где m — натуральное число, заполнен случайным образом. Найдите в массиве | ||
| # медиану. Медианой называется элемент ряда, делящий его на две равные части: в одной находятся | ||
| # элементы, которые не меньше медианы, в другой — не больше медианы. | ||
| # | ||
| # Примечание: задачу можно решить без сортировки исходного массива. Но если это слишком сложно, | ||
| # используйте метод сортировки, который не рассматривался на уроках (сортировка слиянием также | ||
| # недопустима). | ||
|
|
||
|
|
||
| import random | ||
|
|
||
| m = int(input('Введите m')) | ||
| array = [random.randint(0, 10) for _ in range(2*m+1)] | ||
| print(array) | ||
|
|
||
| #array = [6, 0, 3, 9, 4, 4, 4, 4, 5] | ||
|
|
||
| for inx_i,i in enumerate(array): | ||
| count_l = 0; count_g = 0; count_e = 0; | ||
|
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. Это что за финт ушами? JS? C++? |
||
| for inx_j,j in enumerate(array): | ||
| if inx_i != inx_j: | ||
| count_l += 1 if i < j else 0 | ||
| count_g += 1 if i > j else 0 | ||
| count_e += 1 if i == j else 0 | ||
| if count_l == count_g or min(count_l, count_g)+count_e >= max(count_l, count_g): | ||
| print('Медина: ', i) | ||
| break | ||
|
|
||
This file was deleted.
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.
👍