Skip to content

Commit 2abeb75

Browse files
Create functions_hatice_akgul.py
1 parent 71f5b39 commit 2abeb75

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Week04/functions_hatice_akgul.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import inspect
2+
3+
# 1. custom_power: lambda function with positional-only x
4+
custom_power = lambda x=0, /, e=1: x**e
5+
6+
# 2. custom_equation: positional-only, positional-or-keyword, and keyword-only params
7+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
8+
"""
9+
Calculates (x**a + y**b) / c with specific parameter constraints.
10+
11+
:param x: Base 1, positional-only
12+
:param y: Base 2, positional-only
13+
:param a: Exponent 1, positional-or-keyword
14+
:param b: Exponent 2, positional-or-keyword
15+
:param c: Divisor, keyword-only
16+
:returns: Result as float
17+
"""
18+
return float((x**a + y**b) / c)
19+
20+
# 3. fn_w_counter: tracks total calls and caller names
21+
def fn_w_counter() -> tuple[int, dict]:
22+
"""
23+
Counts the number of calls and tracks the caller (__name__).
24+
"""
25+
if not hasattr(fn_w_counter, "calls"):
26+
fn_w_counter.calls = 0
27+
fn_w_counter.callers = {}
28+
29+
caller_frame = inspect.currentframe().f_back
30+
caller_name = caller_frame.f_globals.get('__name__', 'unknown')
31+
32+
fn_w_counter.calls += 1
33+
fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1
34+
35+
return (fn_w_counter.calls, fn_w_counter.callers)

0 commit comments

Comments
 (0)