diff --git a/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_1.py b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_1.py new file mode 100644 index 0000000..8807cf6 --- /dev/null +++ b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_1.py @@ -0,0 +1,77 @@ +class Picture: + def __init__(self, colums, lines, matrix): + self.colums = colums + self.lines = lines + self.matrix = matrix + + def check_matrix(self): + for i in range(0, self.colums): + for y in range(0, self.lines): + if self.matrix[i][y] < 0 or self.matrix[i][y] > 255: + return False + return True + +def check_element(matrix, x, y, colums, lines, sum, flag): + if matrix[x][y] == 0: + return [[0], [0], 0] + + random_point = matrix[x][y] + sum[0] = sum[0] + matrix[x][y] + flag[0] = flag[0] + 1 + matrix[x][y] = 0 + + if x < (colums - 1): + check_element(matrix, x + 1, y, colums, lines, sum, flag) + + if x < (colums - 1) and y < (lines - 1): + check_element(matrix, x + 1, y + 1, colums, lines, sum, flag) + + if y < (lines - 1): + check_element(matrix, x, y + 1, colums, lines, sum, flag)[0] + + + return [sum, flag, random_point] + +def avg_brightness(picture): + working_matrix = picture.matrix + avgs = [] + avgs_index = 0 + + + for i in range(0, picture.colums): + for y in range(0, picture.lines): + sum = [0] + flag = [0] + result = [0, 0] + result = check_element(working_matrix, i, y, picture.colums, picture.lines, sum, flag) + sum = result[0] + flag = result[1] + + if sum[0] > 0: + print(f'Random point in area {avgs_index + 1} : {result[2]} \n') + avgs.insert(avgs_index, sum[0] / flag[0]) + avgs_index = avgs_index + 1 + + avgs.sort(reverse = True) + + return avgs + +matrix = [[2, 2, 0, 5, 3], + [0, 0, 0, 3, 5], + [0, 0, 0, 0, 0], + [0, 1, 2, 3, 0], + [0, 0, 0, 0, 0]] + +p1 = Picture(5, 5, matrix) + +if p1.check_matrix() is False: + print("Invalid matrix numbers!") +else: + average_p1 = avg_brightness(p1) + + print("\nAverage brightness of areas:\n") + + print(average_p1) + + for i in range (0, len(average_p1)): + print(f'\nArea {i + 1} : {average_p1[i]}\n') \ No newline at end of file diff --git a/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_2.py b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_2.py new file mode 100644 index 0000000..42cbc65 --- /dev/null +++ b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_2.py @@ -0,0 +1,19 @@ +def num_ways(N): + first_num = 0 + second_num = 1 + ways = 0 + + for i in range(1, (N + 1)): + ways = first_num + second_num + first_num = second_num + second_num = ways + + return ways + +stairs1 = 5 +ways_for_stairs1 = num_ways(stairs1) +print(f'{stairs1} steps could be walked by {ways_for_stairs1} ways.\n') + +stairs2 = 15 +ways_for_stairs2 = num_ways(stairs2) +print(f'{stairs2} steps could be walked by {ways_for_stairs2} ways.\n') \ No newline at end of file diff --git a/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_3.py b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_3.py new file mode 100644 index 0000000..c27c320 --- /dev/null +++ b/homeworks/04_Alexandra_Stancheva/alexandra_04_homework_2/task_3.py @@ -0,0 +1,21 @@ + +def replace(current_list, find, replacement): + for i in range(0, len(current_list)): + if isinstance(current_list[i], list): + replace(current_list[i], find, replacement) + + if type(current_list[i]) is tuple: + new_list = list(current_list[i]) + replace(new_list, find, replacement) + current_list[i] = tuple(new_list) + + if current_list[i] == find: + current_list[i] = replacement + + + +my_list = [ 'a', 1, [ ['a', 'b'], 1], ([1, 3, 'a'], 'b')] + +replace(my_list, 'a', 'c') + +print(my_list)