diff --git a/homeworks/19_Lachezar_Velinov/01_checkCircles.py b/homeworks/18_Lachezar_Velinov/01_checkCircles.py similarity index 100% rename from homeworks/19_Lachezar_Velinov/01_checkCircles.py rename to homeworks/18_Lachezar_Velinov/01_checkCircles.py diff --git a/homeworks/18_Lachezar_Velinov/02_task1.py b/homeworks/18_Lachezar_Velinov/02_task1.py new file mode 100644 index 0000000..de530ee --- /dev/null +++ b/homeworks/18_Lachezar_Velinov/02_task1.py @@ -0,0 +1,51 @@ + + +def getSectorValues(matrix, rowpos, columnpos, sectorList): #finds all the values of a sector and returns them + sectorList.append(matrix[rowpos][columnpos]) + matrix[rowpos][columnpos] = 0 #turns an appended square into a black square + for i in range(-1, 2): + for j in range(-1, 2): + if rowpos + i >= 0 and columnpos + j >= 0 and rowpos + i < len(matrix) and columnpos + j < len(matrix[i]): #check if location is in range of the matrix + if matrix[rowpos+i][columnpos+j] != 0: #check if square is non black + getSectorValues(matrix, rowpos+i, columnpos+j, sectorList) #reccursive calling + return + + +def findSector(matrix): #looks for a sector and returns it's average value + returnValueList = [] #holds average value of every sector + sectorList = [] #holds all the values of a single sector + for rowpos, row in enumerate(matrix): + for columnpos, column in enumerate(row): #go throught the matrix to find a non black square + if matrix[rowpos][columnpos] > 0: + getSectorValues(matrix, rowpos, columnpos, sectorList) + average = 0 #average value of a sector + for amount in sectorList: + average+=amount + average /= len(sectorList) + del sectorList[:] #reset the sector list + returnValueList.append(average) #add average sector value to rest list + + return returnValueList + + +def avg_brightnes(matrix): + outputList = [] + outPutList = findSector(matrixCopy) + outPutList.sort(reverse=True) #sort list in descending order + for i in outPutList: #prints final values + print (i) + + + + +matrix = ( #test matrix + (170, 0, 0, 255, 221, 0), + ( 68, 0, 17, 0, 0, 68), + (221, 0, 238, 136, 0, 255), + ( 0, 0, 85, 0, 136, 238), + (238, 17, 0, 68, 0, 255), + ( 85, 170, 0, 221, 17, 0) +) + +matrixCopy = [list(matrix_inner) for matrix_inner in matrix] #since we cannot use the original matrix, we make a copy +avg_brightnes(matrixCopy) diff --git a/homeworks/18_Lachezar_Velinov/02_task2.py b/homeworks/18_Lachezar_Velinov/02_task2.py new file mode 100644 index 0000000..bc4afe0 --- /dev/null +++ b/homeworks/18_Lachezar_Velinov/02_task2.py @@ -0,0 +1,11 @@ +from functools import lru_cache + + +@lru_cache(maxsize=10) #allows us to use 10 most recent values by caching them +def num_ways(n): + if n <= 1: + return n + return num_ways(n-1) + num_ways(n-2) + + +print(num_ways(23)) diff --git a/homeworks/18_Lachezar_Velinov/02_task3.py b/homeworks/18_Lachezar_Velinov/02_task3.py new file mode 100644 index 0000000..5dc174e --- /dev/null +++ b/homeworks/18_Lachezar_Velinov/02_task3.py @@ -0,0 +1,16 @@ +def replace(List, Find, Replace): + listCopy = list(List) + + for i, value in enumerate(listCopy): #go through list + if value == Find: + listCopy[i] = Replace #looks for value and replaces it + + elif type(value) in [list, tuple]: + listCopy[i] = replace(listCopy[i], Find, Replace) #if container is found, goes inside it + + return listCopy + + +exampleList = [ 'a', 1, [ ['a', 'b'], 1], ([1, 3, 'a'], 'b')] +res = replace(exampleList, 'a', 'c') +print(res)