Skip to content

Commit c28d903

Browse files
authored
Add custom functions for power and counting calls
Added custom_power lambda function and custom_equation function with parameter validation. Implemented fn_w_counter to count function calls.
1 parent 71f5b39 commit c28d903

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Week04/functions_dilara_agac.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
custom_power = lambda x=0, /, e=1: x ** e
2+
3+
4+
def custom_equation(
5+
x: int = 0,
6+
y: int = 0,
7+
/,
8+
a: int = 1,
9+
b: int = 1,
10+
*,
11+
c: int = 1
12+
) -> float:
13+
"""
14+
Custom equation function.
15+
16+
:param x: first value
17+
:param y: second value
18+
:param a: power of x
19+
:param b: power of y
20+
:param c: divisor
21+
:return: result of equation
22+
"""
23+
if not isinstance(x, int) or not isinstance(y, int):
24+
raise TypeError("x and y must be integers")
25+
if not isinstance(a, int) or not isinstance(b, int) or not isinstance(c, int):
26+
raise TypeError("a, b and c must be integers")
27+
28+
return (x ** a + y ** b) / c
29+
30+
31+
def fn_w_counter() -> (int, dict[str, int]):
32+
if not hasattr(fn_w_counter, "count"):
33+
fn_w_counter.count = 0
34+
35+
fn_w_counter.count += 1
36+
37+
caller_name = __name__
38+
return fn_w_counter.count, {caller_name: fn_w_counter.count}

0 commit comments

Comments
 (0)