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

Lab 5 on golang -> lab 1 on Python + 7task's from YandexKontest #60

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

## ФИО
## Вераксо Марк Владимирович

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

Expand Down
42 changes: 42 additions & 0 deletions python/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Cat:
def __init__ (self, name: str, age: int, breed: str):
self.__name = name
self.__age = age
self.__breed = breed
self.check_age(age)

def check_age(self, age:int):
if age < 0 or age > 18:
print(f"Вы указали не верный возраст вашего питомца!")
else:
self.__age = age

@property
def name(self):
return self.__name

@name.setter
def name(self, n: str):
self.__name = n

@property
def age(self):
return self.__age
@age.setter
def age(self, a: int):
self.check_age(a)
self.__age = a

@property
def breed(self):
return self.__breed

@breed.setter
def breed(self, b: str):
self.__breed = b

def output(self):
print(f"Имя вашего питомца - {self.__name}")
print(f"Возраст вашего питомца - {self.__age}")
print(f"Порода вашего питомца - {self.__breed}")

8 changes: 3 additions & 5 deletions python/src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
def summ(a: int, b: int) -> int:
return a + b

import __init__

if __name__ == "__main__":
print("Hello world")
print(summ(3, 4))
cat1 = __init__.Cat("Пушок", 5, "Русская голубая")
cat1.output()
17 changes: 17 additions & 0 deletions python/src/module2/task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
N = int(input())
nums = list(map(int, input().split()))

def bubbles(nums):
len_nums = len(nums)
s = 0
for i in range(len_nums - 1):
for j in range(len_nums - i - 1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
s += 1
print(*nums)
if s == 0:
print(0)

bubbles(nums)

23 changes: 23 additions & 0 deletions python/src/module2/task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def bubble(numbers):
n = len(numbers)
for i in range(n):
for j in range(0, n-i-1):
if numbers[j][1] < numbers[j+1][1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
if numbers[j][1] == numbers[j+1][1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
if numbers[j][0] > numbers[j+1][0]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]

return numbers

n = int(input())
nums = []
for _ in range(n):
num1, num2 = map(int, input().split())
nums.append((num1, num2))

sort = bubble(nums)
for nums in sort:
print(nums[0], nums[1])

33 changes: 33 additions & 0 deletions python/src/module2/task3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
nums = list(map(int, input().split()))
def merge_sort(nums):
n = len(nums)
if len(nums) == 1:
return nums
if len(nums) == 0:
return nums
left_half = merge_sort(nums[:len(nums) // 2])
right_half = merge_sort(nums[len(nums) // 2:])
i = j = k = 0
zlist = [0] * len(nums)
while i < len(left_half) and j < len(right_half):
if left_half[i] <= right_half[j]:
zlist[k] = left_half[i]
i += 1
else:
zlist[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
zlist[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
zlist[k] = right_half[j]
j += 1
k += 1
for i in range(len(nums)):
nums[i] = zlist[i]
return nums

merge_sort(nums)
print(nums)
30 changes: 30 additions & 0 deletions python/src/module2/task4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def merge_sort_inversion_count(arr):
if len(arr) <= 1:
return arr, 0
mid = len(arr) // 2
left, inv_left = merge_sort_inversion_count(arr[:mid])
right, inv_right = merge_sort_inversion_count(arr[mid:])
sorted_arr, inversions = merge(left, right)
return sorted_arr, inversions + inv_left + inv_right

def merge(left, right):
result = []
inv = 0
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
inv += len(left) - i
result += left[i:]
result += right[j:]
return result, inv

if __name__ == "__main__":
N = int(input())
nums = list(map(int, input().split()))
sorted_nums, inversions = merge_sort_inversion_count(nums)
print(inversions
4 changes: 4 additions & 0 deletions python/src/module2/task5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
N = int(input())
nums = list(map(int, input().split()))
array = set(nums)
print(len(array))
28 changes: 28 additions & 0 deletions python/src/module2/task6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def purchase_automation():
types = int(input())
storage = list(map(int, input().split()))
total = int(input())
orders = list(map(int, input().split()))

orders_count = {}
for order in orders:
if order in orders_count:
orders_count[order] += 1
else:
orders_count[order] = 1

search_result = []
for item in range(types):
if item + 1 in orders_count:
if storage[item] < orders_count[item+1]:
search_result.append("yes")
else:
search_result.append("no")
else:
search_result.append("no")

return search_result

output = purchase_automation()
for line in output:
print(line)
49 changes: 49 additions & 0 deletions python/src/module2/task7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
N = int(input())
nums = [int(input()) for _ in range(N)]
def bucket_sort(nums, exp, buckets):
dlina = N
output = [0] * dlina
counter = [0] * 10

for i in range(dlina):
index_of_1_bucket = nums[i] // exp
counter[index_of_1_bucket % 10] += 1

for i in range(1, 10):
counter[i] += counter[i - 1]

for i in range(dlina - 1, -1, -1):
index_of_2_bucket = nums[i] // exp
output[counter[index_of_2_bucket % 10] - 1] = nums[i]
counter[index_of_2_bucket % 10] -= 1

for i in range(dlina):
nums[i] = output[i]

print("Bucket status:")
for i in range(10):
print(f"Bucket {i}: {', '.join(str(num) for num in buckets[i])}")

def secondary_sort(nums):
max_num = max(nums)
exp = 1
buckets = [[] for _ in range(10)]

print("Initial array:")
print(", ".join(str(num) for num in nums))

while max_num // exp > 0:
for num in nums:
buckets[num // exp % 10].append(num)

bucket_sort(nums, exp, buckets)

for i in range(10):
buckets[i] = []

exp *= 10

print("Sorted array:")
print(", ".join(str(num) for num in nums))

secondary_sort(nums)