Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 14 additions & 3 deletions solutions/sem02/lesson03/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ 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[np.newaxis, :, :]

return np.sqrt(np.sum(diff**2, axis=-1))
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 distances.shape != azimuth.shape or distances.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 abscissa.shape != ordinates.shape or abscissa.shape != applicates.shape:
raise ShapeMismatchError

distances = np.sqrt(abscissa**2 + ordinates**2 + applicates**2)
azimuth = np.arctan2(ordinates, abscissa)
inclination = np.arctan2(np.sqrt(abscissa**2 + ordinates**2), applicates)

return distances, azimuth, inclination
16 changes: 15 additions & 1 deletion solutions/sem02/lesson03/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,18 @@

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

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

minima_mask = (middle < left) & (middle < right)
maxima_mask = (middle > left) & (middle > right)

minima_indices = np.where(minima_mask)[0] + 1
maxima_indices = np.where(maxima_mask)[0] + 1

return minima_indices, maxima_indices
33 changes: 29 additions & 4 deletions solutions/sem02/lesson04/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,41 @@


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

height, width = image.shape[:2]
new_shape = (height + 2 * pad_size, width + 2 * pad_size) + image.shape[2:]

padded = np.zeros(new_shape, dtype=image.dtype)
padded[pad_size : pad_size + height, pad_size : pad_size + width] = image

return padded


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.copy()

pad_size = kernel_size // 2
padded = pad_image(image, pad_size)

height, width = image.shape[:2]
accumulator = np.zeros(image.shape, dtype=np.float64)

for di in range(kernel_size):
for dj in range(kernel_size):
accumulator += padded[di : di + height, dj : dj + width]

result = accumulator / (kernel_size**2)

return result.astype(image.dtype)


if __name__ == "__main__":
Expand Down
20 changes: 18 additions & 2 deletions solutions/sem02/lesson04/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ 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
total = image.size
histogram = np.bincount(image.flatten(), minlength=256)

cumulative = np.concatenate(([0], np.cumsum(histogram)))

colors = np.arange(256)
left = np.maximum(0, colors - threshold + 1)
right = np.minimum(256, colors + threshold)

counts = cumulative[right] - cumulative[left]
counts[histogram == 0] = -1

dominant_color = np.argmax(counts)
percent = counts[dominant_color] / total * 100

return np.uint8(dominant_color), percent
16 changes: 15 additions & 1 deletion solutions/sem02/lesson05/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ def can_satisfy_demand(
costs: np.ndarray,
resource_amounts: np.ndarray,
demand_expected: np.ndarray,
) -> bool: ...
) -> bool:
if costs.ndim != 2:
raise ShapeMismatchError

m, n = costs.shape

if resource_amounts.shape != (m,):
raise ShapeMismatchError

if demand_expected.shape != (n,):
raise ShapeMismatchError

resources_required = costs @ demand_expected

return bool(np.all(resources_required <= resource_amounts))
21 changes: 20 additions & 1 deletion solutions/sem02/lesson05/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,23 @@ 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.ndim != 2 or matrix.shape[0] != matrix.shape[1]:
raise ShapeMismatchError

if matrix.shape[1] != vector.shape[0]:
raise ShapeMismatchError

n = matrix.shape[0]

if np.linalg.matrix_rank(matrix) != n:
return None, None

scalar_products = matrix @ vector
squared_norms = np.sum(matrix**2, axis=1)
coefficients = scalar_products / squared_norms

projections = coefficients[:, np.newaxis] * matrix
components = vector - projections

return projections, components
23 changes: 22 additions & 1 deletion solutions/sem02/lesson05/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,25 @@ def adaptive_filter(
Vs: np.ndarray,
Vj: np.ndarray,
diag_A: np.ndarray,
) -> np.ndarray: ...
) -> np.ndarray:
if Vs.ndim != 2 or Vj.ndim != 2 or diag_A.ndim != 1:
raise ShapeMismatchError

if Vs.shape[0] != Vj.shape[0]:
raise ShapeMismatchError

if Vj.shape[1] != diag_A.shape[0]:
raise ShapeMismatchError

m = Vs.shape[0]
k = Vj.shape[1]

Vj_H = Vj.conj().T
A = np.diag(diag_A)

inner = np.eye(k) + Vj_H @ Vj @ A
inner_inv = np.linalg.inv(inner)

R_inv = np.eye(m) - Vj @ inner_inv @ Vj_H

return R_inv @ Vs
Loading
Loading