From 41dcddd9ad0e66ea9f01208c6636cca6d2da91d9 Mon Sep 17 00:00:00 2001 From: Yassen Efremov <48584741+YassenEfremov@users.noreply.github.com> Date: Thu, 30 Sep 2021 11:06:27 +0300 Subject: [PATCH 1/4] Update README.md Add name, github profile link and candy --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2313615..68bfd5e 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,4 @@ |Номер| Име | Github | Бонбони | Отсъствия | |-----|-----|--------|---------|-----------| |5| Владимир Алексиев | [valeksiev](https://github.com/valeksiev) | 🍬 | | +| 27 |Ясен Ефремов | https://github.com/YassenEfremov | :candy: | | From 3f9e418f96d9f6a10090b8e2f3ad43196ee72f40 Mon Sep 17 00:00:00 2001 From: Yassen Efremov <48584741+YassenEfremov@users.noreply.github.com> Date: Thu, 30 Sep 2021 22:00:35 +0300 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 68bfd5e..1c8aa16 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ |Номер| Име | Github | Бонбони | Отсъствия | |-----|-----|--------|---------|-----------| |5| Владимир Алексиев | [valeksiev](https://github.com/valeksiev) | 🍬 | | -| 27 |Ясен Ефремов | https://github.com/YassenEfremov | :candy: | | +| 27 |Ясен Ефремов | [yassen](https://github.com/YassenEfremov) | :candy: | | From c4984a3c67a9c76d56434c6fc2624c650639b5de Mon Sep 17 00:00:00 2001 From: Yassen Efremov <48584741+YassenEfremov@users.noreply.github.com> Date: Fri, 15 Oct 2021 23:49:54 +0300 Subject: [PATCH 3/4] Upload homework - check circles --- .../27_Yassen_Efremov/01_check_circles.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 homeworks/27_Yassen_Efremov/01_check_circles.py diff --git a/homeworks/27_Yassen_Efremov/01_check_circles.py b/homeworks/27_Yassen_Efremov/01_check_circles.py new file mode 100644 index 0000000..a07cd19 --- /dev/null +++ b/homeworks/27_Yassen_Efremov/01_check_circles.py @@ -0,0 +1,34 @@ +import math + +# A function that returns information about two given circles in a cartesian coordinate system +def checkCircles(c1_center, c1_radius, c2_center, c2_radius): + + # Calculate the distance here because we use it a lot later + center_distance = math.sqrt((c2_center[0] - c1_center[0])**2 + (c2_center[1] - c1_center[1])**2) + + # No common + if center_distance > c1_radius + c2_radius: + return "no common" + + elif center_distance < c1_radius + c2_radius: + # Matching + if not center_distance and c1_radius == c2_radius: return "matching" + # Touching (from the inside) + if c1_radius == center_distance + c2_radius or c2_radius == center_distance + c1_radius: return "touching" + # Containing + if c1_radius > center_distance + c2_radius: return "c1 contains c2" + if c2_radius > center_distance + c1_radius: return "c2 contains c1" + # Intersecting + return "intersecting" + + else: # Touching + return "touching" + + + +# Example of two intersecting circles +print(checkCircles([0, 1], 2, [2, 1], 3)) +# Example of two touching circles +print(checkCircles([-2, 0], 2, [1, 0], 1)) +# Example of two containing circles +print(checkCircles([0, 2], 4, [2, 0], 1)) From 7a3e1c816d38f98e923f812d614997a883d7f1bb Mon Sep 17 00:00:00 2001 From: Yassen Efremov <48584741+YassenEfremov@users.noreply.github.com> Date: Thu, 13 Jan 2022 01:05:24 +0200 Subject: [PATCH 4/4] Upload homework - the 3 tasks --- homeworks/27_Yassen_Efremov/avg_brightness.py | 57 +++++++++++++++++++ homeworks/27_Yassen_Efremov/num_ways.py | 46 +++++++++++++++ homeworks/27_Yassen_Efremov/replace.py | 34 +++++++++++ 3 files changed, 137 insertions(+) create mode 100644 homeworks/27_Yassen_Efremov/avg_brightness.py create mode 100644 homeworks/27_Yassen_Efremov/num_ways.py create mode 100644 homeworks/27_Yassen_Efremov/replace.py diff --git a/homeworks/27_Yassen_Efremov/avg_brightness.py b/homeworks/27_Yassen_Efremov/avg_brightness.py new file mode 100644 index 0000000..8924d79 --- /dev/null +++ b/homeworks/27_Yassen_Efremov/avg_brightness.py @@ -0,0 +1,57 @@ +# =============================================================================================== # +# Classes, Functions, Global variables # + + +def avg_recursive(matrix, cell_row, cell_col, avg_region_brightness): + avg_region_brightness.append(matrix[cell_row][cell_col]) # add the brightness of the current cell + matrix[cell_row][cell_col] = 0 # Mark the cell as visited + # Start calling the function recursively on all the neighboor cells + for i in range(-1, 2): + for j in range(-1, 2): + # Check if the next indexes are out of range + if (0 <= cell_row + i < len(matrix)) and (0 <= cell_col + j < len(matrix[0])): + # Check if the next cell has brightness 0 or is the current cell + if (matrix[cell_row + i][cell_col + j] != 0) and ([i, j] != [0, 0]): + avg_recursive(matrix, cell_row + i, cell_col + j, avg_region_brightness) + + +def avg_brightness(matrix): + matrix_copy = [list(matrix_inner) for matrix_inner in matrix] # copy so that we don't modify the passed list + avg_brightnesses = [] + + # Look for cells with brightness above 0 and start recursively going through them + for i in range(0, len(matrix_copy)): + for j in range(0, len(matrix_copy[0])): + if matrix_copy[i][j] > 0: + avg_region_brightness = [] + avg_recursive(matrix_copy, i, j, avg_region_brightness) + avg_brightnesses.append(avg_region_brightness) + + avg_brightnesses.sort(key = lambda row: sum(row) / len(row), reverse=True) + for row in avg_brightnesses: + print(sum(row) / len(row)) + + +# =============================================================================================== # + + +def main(): + + # Example + 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) + ) + + avg_brightness(matrix) + + +# =============================================================================================== # + + +if __name__ == "__main__": + main() diff --git a/homeworks/27_Yassen_Efremov/num_ways.py b/homeworks/27_Yassen_Efremov/num_ways.py new file mode 100644 index 0000000..03c5d3b --- /dev/null +++ b/homeworks/27_Yassen_Efremov/num_ways.py @@ -0,0 +1,46 @@ +import time + + +# =============================================================================================== # +# Classes, Functions, Global variables # + + +def num_ways(N, found): + # Optimized with memoization + + if N in found: + return found[N] + + if N <= 1: return 1 + + num = num_ways(N - 1, found) + num_ways(N - 2, found) + found[N] = num + + return num + + + +# =============================================================================================== # + + +def main(): + + start_time = time.time() + dict = {} + + # Examples + print("Ways to climb a ladder with {} steps: {}".format(2, num_ways(2, dict))) + print("Ways to climb a ladder with {} steps: {}".format(3, num_ways(3, dict))) + print("Ways to climb a ladder with {} steps: {}".format(5, num_ways(5, dict))) + print("Ways to climb a ladder with {} steps: {}".format(7, num_ways(7, dict))) + + # print("Ways to climb a ladder with {} steps: {}".format(100, num_ways(200, dict))) + + print("\n### Completed after {:.6f} seconds ###".format(time.time() - start_time)) + + +# =============================================================================================== # + + +if __name__ == "__main__": + main() diff --git a/homeworks/27_Yassen_Efremov/replace.py b/homeworks/27_Yassen_Efremov/replace.py new file mode 100644 index 0000000..1b6dc7f --- /dev/null +++ b/homeworks/27_Yassen_Efremov/replace.py @@ -0,0 +1,34 @@ +# =============================================================================================== # +# Classes, Functions, Global variables # + + +def replace(a_list, to_find, replace_with): + list_copy = list(a_list) # copy so that we don't modify the passed list + + for i, value in enumerate(list_copy): + if value == to_find: + # Found a value to replace + list_copy[i] = replace_with + elif type(value) in [list, tuple]: + # Found another collection => iterate through it recursively + list_copy[i] = replace(list_copy[i], to_find, replace_with) + + return list_copy + + +# =============================================================================================== # + + +def main(): + + # Examples + list = [ 'a', 1, [['a', 'b'], 1], ([1, 3, 'a'], 'b')] + res = replace(list, 'a', 'c') + print(res) # => [ 'c', 1, [ ['c', 'b'], 1], ([1, 3, 'c'], 'b')] + + +# =============================================================================================== # + + +if __name__ == "__main__": + main()