From e30c4cf8bd5576849e94151e55d6110cf05f9425 Mon Sep 17 00:00:00 2001 From: Ilya Ryabokon Date: Fri, 6 Mar 2026 23:31:57 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9112-514=20=D0=A0=D1=8F=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=8C=20=D0=98=D0=BB=D1=8C=D1=8F:=20=D0=B2?= =?UTF-8?q?=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D1=8B=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=201,2,3=20=D0=B8=D0=B7=20l?= =?UTF-8?q?esson=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/sem02/lesson03/task1.py | 22 ++++++++++++++++++---- solutions/sem02/lesson03/task2.py | 22 ++++++++++++++++++++-- solutions/sem02/lesson03/task3.py | 16 +++++++++++++++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/solutions/sem02/lesson03/task1.py b/solutions/sem02/lesson03/task1.py index 2c3fc0b5..41f9b9d9 100644 --- a/solutions/sem02/lesson03/task1.py +++ b/solutions/sem02/lesson03/task1.py @@ -8,13 +8,27 @@ class ShapeMismatchError(Exception): def sum_arrays_vectorized( lhs: np.ndarray, rhs: np.ndarray, -) -> np.ndarray: ... +) -> np.ndarray: + if lhs.shape != rhs.shape: + raise ShapeMismatchError + return lhs + rhs - -def compute_poly_vectorized(abscissa: np.ndarray) -> np.ndarray: ... +def compute_poly_vectorized(abscissa: np.ndarray) -> np.ndarray: + return 3 * (abscissa ** 2) + 2 * abscissa + 1 def get_mutual_l2_distances_vectorized( lhs: np.ndarray, rhs: np.ndarray, -) -> np.ndarray: ... +) -> np.ndarray: + if lhs.sahpe[1] != rhs.shape[1]: + raise ShapeMismatchError + + lhsn = np.sum(lhs ** 2, axis = 1, keepdims=True) + rhsn = np.sum(rhs ** 2, axis = 1, keepdims=True) + + inter_of_matrices = lhs @ rhs.T + + distances = np.sqrt(lhsn + rhsn.T - 2 * inter_of_matrices) + + return distances \ No newline at end of file diff --git a/solutions/sem02/lesson03/task2.py b/solutions/sem02/lesson03/task2.py index fc823c1d..b828f567 100644 --- a/solutions/sem02/lesson03/task2.py +++ b/solutions/sem02/lesson03/task2.py @@ -9,11 +9,29 @@ def convert_from_sphere( distances: np.ndarray, azimuth: np.ndarray, inclination: np.ndarray, -) -> tuple[np.ndarray, np.ndarray, np.ndarray]: ... +) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + + if not (distances.shape == azimuth.shape == inclination.shape): + raise ShapeMismatchError + + abcissa = distances * np.sin(inclination) * np.cos(azimuth) + ordinates = distances * np.sin(inclination) * np.sin(azimuth) + applicates = distances * np.cos(inclination) + + return (abcissa, ordinates, applicates) def convert_to_sphere( abscissa: np.ndarray, ordinates: np.ndarray, applicates: np.ndarray, -) -> tuple[np.ndarray, np.ndarray, np.ndarray]: ... +) -> tuple[np.ndarray, np.ndarray, np.ndarray]: + + if not (abscissa.shape == ordinates.shape == applicates.shape): + raise ShapeMismatchError + + distances = np.sqrt(abscissa ** 2 + ordinates ** 2 + applicates ** 2) + azimuth = np.arctan2(ordinates, abscissa) + inclination = np.arctan2(applicates, np.sqrt(abscissa ** 2 + ordinates ** 2)) + + return (distances, azimuth, inclination) diff --git a/solutions/sem02/lesson03/task3.py b/solutions/sem02/lesson03/task3.py index 477acd0c..b3c9005f 100644 --- a/solutions/sem02/lesson03/task3.py +++ b/solutions/sem02/lesson03/task3.py @@ -3,4 +3,18 @@ def get_extremum_indices( ordinates: np.ndarray, -) -> tuple[np.ndarray, np.ndarray]: ... +) -> tuple[np.ndarray, np.ndarray]: + if len(ordinates) < 3: + raise ValueError + + left = ordinates[:-2] + mid = ordinates[1:-1] + right = ordinates[2:] + + mins = (left > mid) & (mid < right) + maxes = (left < mid) & (mid > right) + + min_indices = np.where(mins)[0] + 1 + max_indices = np.where(maxes)[0] + 1 + + return (min_indices, max_indices) From 7df07c2233eade5f493b4c26678b7ab138ed8d29 Mon Sep 17 00:00:00 2001 From: Ilya Ryabokon Date: Fri, 13 Mar 2026 23:57:50 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9112-514=20=D0=A0=D1=8F=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=8C=20=D0=98=D0=BB=D1=8C=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- solutions/sem02/lesson03/task1.py | 2 +- solutions/sem02/lesson04/task1.py | 52 ++++++++++++++++++++++++++++--- solutions/sem02/lesson04/task2.py | 24 ++++++++++++-- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/solutions/sem02/lesson03/task1.py b/solutions/sem02/lesson03/task1.py index 41f9b9d9..bcffa173 100644 --- a/solutions/sem02/lesson03/task1.py +++ b/solutions/sem02/lesson03/task1.py @@ -21,7 +21,7 @@ def get_mutual_l2_distances_vectorized( lhs: np.ndarray, rhs: np.ndarray, ) -> np.ndarray: - if lhs.sahpe[1] != rhs.shape[1]: + if lhs.shape[1] != rhs.shape[1]: raise ShapeMismatchError lhsn = np.sum(lhs ** 2, axis = 1, keepdims=True) diff --git a/solutions/sem02/lesson04/task1.py b/solutions/sem02/lesson04/task1.py index 1b5526c1..2e7f306f 100644 --- a/solutions/sem02/lesson04/task1.py +++ b/solutions/sem02/lesson04/task1.py @@ -2,16 +2,60 @@ def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray: - # ваш код - return image + + if pad_size < 1: + raise ValueError + + if image.ndim == 2: + height, width = image.shape + padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size), dtype=image.dtype) + padded_image[pad_size:pad_size + height, pad_size:pad_size + width] = image + + elif image.ndim == 3: + height, width, channels = image.shape + padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size, channels), dtype=image.dtype) + padded_image[pad_size:pad_size + height, pad_size:pad_size + width, :] = image + + return padded_image def blur_image( image: np.ndarray, kernel_size: int, ) -> np.ndarray: - # ваш код - return image + + if kernel_size < 1 or kernel_size % 2 == 0: + raise ValueError + + pad_size = kernel_size // 2 + + if image.ndim == 2: + height, width = image.shape + padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size), dtype=image.dtype) + padded_image[pad_size:pad_size + height, pad_size:pad_size + width] = image + + result = np.zeros((height, width), dtype=image.dtype) + + for i in range(height): + for j in range(width): + window = padded_image[i:i+kernel_size, j:j+kernel_size] + result[i, j] = np.mean(window) + + + elif image.ndim == 3: + height, width, channels = image.shape + padded_image = np.zeros((height + 2 * pad_size, width + 2 * pad_size, channels), dtype=image.dtype) + padded_image[pad_size:pad_size + height, pad_size:pad_size + width, :] = image + + result = np.zeros((height, width, channels), dtype=image.dtype) + + for i in range(height): + for j in range(width): + for k in range(channels): + window = padded_image[i:i+kernel_size, j:j+kernel_size, k] + result[i, j, k] = np.mean(window) + + return result.astype(image.dtype) if __name__ == "__main__": diff --git a/solutions/sem02/lesson04/task2.py b/solutions/sem02/lesson04/task2.py index be9a2288..66a516ce 100644 --- a/solutions/sem02/lesson04/task2.py +++ b/solutions/sem02/lesson04/task2.py @@ -5,6 +5,26 @@ def get_dominant_color_info( image: np.ndarray[np.uint8], threshold: int = 5, ) -> tuple[np.uint8, float]: - # ваш код + if threshold < 1: + raise ValueError - return 0, 0 + pixels = image.flatten() + zerose = np.zeros(256, dtype=np.int64) + for color in range(256): + mask = pixels == color + zerose[color] = np.sum(mask) + + max_pixels = 0 + best_color = 0 + for current_color in range(256): + if zerose[current_color] != 0: + left = max(0, current_color - (threshold - 1)) + right = min(255, current_color + (threshold - 1)) + current_sum = np.sum(zerose[left : right + 1]) + + if current_sum > max_pixels: + max_pixels = current_sum + best_color = current_color + + percent = (max_pixels / pixels.size) * 100 + return np.uint8(best_color), float(percent) \ No newline at end of file