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: 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
2 changes: 1 addition & 1 deletion solutions/sem01/lesson02/task3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def get_amount_of_ways_to_climb(stair_amount: int) -> int:
step_prev, step_curr = 1, 1
step_curr = 1
# ваш код
return step_curr
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
7 changes: 0 additions & 7 deletions solutions/sem01/lesson06/task3.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
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
10 changes: 0 additions & 10 deletions solutions/sem01/lesson08/task2.py
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
from typing import Callable, TypeVar

T = TypeVar("T")

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

# ваш код
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
15 changes: 12 additions & 3 deletions solutions/sem02/lesson03/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ 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, :, :]
distances = np.linalg.norm(diff, axis=-1)
return distances
18 changes: 16 additions & 2 deletions solutions/sem02/lesson03/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ 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.cos(azimuth) * np.sin(inclination)
y = distances * np.sin(azimuth) * np.sin(inclination)
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
r = np.sqrt(abscissa**2 + ordinates**2 + applicates**2)
azimuth = np.arctan2(ordinates, abscissa)
inclination = np.zeros_like(r)
mask = r != 0
inclination[mask] = np.arccos(applicates[mask] / r[mask])
return r, azimuth, inclination
12 changes: 11 additions & 1 deletion solutions/sem02/lesson03/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@

def get_extremum_indices(
ordinates: np.ndarray,
) -> tuple[np.ndarray, np.ndarray]: ...
) -> tuple[np.ndarray, np.ndarray]:
if ordinates.shape < (3,):
raise ValueError
indexs = np.arange(1, len(ordinates) - 1)
sp_min = (ordinates[indexs] < ordinates[indexs - 1]) & (
ordinates[indexs] < ordinates[indexs + 1]
)
sp_max = (ordinates[indexs] > ordinates[indexs - 1]) & (
ordinates[indexs] > ordinates[indexs + 1]
)
return indexs[sp_min], indexs[sp_max]
34 changes: 30 additions & 4 deletions solutions/sem02/lesson04/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,42 @@


def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray:
# ваш код
return image
if pad_size < 1 or image.ndim > 3:
raise ValueError
if image.ndim == 2:
height, weight = image.shape
res_image = np.zeros((height + 2 * pad_size, weight + 2 * pad_size), dtype=image.dtype)
res_image[pad_size : pad_size + height, pad_size : pad_size + weight] = image
if image.ndim == 3:
height, weight, razm = image.shape
res_image = np.zeros(
(height + 2 * pad_size, weight + 2 * pad_size, razm), dtype=image.dtype
)
res_image[pad_size : pad_size + height, pad_size : pad_size + weight, :] = image
return res_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
elif kernel_size == 1:
return image
res_image = np.zeros(image.shape, dtype=np.float64)
padded_image = pad_image(image, kernel_size // 2)
if image.ndim == 2:
for i in range(image.shape[0]):
for j in range(image.shape[1]):
res_image[i, j] = np.mean(padded_image[i : i + kernel_size, j : j + kernel_size])
if image.ndim == 3:
for i in range(image.shape[0]):
for j in range(image.shape[1]):
res_image[i, j] = np.mean(
padded_image[i : i + kernel_size, j : j + kernel_size, :], axis=(0, 1)
)
return np.round(res_image).astype(image.dtype)


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

return 0, 0
if threshold < 1:
raise ValueError("threshold must be positive")
pixels = np.sort(image.flatten())
max_color = 0
max_count = 0
left = 0
center = 0
for right in range(len(pixels)):
while pixels[right] - pixels[center] >= threshold:
center += 1
while pixels[center] - pixels[left] >= threshold:
left += 1
if right - left + 1 > max_count:
max_count = right - left + 1
max_color = pixels[center]
return max_color, max_count * 100 / len(pixels)
13 changes: 12 additions & 1 deletion solutions/sem02/lesson05/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ def can_satisfy_demand(
costs: np.ndarray,
resource_amounts: np.ndarray,
demand_expected: np.ndarray,
) -> bool: ...
) -> bool:
if np.ndim(costs) != 2:
raise ShapeMismatchError
M, N = costs.shape
if np.ndim(resource_amounts) != 1 or resource_amounts.size != M:
raise ShapeMismatchError

