-
Notifications
You must be signed in to change notification settings - Fork 0
Hw7 #8
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?
Hw7 #8
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,27 @@ | ||
| # 1. Отсортируйте по убыванию методом "пузырька" одномерный целочисленный массив, заданный | ||
| # случайными числами на промежутке [-100; 100). Выведите на экран исходный и отсортированный массивы. | ||
| # Сортировка должна быть реализована в виде функции. | ||
| # По возможности доработайте алгоритм (сделайте его умнее). | ||
|
|
||
| import random | ||
|
|
||
| LEFT_EDGE = -100 | ||
| RIGHT_EDGE = 100 | ||
| SIZE = 20 | ||
|
|
||
|
|
||
| def sort_bubble(array): | ||
| for ii in range(0, len(array) - 1): | ||
| for i in range(0, len(array) - ii - 1): | ||
| if array[i] < array[i + 1]: | ||
| array[i], array[i + 1] = array[i + 1], array[i] | ||
| # print(array) | ||
|
|
||
|
|
||
| array_rand = [random.randrange(LEFT_EDGE, RIGHT_EDGE) for _ in range(SIZE)] | ||
| array_sort = array_rand.copy() | ||
| sort_bubble(array_sort) | ||
|
|
||
| # print('*' * 10) | ||
| print(array_rand) | ||
| print(array_sort) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # 2. Отсортируйте по возрастанию методом слияния одномерный вещественный массив, | ||
| # заданный случайными числами на промежутке [0; 50). | ||
| # Выведите на экран исходный и отсортированный массивы. | ||
|
|
||
| import random | ||
|
|
||
| LEFT_EDGE = 0 | ||
| RIGHT_EDGE = 50 | ||
| SIZE = 11 | ||
|
|
||
|
|
||
| def get_rand_val(left, right): | ||
| rand = random.uniform(left, right) | ||
| while rand == right: | ||
| rand = random.uniform(left, right) | ||
| return rand | ||
|
|
||
|
|
||
| def sort_merge(array): | ||
|
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 len(array) <= 1: | ||
| return | ||
|
|
||
| middle = len(array) // 2 | ||
| array_1 = array[0:middle] | ||
|
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_2 = array[middle:] | ||
|
|
||
| sort_merge(array_1) | ||
| sort_merge(array_2) | ||
|
|
||
| i = 0 | ||
| a1 = 0 | ||
| a2 = 0 | ||
|
|
||
| while len(array_1) > a1 and len(array_2) > a2: | ||
| if array_1[a1] < array_2[a2]: | ||
| array[i] = array_1[a1] | ||
| a1 += 1 | ||
| else: | ||
| array[i] = array_2[a2] | ||
| a2 += 1 | ||
|
|
||
| i += 1 | ||
|
|
||
| while len(array_1) > a1: | ||
| array[i] = array_1[a1] | ||
| a1 += 1 | ||
| i += 1 | ||
| while len(array_2) > a2: | ||
| array[i] = array_2[a2] | ||
| a2 += 1 | ||
| i += 1 | ||
|
|
||
|
|
||
| array_rand = [get_rand_val(LEFT_EDGE, RIGHT_EDGE) for _ in range(SIZE)] | ||
| array_sort = array_rand[:] | ||
| sort_merge(array_sort) | ||
|
|
||
| print(array_rand) | ||
| print(array_sort) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # 3. Массив размером 2m + 1, где m – натуральное число, заполнен случайным образом. | ||
| # Найдите в массиве медиану. Медианой называется элемент ряда, делящий его на две равные части: | ||
| # в одной находятся элементы, которые не меньше медианы, в другой – не больше медианы. | ||
| # Задачу можно решить без сортировки исходного массива. | ||
| # Но если это слишком сложно, то используйте метод сортировки, который не рассматривался на уроках | ||
|
|
||
| import random | ||
| from collections import deque | ||
|
|
||
| LEFT_EDGE = 0 | ||
| RIGHT_EDGE = 200 | ||
|
|
||
|
|
||
| def get_median(array): | ||
|
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. Интересное оригинальное решение. |
||
| sum_ = 0 | ||
| for num in array: | ||
| sum_ += num | ||
|
|
||
| middle = sum_ // len(array) | ||
|
|
||
| deq_left = deque() | ||
| deq_right = deque() | ||
|
|
||
| # print(middle) | ||
|
|
||
| for num in array: | ||
| if num > middle: | ||
| if len(deq_right) == 0 or deq_right[0] > num: | ||
| deq_right.appendleft(num) | ||
| else: | ||
| deq_right.append(num) | ||
| else: | ||
| if len(deq_left) == 0 or deq_left[-1] > num: | ||
| deq_left.appendleft(num) | ||
| else: | ||
| deq_left.append(num) | ||
|
|
||
| # print(deq_left, deq_right) | ||
| if len(deq_left) > len(deq_right): | ||
| while len(deq_left) - 1 != len(deq_right): | ||
| deq_right.appendleft(deq_left[-1]) | ||
| deq_left.pop() | ||
| max_left_ind = 0 | ||
|
|
||
| for i, num in enumerate(deq_left): | ||
| if num > deq_left[max_left_ind]: | ||
| max_left_ind = i | ||
|
|
||
| deq_left[max_left_ind], deq_left[-1] = deq_left[-1], deq_left[max_left_ind] | ||
| # print(deq_left, deq_right) | ||
|
|
||
| return deq_left[-1] | ||
| else: | ||
| while len(deq_right) - 1 != len(deq_left): | ||
| deq_left.append(deq_right[0]) | ||
| deq_right.popleft() | ||
| min_right_ind = 0 | ||
|
|
||
| for i, num in enumerate(deq_right): | ||
| if num < deq_right[min_right_ind]: | ||
| min_right_ind = i | ||
|
|
||
| deq_right[min_right_ind], deq_right[0] = deq_right[0], deq_right[min_right_ind] | ||
| # print(deq_left, deq_right) | ||
|
|
||
| return deq_right[0] | ||
|
|
||
|
|
||
| m = int(input('Введите m: ')) | ||
|
|
||
| array = [random.randrange(LEFT_EDGE, RIGHT_EDGE) for _ in range(2 * m + 1)] | ||
|
|
||
| print(array) | ||
| print(f'Медиана: {get_median(array)}') | ||
|
|
||
| # print(sorted(array)) | ||
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.
Отлично. На уроке дополним код, чтобы алгоритм стал умнее