diff --git a/homeworks/26_Yavor_Pachedjiev/second_homework/zad1.py b/homeworks/26_Yavor_Pachedjiev/second_homework/zad1.py new file mode 100644 index 0000000..9e60ef1 --- /dev/null +++ b/homeworks/26_Yavor_Pachedjiev/second_homework/zad1.py @@ -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) \ No newline at end of file diff --git a/homeworks/26_Yavor_Pachedjiev/second_homework/zad2.py b/homeworks/26_Yavor_Pachedjiev/second_homework/zad2.py new file mode 100644 index 0000000..b0f12ad --- /dev/null +++ b/homeworks/26_Yavor_Pachedjiev/second_homework/zad2.py @@ -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)) diff --git a/homeworks/26_Yavor_Pachedjiev/second_homework/zad3.py b/homeworks/26_Yavor_Pachedjiev/second_homework/zad3.py new file mode 100644 index 0000000..b73475d --- /dev/null +++ b/homeworks/26_Yavor_Pachedjiev/second_homework/zad3.py @@ -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) \ No newline at end of file