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
9 changes: 9 additions & 0 deletions Week03/pyramid_khazar_abdullayev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def calculate_pyramid_height(number_of_blocks):
height = 0
needed = 1
while number_of_blocks >= needed:
number_of_blocks -= needed
height += 1
needed += 1
return height

28 changes: 28 additions & 0 deletions Week03/sequences_khazar_abdullayev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def remove_duplicates(seq: list) -> list:
seq.sort()
i = 1
while i < len(seq):
if seq[i] == seq[i - 1]:
seq.pop(i)
else:
i = i + 1

return seq

def list_counts(seq: list) -> dict:
seq.sort()
counts = {}
for item in seq:
if item in counts:
counts[item] += 1
else:
counts[item] = 1
return counts

def reverse_dict(d: dict) -> dict:
d_reverse = {}
for key in d:
value = d[key]
d_reverse[value] = key

return d_reverse
47 changes: 47 additions & 0 deletions Week04/decorators_khazar_abdullayev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import functools
import time
import tracemalloc


def performance(fn):
"""
Decorator that measures execution time and memory of the wrapped function.

Statistics are stored as attributes on the decorator itself:

- ``performance.counter``: number of decorated-function calls
- ``performance.total_time``: total time spent in seconds
- ``performance.total_mem``: total memory consumed in bytes

:param fn: the function to decorate
:type fn: callable
:return: the wrapped function
:rtype: callable
"""

@functools.wraps(fn)
def wrapper(*args, **kwargs):
started_here = not tracemalloc.is_tracing()
if started_here:
tracemalloc.start()
tracemalloc.reset_peak()
mem_before = tracemalloc.get_traced_memory()[0]
start = time.perf_counter()

try:
return fn(*args, **kwargs)
finally:
elapsed = time.perf_counter() - start
peak = tracemalloc.get_traced_memory()[1]
if started_here:
tracemalloc.stop()
performance.counter += 1
performance.total_time += elapsed
performance.total_mem += max(peak - mem_before, 0)

return wrapper


performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
29 changes: 29 additions & 0 deletions Week04/functions_khazar_abdullayev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
custom_power = lambda x=0, /, e=1: x ** e


def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
"""
Calculate the custom equation (x**a + y**b) / c.

:param x: first base
:param y: second base
:param a: first exponent
:param b: second exponent
:param c: divisor
:return: the computed float result
"""
equation = (x**a + y**b) / c
return equation


def fn_w_counter() -> (int, dict[str, int]):
if not hasattr(fn_w_counter, "call_counter"):
fn_w_counter.call_counter = 0
fn_w_counter.caller_dict = {}
caller = __name__
fn_w_counter.call_counter += 1
if caller in fn_w_counter.caller_dict:
fn_w_counter.caller_dict[caller] += 1
else:
fn_w_counter.caller_dict[caller] = 1
return fn_w_counter.call_counter, fn_w_counter.caller_dict
Loading