Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions task01.py
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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"на промежутке [-100; 100)"
У вас 100 входит.

print("Неотсортированный массив", array)
sort_(array)
print("Отсортированный массив", array)
46 changes: 46 additions & 0 deletions task02.py
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)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random.uniform(0, 50)

#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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Забавно, когда студент внезапно забывает Python Style и именно в этой задачи начинает использовать camelCase.
Алгоритм принимается 👍

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)
29 changes: 29 additions & 0 deletions task03.py
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;

Choose a reason for hiding this comment

The 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

1 change: 0 additions & 1 deletion test.txt

This file was deleted.