Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f4e72da
решенные задачи lesson02
bermo0d Sep 23, 2025
5173935
remove unused variable
bermo0d Sep 25, 2025
6fb47db
new solution
bermo0d Sep 26, 2025
254a12c
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Oct 4, 2025
462565f
lesson03 solution
bermo0d Oct 10, 2025
92b00a6
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Oct 11, 2025
88935c5
lesson04 solutions
bermo0d Oct 15, 2025
8944cea
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Oct 18, 2025
63a836f
lesson05 solutions
bermo0d Oct 23, 2025
96e9f32
code formatted
bermo0d Oct 23, 2025
8fbaeaa
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Oct 25, 2025
17c0a66
solutions lesson06
bermo0d Oct 26, 2025
d6628d2
optimize solution of task3
bermo0d Oct 28, 2025
6064f83
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Nov 9, 2025
39749ea
solutions lesson08
bermo0d Nov 14, 2025
40e4b2a
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Nov 18, 2025
c9b4f26
solution homework task1
bermo0d Nov 18, 2025
fa93202
solution homework task2
bermo0d Nov 18, 2025
ec65c26
modified solution task2
bermo0d Nov 19, 2025
099f7ae
solution homework task3
bermo0d Nov 19, 2025
b413135
solution homework task4
bermo0d Nov 20, 2025
ce43573
modified solution homework task4
bermo0d Nov 20, 2025
9c28796
formatting code in homework tasks
bermo0d Nov 20, 2025
180bd61
used default dict
bermo0d Nov 22, 2025
ab61c37
modified homework task1
bermo0d Nov 22, 2025
459c0ef
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Nov 29, 2025
92cbb23
modified solution task1
bermo0d Nov 30, 2025
c57de73
solution lesson11
bermo0d Dec 1, 2025
f5d844a
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Dec 12, 2025
ffb6691
solution lesson12
bermo0d Dec 12, 2025
33529b1
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Feb 28, 2026
ef55bc7
Merge branch 'main' of https://github.com/EvgrafovMichail/python_mipt…
bermo0d Mar 13, 2026
cad53e4
Merge branch 'EvgrafovMichail:main' into main
bermo0d Apr 10, 2026
be9b7fa
sotution task1
bermo0d Apr 10, 2026
b1a6159
sotution task2
bermo0d Apr 10, 2026
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
75 changes: 73 additions & 2 deletions homeworks/sem01/hw1/aggregate_segmentation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
from uuid import UUID

ALLOWED_TYPES = {
"spotter_word",
"voice_human",
"voice_bot",
}


def is_valid_uuid(uuid):
try:
UUID(uuid)
return True
except ValueError:
return False


def is_valid_segment(segment):
if "segment_id" not in segment:
return False

if not is_valid_uuid(segment["segment_id"]):
return False

if (
segment["type"] is None
and segment["segment_start"] is None
and segment["segment_end"] is None
):
pass
elif (
not isinstance(segment["type"], str)
or not isinstance(segment["segment_start"], float)
or not isinstance(segment["segment_end"], float)
):
return False

if segment["type"] not in ALLOWED_TYPES and segment["type"] is not None:
return False

return True


