Skip to content

Commit 2879ca7

Browse files
Add custom power, equation, and call counter functions
1 parent 71f5b39 commit 2879ca7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 1. custom_power: A lambda function that raises x to the power of e
2+
custom_power = lambda x=0, /, e=1: x ** e
3+
4+
# 2. custom_equation: Implements (x^a + y^b) / c
5+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
6+
"""
7+
Calculates the equation (x**a + y**b) / c.
8+
9+
:param x: Base for the first term (positional only)
10+
:param y: Base for the second term (positional only)
11+
:param a: Exponent for the first term
12+
:param b: Exponent for the second term
13+
:param c: Divisor (keyword only)
14+
:return: The result as a float
15+
"""
16+
# Type checking to satisfy the "must" message requirement in TypeError
17+
if not all(isinstance(arg, int) for arg in [x, y, a, b, c]):
18+
raise TypeError("All arguments must be integers.")
19+
20+
return float((x ** a + y ** b) / c)
21+
22+
# 3. fn_w_counter: A stateful function tracking how many times it is called
23+
# We use a global variable to maintain the state across calls.
24+
_call_count = 0
25+
26+
def fn_w_counter() -> (int, dict[str, int]):
27+
global _call_count
28+
_call_count += 1
29+
# Returns the current count and a dict mapping the module name to the count
30+
return _call_count, {__name__: _call_count}

0 commit comments

Comments
 (0)