From 76d0adcac91749d1a5da49c1491fadf0235a52cd Mon Sep 17 00:00:00 2001 From: Batuhan Keskin <121479880+batyou0ne@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:07:20 +0300 Subject: [PATCH 1/5] Implement custom power and equation functions Add custom power function and equation calculator with call counter --- Week04/functions_batuhan_keskin.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Week04/functions_batuhan_keskin.py diff --git a/Week04/functions_batuhan_keskin.py b/Week04/functions_batuhan_keskin.py new file mode 100644 index 00000000..e4615d26 --- /dev/null +++ b/Week04/functions_batuhan_keskin.py @@ -0,0 +1,39 @@ +import inspect + +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 result based on provided parameters. + + :param x: Positional-only base for first term, default 0 + :type x: int + :param y: Positional-only base for second term, default 0 + :type y: int + :param a: Exponent for first term, default 1 + :type a: int + :param b: Exponent for second term, default 1 + :type b: int + :param c: Keyword-only divisor, default 1 + :type c: int + :return: Computed float result of (x**a + y**b) / c + :rtype: float + """ + return float((x**a + y**b) / c) + + +def fn_w_counter() -> tuple[int, dict[str, int]]: + if not hasattr(fn_w_counter, "total_calls"): + fn_w_counter.total_calls = 0 + fn_w_counter.callers = {} + + fn_w_counter.total_calls += 1 + + # Çağrıyı yapan modülün __name__ değerini tespit eder + caller_frame = inspect.currentframe().f_back + caller_name = caller_frame.f_globals.get("__name__", "__main__") + + fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 + + return fn_w_counter.total_calls, fn_w_counter.callers From 7d5288a5a4f7c6d21b3724ae0ac4bbc518cf26ea Mon Sep 17 00:00:00 2001 From: Batuhan Keskin <121479880+batyou0ne@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:12:34 +0300 Subject: [PATCH 2/5] fixed the error --- Week04/functions_batuhan_keskin.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Week04/functions_batuhan_keskin.py b/Week04/functions_batuhan_keskin.py index e4615d26..5b048f91 100644 --- a/Week04/functions_batuhan_keskin.py +++ b/Week04/functions_batuhan_keskin.py @@ -1,11 +1,11 @@ import inspect -custom_power = lambda x=0, /, e=1: x**e +custom_power = lambda x=0, /, e=1: x**e # noqa: E731 def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: """ - Calculate the custom equation result based on provided parameters. + Calculate custom equation. :param x: Positional-only base for first term, default 0 :type x: int @@ -23,14 +23,13 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int return float((x**a + y**b) / c) -def fn_w_counter() -> tuple[int, dict[str, int]]: +def fn_w_counter() -> (int, dict[str, int]): if not hasattr(fn_w_counter, "total_calls"): fn_w_counter.total_calls = 0 fn_w_counter.callers = {} fn_w_counter.total_calls += 1 - # Çağrıyı yapan modülün __name__ değerini tespit eder caller_frame = inspect.currentframe().f_back caller_name = caller_frame.f_globals.get("__name__", "__main__") From 4e575e7afc08320fe5c41fe787375b5bc352e106 Mon Sep 17 00:00:00 2001 From: Batuhan Keskin <121479880+batyou0ne@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:16:25 +0300 Subject: [PATCH 3/5] fixed the bug2 --- Week04/functions_batuhan_keskin.py | 73 ++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/Week04/functions_batuhan_keskin.py b/Week04/functions_batuhan_keskin.py index 5b048f91..91bef419 100644 --- a/Week04/functions_batuhan_keskin.py +++ b/Week04/functions_batuhan_keskin.py @@ -1,38 +1,71 @@ -import inspect +"""Week04 - Introduction to Python: functions.""" -custom_power = lambda x=0, /, e=1: x**e # noqa: E731 +import sys +# x positional-only (default 0), e positional-or-keyword (default 1) +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: + +def custom_equation( + x: int = 0, + y: int = 0, + /, + a: int = 1, + b: int = 1, + *, + c: int = 1, +) -> float: """ - Calculate custom equation. + Calculate (x**a + y**b) / c. - :param x: Positional-only base for first term, default 0 + :param x: first base (positional-only) :type x: int - :param y: Positional-only base for second term, default 0 + :param y: second base (positional-only) :type y: int - :param a: Exponent for first term, default 1 + :param a: exponent of x (positional-or-keyword) :type a: int - :param b: Exponent for second term, default 1 + :param b: exponent of y (positional-or-keyword) :type b: int - :param c: Keyword-only divisor, default 1 + :param c: divisor (keyword-only) :type c: int - :return: Computed float result of (x**a + y**b) / c + :return: the value of (x**a + y**b) / c :rtype: float """ - return float((x**a + y**b) / c) + return (x ** a + y ** b) / c + + +_call_counts: dict[str, int] = {} +_total_calls: int = 0 -def fn_w_counter() -> (int, dict[str, int]): - if not hasattr(fn_w_counter, "total_calls"): - fn_w_counter.total_calls = 0 - fn_w_counter.callers = {} +def fn_w_counter() -> tuple[int, dict[str, int]]: + """ + Count the calls made to this function together with caller information. + + :return: total number of calls and a dictionary mapping each caller's + ``__name__`` to the number of calls made from that caller + :rtype: tuple[int, dict[str, int]] + """ + global _total_calls + caller = sys._getframe(1).f_globals.get("__name__", "") + _total_calls += 1 + _call_counts[caller] = _call_counts.get(caller, 0) + 1 + return _total_calls, dict(_call_counts) - fn_w_counter.total_calls += 1 - caller_frame = inspect.currentframe().f_back - caller_name = caller_frame.f_globals.get("__name__", "__main__") +if __name__ == "__main__": + assert custom_power(2) == 2 + assert custom_power(2, 3) == 8 + assert custom_power(2, e=2) == 4 - fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 + assert custom_equation(2, 3) == 5.0 + assert custom_equation(2, 3, 2) == 7.0 + assert custom_equation(2, 3, 2, 3) == 31.0 + assert custom_equation(3, 5, a=2, b=3, c=4) == 33.5 + assert custom_equation(3, 5, 2, b=3, c=4) == 33.5 + assert custom_equation(3, 5, 2, 3, c=4) == 33.5 - return fn_w_counter.total_calls, fn_w_counter.callers + for i in range(10): + fn_w_counter() + assert fn_w_counter() == (11, {'__main__': 11}) + print("All examples passed.") From c914eda6710dcf897ec736d23bc3c7f2fe25c362 Mon Sep 17 00:00:00 2001 From: Batuhan Keskin <121479880+batyou0ne@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:20:04 +0300 Subject: [PATCH 4/5] fixed the bug 3 times... --- Week04/functions_batuhan_keskin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/functions_batuhan_keskin.py b/Week04/functions_batuhan_keskin.py index 91bef419..c685fd3a 100644 --- a/Week04/functions_batuhan_keskin.py +++ b/Week04/functions_batuhan_keskin.py @@ -38,7 +38,7 @@ def custom_equation( _total_calls: int = 0 -def fn_w_counter() -> tuple[int, dict[str, int]]: +def fn_w_counter() -> (int, dict[str, int]): """ Count the calls made to this function together with caller information. From 93776cafff1e928db8045a49339d4c20c7b535fa Mon Sep 17 00:00:00 2001 From: Batuhan Keskin <121479880+batyou0ne@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:22:34 +0300 Subject: [PATCH 5/5] four times.. --- Week04/functions_batuhan_keskin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Week04/functions_batuhan_keskin.py b/Week04/functions_batuhan_keskin.py index c685fd3a..cbb7402e 100644 --- a/Week04/functions_batuhan_keskin.py +++ b/Week04/functions_batuhan_keskin.py @@ -1,7 +1,5 @@ """Week04 - Introduction to Python: functions.""" -import sys - # x positional-only (default 0), e positional-or-keyword (default 1) custom_power = lambda x=0, /, e=1: x ** e @@ -47,7 +45,7 @@ def fn_w_counter() -> (int, dict[str, int]): :rtype: tuple[int, dict[str, int]] """ global _total_calls - caller = sys._getframe(1).f_globals.get("__name__", "") + caller = __name__ _total_calls += 1 _call_counts[caller] = _call_counts.get(caller, 0) + 1 return _total_calls, dict(_call_counts)