def aggregate_segmentation(
segmentation_data: list[dict[str, str | float | None]],
) -> tuple[dict[str, dict[str, dict[str, str | float]]], list[str]]:
Expand All @@ -24,5 +60,40 @@ def aggregate_segmentation(
Список `audio_id` (str), которые требуют переразметки.
"""

# ваш код
return {}, []
audio = {}
not_valid_audio_id = set()

for i in segmentation_data:
audio_id = i["audio_id"] if "audio_id" in i else None
segment_id = i["segment_id"]
start = i["segment_start"]
end = i["segment_end"]
segment_type = i["type"]

if is_valid_uuid(audio_id) and is_valid_segment(i):
if audio_id not in audio:
audio[audio_id] = {segment_id: {"end": end, "start": start, "type": segment_type}}
else:
if segment_id not in audio[audio_id]:
audio[audio_id][segment_id] = {"end": end, "start": start, "type": segment_type}
else:
segment = audio[audio_id][segment_id]
if (
segment["end"] != end
or segment["start"] != start
or segment["type"] != segment_type
):
not_valid_audio_id.add(audio_id)

else:
not_valid_audio_id.add(audio_id)

audio_without_empty_segments = {}
for a_id, segments in audio.items():
new_segments = {}
for s_id, s in segments.items():
if any(s.values()):
new_segments[s_id] = s
audio_without_empty_segments[a_id] = new_segments

return audio_without_empty_segments, list(not_valid_audio_id)
32 changes: 30 additions & 2 deletions homeworks/sem01/hw1/backoff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
from random import uniform
from time import sleep
from typing import (
Expand Down Expand Up @@ -34,5 +35,32 @@ def backoff(
ValueError, если были переданы невозможные аргументы.
"""

# ваш код
pass
if retry_amount < 1 or retry_amount > 100:
raise ValueError
if timeout_start <= 0 or timeout_start > 10:
raise ValueError
if timeout_max <= 0 or timeout_max > 10:
raise ValueError
if backoff_scale <= 0 or backoff_scale > 10:
raise ValueError

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
nonlocal timeout_start

last_error = None
for i in range(retry_amount):
try:
return func(*args, **kwargs)
except backoff_triggers as e:
sleep(timeout_start + uniform(0, 0.5))
if timeout_start < timeout_max:
timeout_start *= backoff_scale
last_error = e

raise last_error

return wrapper

return decorator
31 changes: 29 additions & 2 deletions homeworks/sem01/hw1/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
from typing import (
Callable,
ParamSpec,
Expand All @@ -23,5 +24,31 @@ def lru_cache(capacity: int) -> Callable[[Callable[P, R]], Callable[P, R]]:
для получения целого числа.
ValueError, если после округления capacity - число, меньшее 1.
"""
# ваш код
pass

try:
capacity = round(capacity)
except TypeError:
raise TypeError(f"{type(capacity).__name__} неверный тип для размера кэша") from None
if capacity < 1:
raise ValueError

cache = {}

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
func_args = args + tuple(sorted(kwargs.items()))
if func_args in cache:
cache[func_args] = cache.pop(func_args)
return cache[func_args]

else:
if len(cache) == capacity:
cache.pop(next(iter(cache.keys())))
res = func(*args, **kwargs)
cache[func_args] = res
return res

return wrapper

return decorator
16 changes: 14 additions & 2 deletions homeworks/sem01/hw1/convert_exception.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
from typing import (
Callable,
ParamSpec,
Expand All @@ -24,5 +25,16 @@ def convert_exceptions_to_api_compitable_ones(
Декоратор для непосредственного использования.
"""

# ваш код
pass
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception as e:
if type(e) in exception_to_api_exception:
raise exception_to_api_exception[type(e)] from None
raise e

return wrapper

return decorator
3 changes: 2 additions & 1 deletion solutions/sem01/lesson02/task1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
def get_factorial(num: int) -> int:
factorial = 1
# ваш код
for i in range(2, num + 1):
factorial *= i
return factorial
4 changes: 3 additions & 1 deletion solutions/sem01/lesson02/task2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def get_doubled_factorial(num: int) -> int:
factorial = 1
# ваш код
while num > 1:
factorial *= num
num -= 2
return factorial
4 changes: 3 additions & 1 deletion solutions/sem01/lesson02/task3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def get_amount_of_ways_to_climb(stair_amount: int) -> int:
step_prev, step_curr = 1, 1
# ваш код
for i in range(1, stair_amount):
step_prev, step_curr = step_curr, step_prev + step_curr

return step_curr
9 changes: 8 additions & 1 deletion solutions/sem01/lesson02/task4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
def get_multiplications_amount(num: int) -> int:
multiplications_amount = 0
# ваш код
while num != 1:
if num % 2 == 0:
multiplications_amount += 1
num //= 2
else:
multiplications_amount += 1
num -= 1

return multiplications_amount
9 changes: 7 additions & 2 deletions solutions/sem01/lesson02/task5.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
def get_gcd(num1: int, num2: int) -> int:
# ваш код
return num1
if num1 < num2:
num1, num2 = num2, num1
d = num1 % num2
while d != 0:
num1, num2 = num2, d
d = num1 % num2
return num2
15 changes: 14 additions & 1 deletion solutions/sem01/lesson02/task6.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
def get_sum_of_prime_divisors(num: int) -> int:
sum_of_divisors = 0
# ваш код
mult_of_divisors = 1
while num != 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
num //= i
if mult_of_divisors % i != 0:
sum_of_divisors += i
mult_of_divisors *= i
break
else:
if mult_of_divisors % num != 0:
sum_of_divisors += num
break

return sum_of_divisors
17 changes: 16 additions & 1 deletion solutions/sem01/lesson02/task7.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
def is_palindrome(num: int) -> bool:
if num < 0:
return False

num_reversed = 0
num_origin = num
# ваш код

smax = 0
for i in range(1, 10):
if num // 10**i > 0:
smax += 1

s = smax
while s != -1:
n_s = num // 10**s
num -= n_s * 10**s
num_reversed += n_s * 10 ** (smax - s)
s -= 1

return num_origin == num_reversed
30 changes: 28 additions & 2 deletions solutions/sem01/lesson03/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
def flip_bits_in_range(num: int, left_bit: int, right_bit: int) -> int:
# ваш код
return num
n = num
s = 0
while n:
s += 1
n //= 2

t = s
new_num = 0

for i in range(left_bit - 1):
r = 0 if num % 2 == 0 else 1
new_num += r * 2 ** (s - t)
num //= 2
t -= 1

for j in range(right_bit - left_bit + 1):
r = 1 if num % 2 == 0 else 0
new_num += r * 2 ** (s - t)
num //= 2
t -= 1

for k in range(t):
r = 0 if num % 2 == 0 else 1
new_num += r * 2 ** (s - t)
num //= 2
t -= 1

return new_num
21 changes: 19 additions & 2 deletions solutions/sem01/lesson03/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
def get_cube_root(n: float, eps: float) -> float:
# ваш код
return n
if n == 0:
return 0

abs_n = abs(n)

low = 0
high = abs_n if abs_n >= 1 else 1
mid = (low + high) / 2
mid3 = mid * mid * mid
while abs(mid3 - abs_n) > eps:
if mid3 > abs_n:
high = mid
else:
low = mid

mid = (low + high) / 2
mid3 = mid * mid * mid

return mid * abs_n / n
21 changes: 19 additions & 2 deletions solutions/sem01/lesson03/task3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
def get_nth_digit(num: int) -> int:
# ваш код
return 0
if num <= 5:
return 2 * (num - 1)

q = 5
r = 1
w = 0
while q < num:
w = q
q += 45 * 10 ** (r - 1) * (r + 1)
r += 1
d = num - w
count = (d + r - 1) // r
res = 10 ** (r - 1) + 2 * (count - 1)
last_num = count * r
while last_num - d:
res //= 10
last_num -= 1

return res % 10
9 changes: 6 additions & 3 deletions solutions/sem01/lesson04/task1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
def is_arithmetic_progression(lst: list[list[int]]) -> bool:
# ваш код
return False
def is_arithmetic_progression(lst: list[int]) -> bool:
lst.sort()
for i in range(len(lst) - 2):
if lst[i] - lst[i + 1] != lst[i + 1] - lst[i + 2]:
return False
return True
16 changes: 14 additions & 2 deletions solutions/sem01/lesson04/task2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
def merge_intervals(intervals: list[list[int, int]]) -> list[list[int, int]]:
# ваш код
return [[0,0]]
if not intervals:
return []

intervals.sort()
new_intervals = [intervals[0]]
for i in range(1, len(intervals)):
x1, y1 = new_intervals[-1]
x2, y2 = intervals[i]
if x2 <= y1:
new_intervals[-1] = [x1, max(y2, y1)]
else:
new_intervals.append(intervals[i])

return new_intervals
6 changes: 4 additions & 2 deletions solutions/sem01/lesson04/task3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
def find_single_number(nums: list[int]) -> int:
# ваш код
return 0
n = 0
for i in nums:
n ^= i
return n
Loading
Loading