Skip to content

Commit 4e575e7

Browse files
authored
fixed the bug2
1 parent 7d5288a commit 4e575e7

1 file changed

Lines changed: 53 additions & 20 deletions

File tree

‎Week04/functions_batuhan_keskin.py‎

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

3-
custom_power = lambda x=0, /, e=1: x**e # noqa: E731
3+
import sys
44

5+
# x positional-only (default 0), e positional-or-keyword (default 1)
6+
custom_power = lambda x=0, /, e=1: x ** e
57

6-
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
8+
9+
def custom_equation(
10+
x: int = 0,
11+
y: int = 0,
12+
/,
13+
a: int = 1,
14+
b: int = 1,
15+
*,
16+
c: int = 1,
17+
) -> float:
718
"""
8-
Calculate custom equation.
19+
Calculate (x**a + y**b) / c.
920
10-
:param x: Positional-only base for first term, default 0
21+
:param x: first base (positional-only)
1122
:type x: int
12-
:param y: Positional-only base for second term, default 0
23+
:param y: second base (positional-only)
1324
:type y: int
14-
:param a: Exponent for first term, default 1
25+
:param a: exponent of x (positional-or-keyword)
1526
:type a: int
16-
:param b: Exponent for second term, default 1
27+
:param b: exponent of y (positional-or-keyword)
1728
:type b: int
18-
:param c: Keyword-only divisor, default 1
29+
:param c: divisor (keyword-only)
1930
:type c: int
20-
:return: Computed float result of (x**a + y**b) / c
31+
:return: the value of (x**a + y**b) / c
2132
:rtype: float
2233
"""
23-
return float((x**a + y**b) / c)
34+
return (x ** a + y ** b) / c
35+
36+
37+
_call_counts: dict[str, int] = {}
38+
_total_calls: int = 0
2439

2540

26-
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 = {}
41+
def fn_w_counter() -> tuple[int, dict[str, int]]:
42+
"""
43+
Count the calls made to this function together with caller information.
44+
45+
:return: total number of calls and a dictionary mapping each caller's
46+
``__name__`` to the number of calls made from that caller
47+
:rtype: tuple[int, dict[str, int]]
48+
"""
49+
global _total_calls
50+
caller = sys._getframe(1).f_globals.get("__name__", "<unknown>")
51+
_total_calls += 1
52+
_call_counts[caller] = _call_counts.get(caller, 0) + 1
53+
return _total_calls, dict(_call_counts)
3054

31-
fn_w_counter.total_calls += 1
3255

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

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

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

0 commit comments

Comments
 (0)