Skip to content
2 changes: 0 additions & 2 deletions homeworks/sem01/hw1/backoff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from random import uniform
from time import sleep
from typing import (
Callable,
ParamSpec,
Expand Down
5 changes: 1 addition & 4 deletions solutions/sem01/lesson02/task3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
def get_amount_of_ways_to_climb(stair_amount: int) -> int:
step_prev, step_curr = 1, 1
# ваш код
return step_curr
def get_amount_of_ways_to_climb(stair_amount: int) -> int: ...
2 changes: 1 addition & 1 deletion solutions/sem01/lesson03/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def flip_bits_in_range(num: int, left_bit: int, right_bit: int) -> int:
# ваш код
return num
return num
2 changes: 1 addition & 1 deletion solutions/sem01/lesson03/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def get_cube_root(n: float, eps: float) -> float:
# ваш код
return n
return n
2 changes: 1 addition & 1 deletion solutions/sem01/lesson04/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def is_arithmetic_progression(lst: list[list[int]]) -> bool:
# ваш код
return False
return False
2 changes: 1 addition & 1 deletion solutions/sem01/lesson04/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def merge_intervals(intervals: list[list[int, int]]) -> list[list[int, int]]:
# ваш код
return [[0,0]]
return [[0, 0]]
2 changes: 1 addition & 1 deletion solutions/sem01/lesson04/task4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def move_zeros_to_end(nums: list[int]) -> list[int]:
# ваш код
return 0
return 0
2 changes: 1 addition & 1 deletion solutions/sem01/lesson04/task5.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def find_row_with_most_ones(matrix: list[list[int]]) -> int:
# ваш код
return 0
return 0
4 changes: 2 additions & 2 deletions solutions/sem01/lesson04/task6.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def count_cycles(arr: list[int]) -> int:
def count_cycles(arr: list[int]) -> int:
# ваш код
return 0
return 0
2 changes: 1 addition & 1 deletion solutions/sem01/lesson05/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def is_palindrome(text: str) -> bool:
# ваш код
return False
return False
2 changes: 1 addition & 1 deletion solutions/sem01/lesson05/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def are_anagrams(word1: str, word2: str) -> bool:
# ваш код
return False
return False
2 changes: 1 addition & 1 deletion solutions/sem01/lesson05/task4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def unzip(compress_text: str) -> str:
# ваш код
return compress_text
return compress_text
4 changes: 2 additions & 2 deletions solutions/sem01/lesson05/task5.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def reg_validator(reg_expr: str, text: str) -> bool:
def reg_validator(reg_expr: str, text: str) -> bool:
# ваш код
return False
return False
2 changes: 1 addition & 1 deletion solutions/sem01/lesson05/task6.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def simplify_path(path: str) -> str:
# ваш код
return path
return path
2 changes: 1 addition & 1 deletion solutions/sem01/lesson06/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def int_to_roman(num: int) -> str:
# ваш код
return ""
return ""
2 changes: 1 addition & 1 deletion solutions/sem01/lesson06/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def get_len_of_longest_substring(text: str) -> int:
# ваш код
return 0
return 0
1 change: 0 additions & 1 deletion solutions/sem01/lesson06/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ def is_there_any_good_subarray(
nums: list[int],
k: int,
) -> bool:

# ваш код
return False
2 changes: 1 addition & 1 deletion solutions/sem01/lesson06/task4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def count_unique_words(text: str) -> int:
# ваш код
return 0
return 0
3 changes: 2 additions & 1 deletion solutions/sem01/lesson08/task1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Callable


def make_averager(accumulation_period: int) -> Callable[[float], float]:
# ваш код
pass
pass
8 changes: 3 additions & 5 deletions solutions/sem01/lesson08/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

T = TypeVar("T")

def collect_statistic(
statistics: dict[str, list[float, int]]
) -> Callable[[T], T]:


def collect_statistic(statistics: dict[str, list[float, int]]) -> Callable[[T], T]:
# ваш код
pass
pass
3 changes: 0 additions & 3 deletions solutions/sem01/lesson12/task3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import sys


class FileOut:
def __init__(
self,
Expand Down
16 changes: 13 additions & 3 deletions solutions/sem02/lesson03/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ 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.shape[1] != rhs.shape[1]:
raise ShapeMismatchError()

diff = lhs[:, np.newaxis] - rhs
return np.sqrt(np.sum(diff**2, axis=2))
20 changes: 18 additions & 2 deletions solutions/sem02/lesson03/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,27 @@ 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()

x = distances * np.sin(inclination) * np.cos(azimuth)
y = distances * np.sin(inclination) * np.sin(azimuth)
z = distances * np.cos(inclination)
return x, y, z


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.arccos(
np.divide(applicates, distances), where=distances != 0
) # Чтобы избегать деления на 0
return distances, azimuth, inclination
14 changes: 13 additions & 1 deletion solutions/sem02/lesson03/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@

def get_extremum_indices(
ordinates: np.ndarray,
) -> tuple[np.ndarray, np.ndarray]: ...
) -> tuple[np.ndarray, np.ndarray]:
if ordinates.shape[0] < 3:
raise ValueError

left = ordinates[:-2]
mid = ordinates[1:-1]
right = ordinates[2:]

minimums = (mid < left) & (mid < right)
maximums = (mid > left) & (mid > right)

indexes = np.arange(1, ordinates.shape[0] - 1)
return indexes[minimums], indexes[maximums]
27 changes: 23 additions & 4 deletions solutions/sem02/lesson04/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@


def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray:
# ваш код
return image
if pad_size < 1:
raise ValueError

h, w = image.shape[:2]
new_h, new_w = h + pad_size * 2, w + pad_size * 2
res_form = (new_h, new_w) if image.ndim == 2 else (new_h, new_w, image.shape[2])
result = np.zeros(res_form, dtype=image.dtype)
result[pad_size : pad_size + h, pad_size : pad_size + w, ...] = image
return result


def blur_image(
image: np.ndarray,
kernel_size: int,
) -> np.ndarray:
# ваш код
return image
if kernel_size < 1 or kernel_size % 2 == 0:
raise ValueError

if kernel_size == 1:
return image

padded_image = pad_image(image, kernel_size // 2)
h, w = image.shape[:2]
windows = [
padded_image[i : i + h, j : j + w, ...]
for j in range(kernel_size)
for i in range(kernel_size)
]
return np.mean(windows, axis=0).astype(image.dtype)


if __name__ == "__main__":
Expand Down
21 changes: 19 additions & 2 deletions solutions/sem02/lesson04/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ def get_dominant_color_info(
image: np.ndarray[np.uint8],
threshold: int = 5,
) -> tuple[np.uint8, float]:
# ваш код
if threshold < 1:
raise ValueError("threshold must be positive")

return 0, 0
pixels = image.flatten()
counts = np.zeros(256)
for color in range(256):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть np.unique, которая быстрее

counts[color] = np.sum(pixels == color)

result_color, result_count = 0, 0
for color in range(256):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше было бы 256 вынести в константу, чтобы было сразу понятно, что вы имеете ввиду количество цветов, и еще вы далее используете 255, что является производной от этого числа

if counts[color] == 0:
continue
min_with_threshold = max(0, color - (threshold - 1))
max_with_threshold = min(255, color + (threshold - 1))
count = np.sum(counts[min_with_threshold : max_with_threshold + 1])
if count > result_count:
result_count = count
result_color = color

return np.uint8(result_color), result_count / pixels.size * 100
8 changes: 7 additions & 1 deletion solutions/sem02/lesson05/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ def can_satisfy_demand(
costs: np.ndarray,
resource_amounts: np.ndarray,
demand_expected: np.ndarray,
) -> bool: ...
) -> bool:
if costs.shape[0] != resource_amounts.shape[0] or costs.shape[1] != demand_expected.shape[0]:
raise ShapeMismatchError

total_expected = costs @ demand_expected
mask = total_expected <= resource_amounts
return np.all(mask)
12 changes: 11 additions & 1 deletion solutions/sem02/lesson05/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ class ShapeMismatchError(Exception):
def get_projections_components(
matrix: np.ndarray,
vector: np.ndarray,
) -> tuple[np.ndarray | None, np.ndarray | None]: ...
) -> tuple[np.ndarray | None, np.ndarray | None]:
if matrix.shape[0] != matrix.shape[1] or matrix.shape[0] != vector.shape[0]:
raise ShapeMismatchError

if np.linalg.det(matrix) == 0:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Во-первых, как вы можете помнить из первого семестра, делать == между 2 числами float - это неправильно.
Во-вторых, использование детерменанта для определения сингулярности матрицы - это ошибка, так как при вычислении детерменанта из-за компьютерных ошибок вы можете получить любое число.

return None, None

scalar_result = matrix @ vector
lengths = np.sum(matrix**2, axis=1)
projections = (scalar_result / lengths)[:, np.newaxis] * matrix
return projections, vector - projections
9 changes: 8 additions & 1 deletion solutions/sem02/lesson05/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ def adaptive_filter(
Vs: np.ndarray,
Vj: np.ndarray,
diag_A: np.ndarray,
) -> np.ndarray: ...
) -> np.ndarray:
if Vs.shape[0] != Vj.shape[0] or Vj.shape[1] != diag_A.shape[0]:
raise ShapeMismatchError

Vj_h = np.transpose(np.conj(Vj))
A = np.diag(diag_A)
y = Vs - Vj @ np.linalg.inv(np.eye(Vj.shape[1]) + Vj_h @ Vj @ A) @ (Vj_h @ Vs)
return y
Loading
Loading