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

Add homework 2 and fix folder name #87

Open
wants to merge 7 commits into
base: master
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
51 changes: 51 additions & 0 deletions homeworks/18_Lachezar_Velinov/02_task1.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions homeworks/18_Lachezar_Velinov/02_task2.py
Original file line number Diff line number Diff line change
@@ -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))
16 changes: 16 additions & 0 deletions homeworks/18_Lachezar_Velinov/02_task3.py
Original file line number Diff line number Diff line change
@@ -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)