if np.ndim(demand_expected) != 1 or demand_expected.size != N:
raise ShapeMismatchError

resources_needed = np.sum(costs * demand_expected, axis=1)
return all(resources_needed <= resource_amounts)
11 changes: 10 additions & 1 deletion solutions/sem02/lesson05/task2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@ 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[1] != vector.shape[0]:
raise ShapeMismatchError
if not np.linalg.det(matrix):
return None, None
scalar = matrix @ vector
vector_square = np.sum(matrix**2, axis=1)
projections = (matrix.T * scalar / vector_square).T
components = vector - projections
return projections, components
12 changes: 11 additions & 1 deletion solutions/sem02/lesson05/task3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ def adaptive_filter(
Vs: np.ndarray,
Vj: np.ndarray,
diag_A: np.ndarray,
) -> np.ndarray: ...
) -> np.ndarray:
if np.ndim(Vs) != 2 or np.ndim(Vj) != 2 or np.ndim(diag_A) != 1:
raise ShapeMismatchError

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

A = np.diag(diag_A)
Vj_H = (Vj.real - 1j * Vj.imag).T
R_1 = np.linalg.inv((np.eye(diag_A.size) + Vj_H @ Vj @ A))
return Vs - Vj @ R_1 @ (Vj_H @ Vs)
55 changes: 53 additions & 2 deletions solutions/sem02/lesson07/task1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,59 @@ def visualize_diagrams(
ordinates: np.ndarray,
diagram_type: Any,
) -> None:
# ваш код
pass
if abscissa.shape != ordinates.shape:
raise ShapeMismatchError

valid_types = {"hist", "violin", "box"}
if diagram_type not in valid_types:
raise ValueError

fig = plt.figure(figsize=(8, 8))
grid = plt.GridSpec(4, 4, wspace=0.2, hspace=0.2)

ax_scat = fig.add_subplot(grid[:-1, 1:])
ax_v = fig.add_subplot(grid[:-1, 0], sharey=ax_scat)
ax_h = fig.add_subplot(grid[-1, 1:], sharex=ax_scat)

ax_scat.scatter(abscissa, ordinates, color="steelblue", alpha=0.35)
ax_scat.set_title("Distribution", fontsize=17, fontweight="bold", c="black")

if diagram_type == "hist":
hist_params = {
"bins": 50,
"color": "steelblue",
"edgecolor": "navy",
"alpha": 0.7,
"density": True,
}
ax_h.hist(abscissa, **hist_params)
ax_v.hist(ordinates, orientation="horizontal", **hist_params)
ax_h.invert_yaxis()
ax_v.invert_xaxis()

elif diagram_type == "violin":

def set_violin(axis, data, vertical):
parts = axis.violinplot(data, vert=vertical, showmedians=True)
for body in parts["bodies"]:
body.set_facecolor("steelblue")
body.set_edgecolor("lightsteelblue")
for part in parts:
if part == "bodies":
continue
parts[part].set_edgecolor("steelblue")

set_violin(ax_h, ordinates, vertical=False)
set_violin(ax_v, abscissa, vertical=True)

elif diagram_type == "box":
box_props = {"facecolor": "steelblue", "alpha": 0.7}
box_params = {"patch_artist": True, "boxprops": box_props}
ax_v.boxplot(abscissa, vert=True, **box_params)
ax_h.boxplot(ordinates, vert=False, **box_params)

ax_v.set_ylabel("Vertical", fontsize=17, fontweight="bold", c="dimgray")
ax_h.set_xlabel("Horizontal", fontsize=17, fontweight="bold", c="dimgray")


if __name__ == "__main__":
Expand Down
Loading
Loading