diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..645a67e --- /dev/null +++ b/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.12" diff --git a/python/src/cat.py b/python/src/cat.py new file mode 100644 index 0000000..ca61bf3 --- /dev/null +++ b/python/src/cat.py @@ -0,0 +1,33 @@ +class Cat(): + __age = 0 + __name = "" + __breed = "" + + def __init__(self, age: int, name: str, breed: str): + self.age = age + self.__name = name + self.__breed = breed + + @property + def age(self) -> int: + return self.__age + + @age.setter + def age(self, age: int): + assert (age >= 0 and age <= 25), "Invalid age" + self.__age = age + + @property + def breed(self) -> str: + return self.__breed + + @property + def name(self) -> str: + return self.__name + + @name.setter + def name(self, name: str): + self.__name = name + + def petTheCat(self): + print("Cat {0} says meow\n".format(self.__name)) diff --git a/python/src/main.py b/python/src/main.py index e37a77c..ed2cd31 100644 --- a/python/src/main.py +++ b/python/src/main.py @@ -1,7 +1,19 @@ -def summ(a: int, b: int) -> int: - return a + b - +# type: ignore +import cat if __name__ == "__main__": - print("Hello world") - print(summ(3, 4)) + cat1 = cat.Cat(6, "Tiger", "Siamese") + cat2 = cat.Cat(9, "No name", "unknown") + + cat1.age = 3 + + cat2.name = "Marshal" + cat1.name = "Luna" + + print("Cat's age is {0}".format(cat1.age)) + print("Its name is {0}".format(cat1.name)) + cat1.petTheCat() + + print("Another cat's age is {0}".format(cat2.age)) + print("Its breed is {0}".format(cat2.breed)) + cat2.petTheCat() diff --git a/python/src/module2/M2_T1.py b/python/src/module2/M2_T1.py new file mode 100644 index 0000000..f2ae605 --- /dev/null +++ b/python/src/module2/M2_T1.py @@ -0,0 +1,32 @@ +def printArray(arr): + for i in range (len(arr)): + if (i == len(arr) - 1): + print(str(arr[i])) + return + print(str(arr[i]) + " ", end="") + +def checkIfSorted(arr): + for i in range(len(arr) - 1): + if (arr[i] > arr[i + 1]): + return False + return True + +def makeSortingIteration(arr, endIndex): + for i in range(endIndex): + if (arr[i] > arr[i + 1]): + proc = arr[i] + arr[i] = arr[i + 1] + arr[i + 1] = proc + printArray(arr) + +l = int(input()) +s = input().split() +iA = [] +for i in range(len(s)): + iA.append(int(s[i])) + +if (checkIfSorted(iA)): + print(0) +else: + for i in range(len(iA)): + makeSortingIteration(iA, len(iA) - i - 1) \ No newline at end of file diff --git a/python/src/module2/M2_T2.py b/python/src/module2/M2_T2.py new file mode 100644 index 0000000..0f742ab --- /dev/null +++ b/python/src/module2/M2_T2.py @@ -0,0 +1,36 @@ +def printArray(arr): + for i in range(len(arr)): + print(str(arr[i][0]) + " " + str(arr[i][1])) + +def checkIfSortedByElement(arr, elementIndex, reverseMultiplier): + for i in range(len(arr) - 1): + if (arr[i][elementIndex] * reverseMultiplier < arr[i + 1][elementIndex] * reverseMultiplier): + return False + return True + +def makeSortingIterationByElement(arr, elementIndex, reverseMultiplier, endIndex): + for i in range(endIndex): + if (arr[i][elementIndex] * reverseMultiplier < arr[i + 1][elementIndex] * reverseMultiplier): + proc = arr[i] + arr[i] = arr[i + 1] + arr[i + 1] = proc + +l = int(input()) +s = [] +for i in range(l): + inp = input().split() + s.append(inp) + +iA = [] +for i in range(len(s)): + iA.append([]) + for j in range(len(s[i])): + iA[i].append(int(s[i][j])) + +while (not(checkIfSortedByElement(iA, 0, -1))): + makeSortingIterationByElement(iA, 0, -1, len(iA) - 1) + +for i in range(len(iA) - 1): + makeSortingIterationByElement(iA, 1, 1, len(iA) - i - 1) + +printArray(iA) \ No newline at end of file diff --git a/python/src/module2/M2_T3.py b/python/src/module2/M2_T3.py new file mode 100644 index 0000000..210aba2 --- /dev/null +++ b/python/src/module2/M2_T3.py @@ -0,0 +1,36 @@ +def mergeArrays(firstHalf, secondHalf, startIndex): + result = [] + curIndex1 = 0 + curIndex2 = 0 + + for i in range(len(firstHalf) + len(secondHalf)): + if (curIndex2 == len(secondHalf) or (curIndex1 < len(firstHalf) and firstHalf[curIndex1] <= secondHalf[curIndex2])): + result.append(firstHalf[curIndex1]) + curIndex1 += 1 + else: + result.append(secondHalf[curIndex2]) + curIndex2 += 1 + + print(str(startIndex + 1) + " " + str(startIndex + len(result)) + " " + str(result[0]) + " " + str(result[len(result) - 1])) + + return result + +def mergeSort(inputArray, startIndex, endIndex): + if (endIndex - startIndex == 1): + return [inputArray[startIndex]] + + middleIndex = int((startIndex + endIndex) / 2) + firstHalf = mergeSort(inputArray, startIndex, middleIndex) + secondHalf = mergeSort(inputArray, middleIndex, endIndex) + + return mergeArrays(firstHalf, secondHalf, startIndex) + +l = int(input()) +s = input().split() +iA = [] +for i in range(l): + iA.append(int(s[i])) + +iA = mergeSort(iA, 0, len(iA)) + +print(" ".join(map(str, iA))) \ No newline at end of file diff --git a/python/src/module2/M2_T4.py b/python/src/module2/M2_T4.py new file mode 100644 index 0000000..7ba28c6 --- /dev/null +++ b/python/src/module2/M2_T4.py @@ -0,0 +1,38 @@ +def mergeArrays(firstHalf, secondHalf, startIndex, count): + resList = [] + curIndex1 = 0 + curIndex2 = 0 + + for i in range(len(firstHalf) + len(secondHalf)): + if (curIndex2 == len(secondHalf) or (curIndex1 < len(firstHalf) and firstHalf[curIndex1] <= secondHalf[curIndex2])): + resList.append(firstHalf[curIndex1]) + curIndex1 += 1 + else: + resList.append(secondHalf[curIndex2]) + curIndex2 += 1 + count += len(firstHalf) - curIndex1 + + return [resList, count] + +def mergeSort(inputArray, startIndex, endIndex): + if (endIndex - startIndex == 1): + return [[inputArray[startIndex]], 0] + + middleIndex = int((startIndex + endIndex) / 2) + ret1 = mergeSort(inputArray, startIndex, middleIndex) + ret2 = mergeSort(inputArray, middleIndex, endIndex) + firstHalf = ret1[0] + secondHalf = ret2[0] + count = ret1[1] + ret2[1] + + return mergeArrays(firstHalf, secondHalf, startIndex, count) + +l = int(input()) +s = input().split() +iA = [] +for i in range(l): + iA.append(int(s[i])) + +iA = mergeSort(iA, 0, len(iA)) + +print(iA[1]) \ No newline at end of file diff --git a/python/src/module2/M2_T5.py b/python/src/module2/M2_T5.py new file mode 100644 index 0000000..4c44d86 --- /dev/null +++ b/python/src/module2/M2_T5.py @@ -0,0 +1,52 @@ +def part(arr, startIndex, endIndex): + firstIndex = startIndex + secondIndex = endIndex - 1 + anchorIndex = int((startIndex + endIndex - 1) / 2) + anchorElement = arr[anchorIndex] + + while(firstIndex <= secondIndex): + while(arr[firstIndex] < anchorElement): + firstIndex += 1 + + while(arr[secondIndex] > anchorElement): + secondIndex -= 1 + + if(firstIndex >= secondIndex): + break + + arr[firstIndex], arr[secondIndex] = arr[secondIndex], arr[firstIndex] + + firstIndex += 1 + secondIndex -= 1 + + return secondIndex + 1 + + +def quickSort(arr, startIndex, endIndex): + + if(endIndex - startIndex <= 1): + return + + anchorIndex = part(arr, startIndex, endIndex) + + quickSort(arr, startIndex, anchorIndex) + quickSort(arr, anchorIndex, endIndex) + +def countDifferentNumbers(arr): + res = len(arr) + + for i in range(len(arr) - 1): + if(arr[i] == arr[i + 1]): + res -= 1 + + return res + +l = int(input()) +s = input().split() +iA = [] +for i in range(l): + iA.append(int(s[i])) + +quickSort(iA, 0, len(iA)) + +print(countDifferentNumbers(iA)) \ No newline at end of file diff --git a/python/src/module2/M2_T6.py b/python/src/module2/M2_T6.py new file mode 100644 index 0000000..acc922d --- /dev/null +++ b/python/src/module2/M2_T6.py @@ -0,0 +1,49 @@ +def findMax(arr): + max = -1 + for i in range(len(arr)): + if(arr[i] > max): + max = arr[i] + return max + +def countSort(arr, maxNum): + countArr = [] + countArr = countArr + [0] * (maxNum + 1) + for i in range(len(arr)): + countArr[arr[i] - 1] += 1 + + return countArr + +def processOrders(productCountList, ordersCountList): + result = [] + for i in range(len(ordersCountList)): + result.append(checkIfInStock(productCountList, ordersCountList, i)) + return result + + +def checkIfInStock(productCountList, ordersCountList, productKind): + while(ordersCountList[productKind] > 0): + if(productCountList[productKind] == 0): + return "yes" + productCountList[productKind] -= 1 + ordersCountList[productKind] -= 1 + return "no" + + +productKindNum = int(input()) +proc = input().split() +productCountList = [] + + +for i in range(productKindNum): + productCountList.append(int(proc[i])) +orderNum = int(input()) + +proc = input().split() +orderList = [] + +for i in range(len(proc)): + orderList.append(int(proc[i])) + +result = processOrders(productCountList, countSort(orderList, findMax(orderList) - 1)) +for i in range(len(result)): + print(result[i]) \ No newline at end of file diff --git a/python/src/module2/M2_T7.py b/python/src/module2/M2_T7.py new file mode 100644 index 0000000..b86c87b --- /dev/null +++ b/python/src/module2/M2_T7.py @@ -0,0 +1,48 @@ +def clearProc(proc, alphabetLength): + for i in range(alphabetLength): + proc[i] = [] + +def radixSort(arr, elementLength, alphabetLength): + proc = [] + result = [] + + for i in range(alphabetLength): + proc.append([]) + + for i in range(elementLength - 1, -1, -1): + for j in range(len(arr)): + proc[int(arr[j][i])].append(arr[j]) + + count = 0 + for j in range(len(proc)): + for k in range(len(proc[j])): + arr[count] = proc[j][k] + count += 1 + printPhase(proc, elementLength - i) + clearProc(proc, alphabetLength) + +def printPhase(arr, phaseCount): + print("Phase " + str(phaseCount)) + for i in range(len(arr)): + value = "" + if(len(arr[i]) == 0): + value = "empty" + else: + value = ", ".join(arr[i]) + print("Bucket " + str(i) + ": " + value) + print("*" * 10) + +stringCount = int(input()) +stringList = [] + +for i in range(stringCount): + stringList.append(input()) + +print("Initial array:") +print(", ".join(stringList)) +print("*" * 10) + +radixSort(stringList, len(stringList[0]), 10) + +print("Sorted array:") +print(", ".join(stringList))