Skip to content

Commit aef97df

Browse files
Week04 homeworks added
1 parent 56cd502 commit aef97df

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
import tracemalloc
3+
from functools import wraps
4+
5+
def performance(func):
6+
@wraps(func)
7+
def wrapper(*args, **kwargs):
8+
performance.counter += 1
9+
10+
tracemalloc.start()
11+
start_time = time.perf_counter()
12+
13+
result = func(*args, **kwargs)
14+
15+
end_time = time.perf_counter()
16+
current, peak = tracemalloc.get_traced_memory()
17+
tracemalloc.stop()
18+
19+
performance.total_time += (end_time - start_time)
20+
performance.total_mem += peak
21+
22+
return result
23+
24+
return wrapper
25+
26+
performance.counter = 0
27+
performance.total_time = 0.0
28+
performance.total_mem = 0

‎Week04/functions_yigithan_erken.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
custom_power = lambda x=0, /, e=1: x**e
2+
3+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
4+
"""
5+
Calculates a custom equation.
6+
7+
:param x: Positional-only integer, defaults to 0.
8+
:param y: Positional-only integer, defaults to 0.
9+
:param a: Positional-or-keyword integer, defaults to 1.
10+
:param b: Positional-or-keyword integer, defaults to 1.
11+
:param c: Keyword-only integer, defaults to 1.
12+
:return: The result of (x**a + y**b) / c as float.
13+
"""
14+
if not all(isinstance(val, int) for val in (x, y, a, b, c)):
15+
raise TypeError("Arguments must be integers.")
16+
17+
return float((x**a + y**b) / c)
18+
19+
def fn_w_counter() -> (int, dict[str, int]):
20+
if not hasattr(fn_w_counter, "total_calls"):
21+
fn_w_counter.total_calls = 0
22+
fn_w_counter.caller_counts = {}
23+
24+
caller_name = __name__
25+
26+
fn_w_counter.total_calls += 1
27+
28+
if caller_name in fn_w_counter.caller_counts:
29+
fn_w_counter.caller_counts[caller_name] += 1
30+
else:
31+
fn_w_counter.caller_counts[caller_name] = 1
32+
33+
return (fn_w_counter.total_calls, fn_w_counter.caller_counts)

0 commit comments

Comments
 (0)