Skip to content
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

pythonalgoritm and lab5 #50

Open
wants to merge 1 commit into
base: Sysujkin_Denis_Dmitrievich
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions python/src/module2/zad1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def sort(arr):
n = len(arr)
swapped = False
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print(*arr)
swapped = True
if swapped== False:
print(0)
break

N = int(input())
arr = list(map(int, input().split()))

sort(arr)
16 changes: 16 additions & 0 deletions python/src/module2/zad2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
n = int(input())
counter = 0
items = []

while counter < n:
item = [int(num) for num in input().split()]
items.append(item)
counter += 1

for _ in range(n-1):
for i in range(n-1):
if items[i][1] < items[i+1][1] or (items[i][1] == items[i+1][1] and items[i][0] > items[i+1][0]):
items[i], items[i+1] = items[i+1], items[i]

for item in items:
print(' '.join(map(str, item)))
29 changes: 29 additions & 0 deletions python/src/module2/zad3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def merge_sort_recursive(sequence, start=1):
if len(sequence) <= 1:
return sequence, start
middle = len(sequence) // 2
left, left_indices = merge_sort_recursive(sequence[:middle], start)
right, right_indices = merge_sort_recursive(sequence[middle:], start + middle)
return combine_lists(left, right, [start, start + len(sequence) - 1]), start
def combine_lists(list1, list2, indices_values):
combined = []
i = j = 0
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
combined.append(list1[i])
i += 1
else:
combined.append(list2[j])
j += 1
if i < len(list1):
combined += list1[i:]
if j < len(list2):
combined += list2[j:]
start_idx = indices_values[0]
end_idx = indices_values[1]
print(start_idx, end_idx, combined[0], combined[-1])
return combined

n = int(input())
result, _ = merge_sort_recursive(list(map(int, input().split())))
print(' '.join(map(str, result)))
30 changes: 30 additions & 0 deletions python/src/module2/zad4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def mergeSort(arr):
if len(arr) <= 1:
return arr, 0

inversions = 0
mid = len(arr) // 2
left, inv_left = mergeSort(arr[:mid])
right, inv_right = mergeSort(arr[mid:])

inversions += inv_left + inv_right

merged = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
inversions += len(left) - i

merged.extend(left[i:])
merged.extend(right[j:])

return merged, inversions
n = int(input())
arr = list(map(int, input().split()))
sorted_arr, inv_count = mergeSort(arr)
print(inv_count)
43 changes: 43 additions & 0 deletions python/src/module2/zad5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1

def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
less = [x for x in arr[1:] if x <= pivot]
greater = [x for x in arr[1:] if x > pivot]
return quick_sort(less) + [pivot] + quick_sort(greater)

n = int(input())
numbers = list(map(int, input().split()))
merge_sort(numbers)
result = []
for el in range(n-1):
if numbers[el] != numbers[el+1]:
result.append(numbers[el])
result.append(numbers[-1])
print(len(result))
15 changes: 15 additions & 0 deletions python/src/module2/zad6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
idT = int(input())
TovarList = [int(num) for num in input().split()]
NumberCustomes = int(input())
CustomesList = [int(num) for num in input().split()]
count_list = [0]*(idT+1)
for i in CustomesList:
count_list[i] += 1
count_list.pop(0)
stopC = 0
while stopC < idT:
if count_list[stopC] > TovarList[stopC]:
print("yes")
else:
print("no")
stopC += 1
35 changes: 35 additions & 0 deletions python/src/module2/zad7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
def get_max_length(arr):
max_len = 0
for item in arr:
max_len = max(max_len, len(item))
return max_len

def distribute_to_bins(arr, bins, digit):
for item in arr:
value = item if len(item) > digit else "0" * digit + item
bins[int(value[-(digit+1)])].append(item)

def collect_from_bins(arr, bins):
output = []
for bin in bins:
output.extend(bin)
return output

def radix_sort(arr):
bins = [[] for _ in range(10)]
max_len = get_max_length(arr)
print("Initial array:\n" + ", ".join(arr))
for phase in range(max_len):
print("**********")
print(f"Phase {phase + 1}")
distribute_to_bins(arr, bins, phase)
for i in range(10):
print(f"Bucket {i}: {', '.join(bins[i]) if bins[i] else 'empty'}")
arr = collect_from_bins(arr, bins)
bins = [[] for _ in range(10)]
print("**********")
print("Sorted array:\n" + ", ".join(arr))

n = int(input())
arr = [input() for i in range(n)]
radix_sort(arr)
Empty file removed python/tests/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions python/tests/lab5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Cat():

def __init__(self, name: str, age: int, weight: float, height: float):
self.name = name
self.age = age
self.weight = weight
self.height = height
self.Rules()

def Rules(self):
if not isinstance(self.name, str):
raise TypeError("Неверный тип данных name")
if self.age < 0 or self.age > 15:
raise ValueError("Неверно указан возраст")
elif self.height < 0 or self.height > 25:
raise ValueError("Неверно указан рост")
elif self.weight < 0 or self.weight > 8:
raise ValueError("Неверно указан вес")
print(f"Успешное создание\nИмя кошки: {self.name}\nВозраст: {self.age} лет\nВес: {self.weight} кг\nРост: {self.height} см ")

6 changes: 6 additions & 0 deletions python/tests/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import lab5 as lab5t

Unitfirst = lab5t.Cat("Stesha", 5, 4, 21 )

Unitsecond = lab5t.Cat("Barsik", 8, 6, 24 )

37 changes: 0 additions & 37 deletions python/tests/test_main.py

This file was deleted.