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

Модуль 2: Сортировки (Основные задачи) #58

Open
wants to merge 1 commit into
base: Kozlov_Egor_Aleksandrovich
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Репозиторий для работ по курсу информатика

Вот сюда нужно будет в первой работе с гитом добавит свое ФИО

## ФИО
## Козлов Егор Александрович, 1/278

## Работа с репозиторием

Expand Down
7 changes: 0 additions & 7 deletions python/src/main.py

This file was deleted.

80 changes: 80 additions & 0 deletions python/src/module1/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Kozlov Egor 1/278; Variant 11

class Document:
def __init__(self, path: str, docType: str, docSize: int):
if not isinstance(path, str):
raise TypeError("path must be str")

if not isinstance(docType, str):
raise TypeError("docType must be str")

self.__documentPath = path
self.__documentType = docType
self.documentSize = docSize

@property
def documentPath(self) -> str:
return self.__documentPath

@property
def documentType(self) -> str:
return self.__documentType

@property
def documentSize(self) -> int:
return self.__documentSize

@documentSize.setter
def documentSize(self, value) -> None:
if not isinstance(value, int):
raise TypeError("docSize must be int")

if value < 0:
raise ValueError(value)

self.__documentSize = value

def __str__(self) -> str:
return f"Document: '{self.documentPath}', '{self.documentType}', {self.documentSize}"


if __name__ == "__main__":
print("Test 1")
instance = Document("/media/Files/Script.docx", "docx", 32000)

print("Test 2")
try:
Document(32, "docx", 32000) # type: ignore
except Exception as e:
print("Catched:", e)

print("Test 3")
try:
Document("/media/Files/Script.docx", 128, 32000) # type: ignore
except Exception as e:
print("Catched:", e)

print("Test 4")
try:
Document("/media/Files/Script.docx", "docx", "32000") # type: ignore
except Exception as e:
print("Catched:", e)

print("Test 5")
try:
Document("/media/Files/Script.docx", "docx", -10)
except Exception as e:
print("Catched:", e)

print(str(instance))

instance.documentSize = 1600

print(str(instance))

try:
instance.documentSize = -18
except Exception as e:
print("Catched:", e)

print(str(instance))
22 changes: 0 additions & 22 deletions python/src/module1/hello.py

This file was deleted.

22 changes: 0 additions & 22 deletions python/src/module1/summ.py

This file was deleted.

36 changes: 0 additions & 36 deletions python/src/module2/bubble_sort.py

This file was deleted.

20 changes: 20 additions & 0 deletions python/src/module2/task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
totals = int(input())
elements = list(map(int, input().split(" ")))

no_swipes_were_made = True
for outer_index in range(len(elements) - 1, 0, -1):
list_is_sorted = True
for inner_index in range(outer_index):
if elements[inner_index] > elements[inner_index + 1]:
elements[inner_index], elements[inner_index + 1] = (
elements[inner_index + 1],
elements[inner_index],
)
list_is_sorted = False
no_swipes_were_made = False
print(*elements)
if list_is_sorted:
break

if no_swipes_were_made:
print(0)
33 changes: 33 additions & 0 deletions python/src/module2/task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Utils
def check_swipe_requirement(
record_current: list[int], record_to_swipe: list[int]
) -> bool:
price_current = record_current[1]
price_to_check = record_to_swipe[1]

if price_current == price_to_check:
id_current = record_current[0]
id_to_check = record_to_swipe[0]
return id_to_check > id_current
else:
return price_to_check < price_current


# Program start
total_count = int(input())
records = [None] * total_count # type: list[list[int] | None]

# Read & sort 'at the run'
for outer_index in range(total_count):
record_to_insert = list(map(int, input().split(" ")))
inner_index = outer_index - 1
while inner_index >= 0 and check_swipe_requirement(
record_to_insert, records[inner_index] # type: ignore
):
records[inner_index + 1] = records[inner_index]
inner_index -= 1
records[inner_index + 1] = record_to_insert

# Output
for record in records:
print(record[0], record[1]) # type: ignore
44 changes: 44 additions & 0 deletions python/src/module2/task_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Utils
def merge(arr1, arr2):
result = []
index1 = 0
index2 = 0

while index1 < len(arr1) and index2 < len(arr2):
if arr1[index1] < arr2[index2]:
result.append(arr1[index1])
index1 += 1
else:
result.append(arr2[index2])
index2 += 1

while index1 < len(arr1):
result.append(arr1[index1])
index1 += 1

while index2 < len(arr2):
result.append(arr2[index2])
index2 += 1

return result


def sort_with_print(arr, print_offset=1):
if len(arr) <= 1:
return arr

split_index = len(arr) // 2

sorted1 = sort_with_print(arr[0:split_index], print_offset)
sorted2 = sort_with_print(arr[split_index:], print_offset + split_index)
merged = merge(sorted1, sorted2)
print(print_offset, print_offset + len(arr) - 1, merged[0], merged[-1])
return merged


# Input
totals = int(input())
list = list(int(i) for i in input().split(" ") if i.strip() != "")

list = sort_with_print(list)
print(*list)
46 changes: 46 additions & 0 deletions python/src/module2/task_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Utils
def merge(arr1, arr2):
result = []
index1 = 0
index2 = 0
total = 0

while index1 < len(arr1) and index2 < len(arr2):
if arr1[index1] <= arr2[index2]:
result.append(arr1[index1])
index1 += 1
else:
# if arr1[index1] != arr2[index2]:
total += len(arr1) - index1
result.append(arr2[index2])
index2 += 1
# counts += 1

while index1 < len(arr1):
result.append(arr1[index1])
index1 += 1

while index2 < len(arr2):
result.append(arr2[index2])
index2 += 1
return (result, total)


def sort_with_print(arr, print_offset=1):
if len(arr) <= 1:
return (arr, 0)

split_index = len(arr) // 2

sorted1 = sort_with_print(arr[0:split_index], print_offset)
sorted2 = sort_with_print(arr[split_index:], print_offset + split_index)
merged = merge(sorted1[0], sorted2[0])
return (merged[0], sorted1[1] + sorted2[1] + merged[1])


# Input
totals = int(input())
list = list(int(i) for i in input().split(" ") if i.strip() != "")

list = sort_with_print(list)
print(list[1])
49 changes: 49 additions & 0 deletions python/src/module2/task_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Utils
def merge(arr1, arr2):
result = []
index1 = 0
index2 = 0

while index1 < len(arr1) and index2 < len(arr2):
if arr1[index1] < arr2[index2]:
result.append(arr1[index1])
index1 += 1
else:
result.append(arr2[index2])
index2 += 1

while index1 < len(arr1):
result.append(arr1[index1])
index1 += 1

while index2 < len(arr2):
result.append(arr2[index2])
index2 += 1

return result


def merge_sort(arr, print_offset=1):
if len(arr) <= 1:
return arr

split_index = len(arr) // 2

sorted1 = merge_sort(arr[0:split_index], print_offset)
sorted2 = merge_sort(arr[split_index:], print_offset + split_index)
merged = merge(sorted1, sorted2)
# print(print_offset, print_offset + len(arr) - 1, merged[0], merged[-1])
return merged


# Input
totals = int(input())
list = list(int(i) for i in input().split(" ") if i.strip() != "")

list = merge_sort(list)
counter = 1
for index in range(len(list) - 1):
if list[index] != list[index + 1]:
counter += 1

print(counter)
Loading