Skip to content
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
67 changes: 67 additions & 0 deletions homeworks/26_Yavor_Pachedjiev/second_homework/zad1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

def get_neighbours(index, matrix):
offsets = [[-1,-1], [-1,0], [-1,1], [0,1], [1,1], [1,0], [1,-1], [0,-1]]

neighbours = []
neighbour = []
for offset in offsets:
neighbour = [x + y for x, y in zip(offset, index)]
if (neighbour[0] >= 0 and neighbour[0] < len(matrix)) and (neighbour[1] >= 0 and neighbour[1] < len(matrix[neighbour[0]])):
if matrix[neighbour[0]][neighbour[1]] != 0:
neighbours.append(neighbour)
return neighbours #returns index


def calculate_sum(matrix, index, has_accessed=[]):
brightness = matrix[index[0]][index[1]]
has_accessed.append(index)

neighbours = get_neighbours(index, matrix)
neighbours = [neighbour for neighbour in neighbours if neighbour not in has_accessed]

# print(index, ':', neighbours)

if(len(neighbours) == 0):
return brightness

for neighbour in neighbours:
if neighbour not in has_accessed:
brightness += calculate_sum(matrix, neighbour, has_accessed)
return brightness





def avg_brightness(matrix):
has_accessed = []
sum = 0
old_len = 0
cells = 0

for i, row in enumerate(matrix):
for j, elem in enumerate(row):
if matrix[i][j] == 0 or [i,j] in has_accessed:
continue
# neighbours = get_neighbours([i,j], matrix)
sum = calculate_sum(matrix, [i,j], has_accessed)
cells = len(has_accessed) - old_len

print("Sum:", sum)
print("Cells:", cells)
print("Average brightness:", sum/cells, "\n")
old_len += cells



matrix = [
[85, 0, 0, 108],
[0, 0, 127, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 7, 0],
[0, 0, 0, 0],
[0, 9, 0, 0],
[0, 0, 0, 0]
]
avg_brightness(matrix)
9 changes: 9 additions & 0 deletions homeworks/26_Yavor_Pachedjiev/second_homework/zad2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
# add mzlat
def num_ways(steps):
if steps <= 1:
return 1
return num_ways(steps-1) + num_ways(steps-2)

# sys.setrecursionlimit(1500)
print(num_ways(4))
29 changes: 29 additions & 0 deletions homeworks/26_Yavor_Pachedjiev/second_homework/zad3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def replaceFunc(the_list, find, replace):
if not isinstance(the_list, tuple) and not isinstance(the_list, list):
if isinstance(the_list, str):
if the_list == 'a':
return 'c'
else:
return the_list

return the_list

new_list = []

for i in range(len(the_list)):
item = the_list[i]
if isinstance(item, tuple):
new_list.append(tuple(replaceFunc(item, find, replace)))
else:
new_list.append(replaceFunc(item, find, replace))

return new_list


# tl = [2, 3, [2, 3, [7, 8], 2], (9,( 0,3, (4,5))), 1]
tl = ['a', 1, [ ['a', 'b'], 1], ([1, 3, 'a'], 'b')]

nl = replaceFunc(tl, 'a', 'c')

print(tl)
print(nl)