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

Added Lab 6 #3

Open
wants to merge 6 commits into
base: Aksenov_Ivan_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
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.12"
33 changes: 33 additions & 0 deletions python/src/cat.py
Original file line number Diff line number Diff line change
@@ -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))
22 changes: 17 additions & 5 deletions python/src/main.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions python/src/module2/M2_T1.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions python/src/module2/M2_T2.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions python/src/module2/M2_T3.py
Original file line number Diff line number Diff line change
@@ -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)))
38 changes: 38 additions & 0 deletions python/src/module2/M2_T4.py
Original file line number Diff line number Diff line change
@@ -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])
52 changes: 52 additions & 0 deletions python/src/module2/M2_T5.py
Original file line number Diff line number Diff line change
@@ -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))
49 changes: 49 additions & 0 deletions python/src/module2/M2_T6.py
Original file line number Diff line number Diff line change
@@ -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])
48 changes: 48 additions & 0 deletions python/src/module2/M2_T7.py
Original file line number Diff line number Diff line change
@@ -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))