diff --git a/homeworks/25_Stanislav_Ivanov/01_check_circles.py b/homeworks/24_Stanislav_Ivanov/01_check_circles.py similarity index 100% rename from homeworks/25_Stanislav_Ivanov/01_check_circles.py rename to homeworks/24_Stanislav_Ivanov/01_check_circles.py diff --git a/homeworks/24_Stanislav_Ivanov/02_avg_brightness.py b/homeworks/24_Stanislav_Ivanov/02_avg_brightness.py new file mode 100644 index 0000000..5ff9dab --- /dev/null +++ b/homeworks/24_Stanislav_Ivanov/02_avg_brightness.py @@ -0,0 +1,42 @@ +def calculate_area(lst: list[list[int]], row: int, col: int) -> tuple[int, int]: + if row not in range(len(lst)) or col not in range(len(lst)): + return 0, 0 + if lst[row][col] == 0: + return 0, 0 + + sum_ = lst[row][col] + lst[row][col] = 0 + pixels = 1 + + for i in range(row-1, row+2): + for j in range(col-1, col+2): + if i == row and j == col: + continue + pixels_to_add, sum_to_add = calculate_area(lst, i, j) + pixels += pixels_to_add + sum_ += sum_to_add + + return pixels, sum_ + + +def avg_brightness(given_list: list) -> None: + lst = given_list.copy() + for index_row, row in enumerate(lst): + for index_col, element in enumerate(row): + if element == 0: + continue + + pixels, sum_ = calculate_area(lst, index_row, index_col) + print(f"({index_row}, {index_col}) {(sum_/pixels):.2f}") + + +if __name__ == "__main__": + test_list = [ + [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] + ] + avg_brightness(test_list) diff --git a/homeworks/24_Stanislav_Ivanov/03_ladder.py b/homeworks/24_Stanislav_Ivanov/03_ladder.py new file mode 100644 index 0000000..826ff0b --- /dev/null +++ b/homeworks/24_Stanislav_Ivanov/03_ladder.py @@ -0,0 +1,18 @@ +def num_ways(steps: int) -> int: + if type(steps) is not int: + raise TypeError("n must be an integer") + + if steps <= 2: + return max(0, steps) + return num_ways(steps-1) + num_ways(steps-2) + + +if __name__ == "__main__": + print(num_ways(2)) + print(num_ways(3)) + print(num_ways(4)) + print(num_ways(-3)) + try: + print(num_ways("5")) + except TypeError as error: + print(error) diff --git a/homeworks/24_Stanislav_Ivanov/04_replace.py b/homeworks/24_Stanislav_Ivanov/04_replace.py new file mode 100644 index 0000000..56ecb86 --- /dev/null +++ b/homeworks/24_Stanislav_Ivanov/04_replace.py @@ -0,0 +1,20 @@ +from typing import Any + + +def replace(element: Any, find: str, replace_: str) -> Any: + if element == find: + return replace_ + element_type = type(element) + try: + if type(element) is str: + return element + element = list(element) + for index in range(len(element)): + element[index] = replace(element[index], find, replace_) + finally: + return element_type(element) + + +if __name__ == "__main__": + test_list = [1, "2", 3, "45", "7", [1, "2"], (1, ["2", "34"], 3, "2")] + print(replace(test_list, "2", "99"))