From d0f23b246e138816fcd95e0e9837faef094a8b9a Mon Sep 17 00:00:00 2001 From: Sude Naz Ay Date: Wed, 28 Oct 2026 21:07:20 +0300 Subject: [PATCH 1/3] Create functions_sude_naz_ay.py --- Week04/functions_sude_naz_ay.py | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Week04/functions_sude_naz_ay.py diff --git a/Week04/functions_sude_naz_ay.py b/Week04/functions_sude_naz_ay.py new file mode 100644 index 00000000..e4615d26 --- /dev/null +++ b/Week04/functions_sude_naz_ay.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 2a8557624e4ac831413107d636ae11ea41438f51 Mon Sep 17 00:00:00 2001 From: Sude Naz Ay Date: Wed, 28 Oct 2026 21:12:15 +0300 Subject: [PATCH 2/3] Update functions_sude_naz_ay.py --- Week04/functions_sude_naz_ay.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Week04/functions_sude_naz_ay.py b/Week04/functions_sude_naz_ay.py index e4615d26..5b048f91 100644 --- a/Week04/functions_sude_naz_ay.py +++ b/Week04/functions_sude_naz_ay.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 40bff0c4df7e0f535b1f46aefbc67fca5ec05ea5 Mon Sep 17 00:00:00 2001 From: Sude Naz Ay Date: Wed, 28 Oct 2026 21:27:59 +0300 Subject: [PATCH 3/3] Update functions_sude_naz_ay.py --- Week04/functions_sude_naz_ay.py | 69 ++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/Week04/functions_sude_naz_ay.py b/Week04/functions_sude_naz_ay.py index 5b048f91..cbb7402e 100644 --- a/Week04/functions_sude_naz_ay.py +++ b/Week04/functions_sude_naz_ay.py @@ -1,38 +1,69 @@ -import inspect +"""Week04 - Introduction to Python: functions.""" -custom_power = lambda x=0, /, e=1: x**e # noqa: E731 +# 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 = {} + """ + 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 = __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.")