diff --git a/python/src/main.py b/python/src/main.py index e37a77c..a066937 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,7 +1,10 @@ -def summ(a: int, b: int) -> int: - return a + b - +import sample if __name__ == "__main__": - print("Hello world") - print(summ(3, 4)) + print("lab 5 python") + sobachka = sample.Dog(age=12, name="Zhychka", gender="male") + + print(f"The dog's name is {sobachka.name}") + print(f"The dog {sobachka.name} is {sobachka.age} years old") + print(f"{sobachka.name} is a {sobachka.gender}") + diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2601.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2601.py" new file mode 100644 index 0000000..2bdc7a1 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2601.py" @@ -0,0 +1,20 @@ +def main(C, S): + count = 0 + for j in range(C - 1): + for i in range(C - 1): + if S[i] > S[i + 1]: + S[i], S[i + 1] = S[i + 1], S[i] + count += 1 + if count > 0: + print(*S) + if count == 0: + print(0) + + +C = int(input()) +if C == 1: + S = int(input()) + print(S) +else: + S = list(map(int, input().split())) + main(C, S) \ No newline at end of file diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2602.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2602.py" new file mode 100644 index 0000000..28f50c2 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2602.py" @@ -0,0 +1,30 @@ +def sortirovka(A): + number = list() + cost = list() + for i in range(A): + listik = list(map(int, input().split())) + number.append(listik[0]) + cost.append(listik[-1]) + + for i in range(A- 1): + for j in range(A - 1): + if cost[j] == cost[j + 1]: + if number[j] < number[j + 1]: + cost[j], cost[j + 1] = cost[j + 1], cost[j] + number[j], number[j + 1] = number[j + 1], number[j] + else: + if cost[j] > cost[j + 1]: + cost[j], cost[j + 1] = cost[j + 1], cost[j] + number[j], number[j + 1] = number[j + 1], number[j] + + for i in range(A - 1, -1, -1): + print(str(number[i]) + " " + str(cost[i])) + + + +A = int(input()) +if A == 1: + listik = list(map(int, input().split())) + print(*listik) +else: + sortirovka(A) \ No newline at end of file diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2604.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2604.py" new file mode 100644 index 0000000..753e2d7 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2604.py" @@ -0,0 +1,40 @@ +def merge(listik, listik_1, l, m, h): + t = l + i = l + j = m + 1 + invers = 0 + while i <= m and j <= h: + if listik[i] <= listik[j]: + listik_1[t] = listik[i] + i = i + 1 + else: + listik_1[t] = listik[j] + j = j + 1 + invers = invers + (m - i + 1) + t = t + 1 + while i <= m: + listik_1[t] = listik[i] + t = t + 1 + i = i + 1 + for i in range(l, h + 1): + listik[i] = listik_1[i] + return invers + +def merge_sort(listik, listik_1, l, h): + if h <= l: + return 0 + m = l + ((h - l) >> 1) + invers = 0 + invers = invers + merge_sort(listik, listik_1, l, m) + invers = invers + merge_sort(listik, listik_1, m + 1, h) + invers = invers + merge(listik, listik_1, l, m, h) + return invers + +A = int(input()) +if A == 1: + listik = int(input()) + print(0) +else: + listik = list(map(int, input().split())) + listik_1 = listik.copy() + print(merge_sort(listik, listik_1, 0, len(listik) - 1)) \ No newline at end of file diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2605.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2605.py" new file mode 100644 index 0000000..cb37fe6 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2605.py" @@ -0,0 +1,28 @@ +def quick_sort(listik): + if len(listik) <= 1: + return listik + ciferka = listik[len(listik) // 2] + l = [i for i in listik if i < ciferka] + m = [i for i in listik if i == ciferka] + r = [i for i in listik if i > ciferka] + result = quick_sort(l) + m + quick_sort(r) + return result + + +def duplicate(arr): + arr = quick_sort(arr) + result = [] + for i in range(len(arr)): + if (i == 0) or (arr[i] != arr[i-1]): + result.append(arr[i]) + return result + + +A = int(input()) +if A == 1: + listik = int(input()) + print(listik) +else: + listik = list(map(int, input().split())) + listik = duplicate(listik) + print(len(listik)) \ No newline at end of file diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2606.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2606.py" new file mode 100644 index 0000000..d9c8548 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2606.py" @@ -0,0 +1,24 @@ +def count_sort(listik, check): + answers = [] + mini = min(listik) + maxi = max(listik) + sup = [0 for i in range(maxi - mini + 1)] + for elem in listik: + sup[elem - mini] = sup[elem - mini] + 1 + + for i in range(len(sup)): + if sup[i] <= check[i]: + answers.append('no') + else: + answers.append('yes') + + for i in answers: + print(i) + + + +goods_types = int(input()) +each_product = list(map(int, input().split())) +orders_count = int(input()) +orders = list(map(int, input().split())) +count_sort(orders, each_product) \ No newline at end of file diff --git "a/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2607.py" "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2607.py" new file mode 100644 index 0000000..ab11a64 --- /dev/null +++ "b/python/src/module2/\320\267\320\260\320\264\320\260\321\207\320\2607.py" @@ -0,0 +1,52 @@ +def counting_sort(listik, position): + mini = min([int(x[position]) for x in listik]) + maxi = max([int(x[position]) for x in listik]) + number = maxi - mini + 1 + support = [0 for i in range(number)] + for elem in listik: + support[int(elem[position]) - mini] = support[int(elem[position]) - mini] + 1 + size = len(listik) + for i in range(number - 1, -1, -1): + size = size - support[i] + support[i] = size + result = [None for i in range(len(listik))] + for elem in listik: + result[support[int(elem[position]) - mini]] = elem + support[int(elem[position]) - mini] = support[int(elem[position]) - mini] + 1 + return result + + +def print_phase(listik, position): + print('Phase ' + str(abs(position))) + for i in range(10): + check= [x for x in listik if int(x[position]) == i] + print(f'Bucket {i}:', end=' ') + if len(check) > 0: + print(*check, sep=', ') + else: + print('empty') + +def radix_sort(listik): + digits_num = len(listik[0]) + for i in range(-1, -(digits_num) - 1, -1): + listik = counting_sort(listik, i) + print_phase(listik, i) + print('*' * 10) + print("Sorted array:") + print(*listik, sep=", ") + + + +A = int(input()) +if A == 1: + listik = input() + print("Initial array:") + print(*listik, sep=", ") + print('*' * 10) + radix_sort(listik) +else: + listik = [input() for _ in range(A)] + print("Initial array:") + print(*listik, sep=", ") + print('*' * 10) + radix_sort(listik) \ No newline at end of file diff --git "a/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2601.py" "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2601.py" new file mode 100644 index 0000000..5c8bfd5 --- /dev/null +++ "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2601.py" @@ -0,0 +1,34 @@ +def get_hash(stroka, len_stroka): + p = 10**9 + 7 + x = 31 + stroka_hash = 0 + for i in range(len_stroka): + stroka_hash = (stroka_hash * x + ord(stroka[i])) % p + return stroka_hash + +def the_Rabin_Karp_algorithm(stroka, strochka): + p = 10**9 + 7 + x = 31 + stroka_hash = get_hash(stroka, len(strochka)) + substring_hash = get_hash(strochka, len(strochka)) + sliding_hash = 1 + for i in range(len(strochka)): + sliding_hash = (sliding_hash * x) % p + + list_s_indexami = [] + + for i in range(len(stroka) - len(strochka) + 1): + if substring_hash == stroka_hash: + list_s_indexami.append(i) + if i + len(strochka) < len(stroka): + stroka_hash = (stroka_hash * x - ord(stroka[i]) * sliding_hash + ord(stroka[i + len(strochka)])) % p + stroka_hash = (stroka_hash + p) % p + return list_s_indexami + +if __name__ == "__main__": + p = 10**9 + 7 + x = 31 + stroka = input() + strochka = input() + res = the_Rabin_Karp_algorithm(stroka, strochka) + print(*res) \ No newline at end of file diff --git "a/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2602.py" "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2602.py" new file mode 100644 index 0000000..23003aa --- /dev/null +++ "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2602.py" @@ -0,0 +1,33 @@ +def get_hash(stroka_t, len_stroka): + p = 10**9 + 7 + x = 31 + stroka_hash = 0 + for i in range(len_stroka): + stroka_hash = (stroka_hash * x + ord(stroka_t[i])) % p + return stroka_hash + +def cyclic_shift(stroka_t, stroka_s): + p = 10**9 + 7 + x = 31 + string_hash = get_hash(stroka_t, len(stroka_s)) + substring_hash = get_hash(stroka_s, len(stroka_s)) + sliding_hash = 1 + for i in range(len(stroka_s)): + sliding_hash = (sliding_hash * x) % p + + + for i in range(len(stroka_t) - len(stroka_s) + 1): + if substring_hash == string_hash: + return i + if i + len(stroka_s) < len(stroka_t): + string_hash = (string_hash * x - ord(stroka_t[i]) * sliding_hash + ord(stroka_t[i + len(stroka_s)])) % p + string_hash = (string_hash + p) % p + return -1 + +if __name__ == "__main__": + stroka_s = input() + stroka_t = input() + stroka_t = stroka_t * 2 + p = 10**9 + 7 + x = 31 + print(cyclic_shift(stroka_t, stroka_s)) \ No newline at end of file diff --git "a/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2603.py" "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2603.py" new file mode 100644 index 0000000..6873084 --- /dev/null +++ "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2603.py" @@ -0,0 +1,26 @@ +def prefix_function(stroka): + res = [0] * len(stroka) + res[0] = 0 + for i in range(len(stroka) - 1): + j = res[i] + while (j > 0) and (stroka[i + 1] != stroka[j]): + j = res[j - 1] + if stroka[i + 1] == stroka[j]: + res[i + 1] = j + 1 + else: + res[i + 1] = 0 + return res + +def repeated_string(stroka): + prefix = prefix_function(stroka) + n = len(stroka) - prefix[-1] + if len(stroka) % n == 0: + return n + return len(stroka) + + +if __name__ == "__main__": + stroka = input() + strochka = repeated_string(stroka) + print(len(stroka) // strochka) + \ No newline at end of file diff --git "a/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2604.py" "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2604.py" new file mode 100644 index 0000000..1ec987a --- /dev/null +++ "b/python/src/module3/\320\267\320\260\320\264\320\260\321\207\320\2604.py" @@ -0,0 +1,37 @@ +def z_function(string): + Z = [0] * len(string) + l = 0 + r = 0 + maximum_index = 0 + for i in range(1, len(string)): + if i <= r: + Z[i] = min(r - i + 1, Z[i - l]) + while i + Z[i] < len(string) and string[Z[i]] == string[Z[i] + i]: + Z[i] += 1 + + if Z[i] > Z[maximum_index]: + maximum_index = i + + if i + Z[i] - 1 > r: + l = i + r = i + Z[i] - 1 + + return Z, maximum_index + +def min_len(string): + z, maximum_index = z_function(string) + if maximum_index + z[maximum_index] == len(string): + return len(string) - z[maximum_index] + else: + k = 0 + for i in range(len(string) - 1, -1, -1): + if z[i] > k and z[i] < z[maximum_index] and i + z[i] == len(string): + k = z[i] + + return len(string) - k + + +if __name__ == "__main__": + string = input() + + print(min_len(string)) \ No newline at end of file diff --git "a/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2601.py" "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2601.py" new file mode 100644 index 0000000..28f61f6 --- /dev/null +++ "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2601.py" @@ -0,0 +1,17 @@ +def psp(stroka): + stack = list() + extra_parenthesis = 0 + for i in stroka: + if i == "(": + stack.append(i) + elif i == ")": + if len(stack) != 0: + stack.pop() + else: + extra_parenthesis = extra_parenthesis + 1 + return len(stack) + extra_parenthesis + +if __name__ == "__main__": + stroka = input() + print(psp(stroka)) + \ No newline at end of file diff --git "a/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2602.py" "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2602.py" new file mode 100644 index 0000000..a931102 --- /dev/null +++ "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2602.py" @@ -0,0 +1,23 @@ +def sort_of_wagon(list_of_wagons): + stack = list() + check_number = 1 + for i in list_of_wagons: + while len(stack) > 0 and stack[-1] == check_number: + stack.pop() + check_number = check_number + 1 + if i == check_number: + check_number = check_number + 1 + else: + stack.append(i) + + while len(stack) > 0: + if stack.pop() != check_number: + return "NO" + check_number = check_number + 1 + return "YES" + +if __name__ == "__main__": + number = int(input()) + list_of_wagons = list(map(int, input().split())) + print(sort_of_wagon(list_of_wagons)) + \ No newline at end of file diff --git "a/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2603.py" "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2603.py" new file mode 100644 index 0000000..c696c55 --- /dev/null +++ "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2603.py" @@ -0,0 +1,14 @@ +def nearest_smaller_one(numbers): + stack = list() + answer = [-1] * len(numbers) + for i in range(len(numbers)): + while len(stack) > 0 and numbers[stack[-1]] > numbers[i]: + answer[stack.pop()] = i + stack.append(i) + return answer + +if __name__ == "__main__": + number = int(input()) + numbers = list(map(int, input().split())) + answer = nearest_smaller_one(numbers) + print(*answer) \ No newline at end of file diff --git "a/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2604.py" "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2604.py" new file mode 100644 index 0000000..0df3570 --- /dev/null +++ "b/python/src/module4/\320\267\320\260\320\264\320\260\321\207\320\2604.py" @@ -0,0 +1,19 @@ +def minimums(numbers, step): + deque = list() + answer = list() + for i in range(len(numbers)): + while (len(deque) > 0) and (deque[0] < (i - step + 1)): + deque.pop(0) + while (len(deque) > 0) and (numbers[deque[-1]] > numbers[i]): + deque.pop() + deque.append(i) + if i >= step - 1: + answer.append(numbers[deque[0]]) + return answer + +if __name__ == "__main__": + number, step = map(int, input().split()) + numbers = list(map(int, input().split())) + answer = minimums(numbers, step) + for i in answer: + print(i) \ No newline at end of file diff --git a/python/src/sample.py b/python/src/sample.py new file mode 100644 index 0000000..0aa7ce5 --- /dev/null +++ b/python/src/sample.py @@ -0,0 +1,39 @@ +class Dog: + age: int + name: str + gender: str + + + def __init__(self, age: int, name: str, gender: str): + self.__age = age + self.__name = name + self.__gender = gender + + + @property + def age(self) -> int: + return self.__age + + @age.setter + def age_setter(self, age: int): + assert age < 0 and age > 15, "The dog's age is incorrectly determined" + self.__age = age + + @property + def name(self) -> str: + return self.__name + + @name.setter + def name_setter(self, name: str): + assert len(name) <= 0, "The dog should have a name" + self.__name = name + + + @property + def gender(self) -> str: + return self.__gender + + @gender.setter + def gender_setter(self, gender: str): + assert gender.lower != 'male' or gender.lower != "female", "The dog's gender is unknown" + self.__gender = gender \ No newline at end of file