Skip to content

Commit 40bff0c

Browse files
authored
Update functions_sude_naz_ay.py
1 parent 2a85576 commit 40bff0c

1 file changed

Lines changed: 50 additions & 19 deletions

File tree

‎Week04/functions_sude_naz_ay.py‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,69 @@
1-
import inspect
1+
"""Week04 - Introduction to Python: functions."""
22

3-
custom_power = lambda x=0, /, e=1: x**e # noqa: E731
3+
# x positional-only (default 0), e positional-or-keyword (default 1)
4+
custom_power = lambda x=0, /, e=1: x ** e
45

56

6-
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
7+
def custom_equation(
8+
x: int = 0,
9+
y: int = 0,
10+
/,
11+
a: int = 1,
12+
b: int = 1,
13+
*,
14+
c: int = 1,
15+
) -> float:
716
"""
8-
Calculate custom equation.
17+
Calculate (x**a + y**b) / c.
918
10-
:param x: Positional-only base for first term, default 0
19+
:param x: first base (positional-only)
1120
:type x: int
12-
:param y: Positional-only base for second term, default 0
21+
:param y: second base (positional-only)
1322
:type y: int
14-
:param a: Exponent for first term, default 1
23+
:param a: exponent of x (positional-or-keyword)
1524
:type a: int
16-
:param b: Exponent for second term, default 1
25+
:param b: exponent of y (positional-or-keyword)
1726
:type b: int
18-
:param c: Keyword-only divisor, default 1
27+
:param c: divisor (keyword-only)
1928
:type c: int
20-
:return: Computed float result of (x**a + y**b) / c
29+
:return: the value of (x**a + y**b) / c
2130
:rtype: float
2231
"""
23-
return float((x**a + y**b) / c)
32+
return (x ** a + y ** b) / c
33+
34+
35+
_call_counts: dict[str, int] = {}
36+
_total_calls: int = 0
2437

2538

2639
def fn_w_counter() -> (int, dict[str, int]):
27-
if not hasattr(fn_w_counter, "total_calls"):
28-
fn_w_counter.total_calls = 0
29-
fn_w_counter.callers = {}
40+
"""
41+
Count the calls made to this function together with caller information.
42+
43+
:return: total number of calls and a dictionary mapping each caller's
44+
``__name__`` to the number of calls made from that caller
45+
:rtype: tuple[int, dict[str, int]]
46+
"""
47+
global _total_calls
48+
caller = __name__
49+
_total_calls += 1
50+
_call_counts[caller] = _call_counts.get(caller, 0) + 1
51+
return _total_calls, dict(_call_counts)
3052

31-
fn_w_counter.total_calls += 1
3253

33-
caller_frame = inspect.currentframe().f_back
34-
caller_name = caller_frame.f_globals.get("__name__", "__main__")
54+
if __name__ == "__main__":
55+
assert custom_power(2) == 2
56+
assert custom_power(2, 3) == 8
57+
assert custom_power(2, e=2) == 4
3558

36-
fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1
59+
assert custom_equation(2, 3) == 5.0
60+
assert custom_equation(2, 3, 2) == 7.0
61+
assert custom_equation(2, 3, 2, 3) == 31.0
62+
assert custom_equation(3, 5, a=2, b=3, c=4) == 33.5
63+
assert custom_equation(3, 5, 2, b=3, c=4) == 33.5
64+
assert custom_equation(3, 5, 2, 3, c=4) == 33.5
3765

38-
return fn_w_counter.total_calls, fn_w_counter.callers
66+
for i in range(10):
67+
fn_w_counter()
68+
assert fn_w_counter() == (11, {'__main__': 11})
69+
print("All examples passed.")

0 commit comments

Comments
 (0)