Skip to content

Commit ee7428c

Browse files
Add custom power and equation functions with counter
Defines a custom power function and an equation function that combines two terms raised to specified powers. Also implements a counter function to track calls from different callers.
1 parent 8415c79 commit ee7428c

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
3+
custom_power = lambda x=0, /, e=1: x ** e
4+
5+
6+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
7+
"""
8+
Raises x to the power a and y to the power b, adds them and divides by c.
9+
10+
:param x: Base of the first term (positional-only)
11+
:type x: int
12+
:param y: Base of the second term (positional-only)
13+
:type y: int
14+
:param a: Power applied to x
15+
:type a: int
16+
:param b: Power applied to y
17+
:type b: int
18+
:param c: Number to divide the sum by (keyword-only)
19+
:type c: int
20+
:return: The calculated value
21+
:rtype: float
22+
"""
23+
first_term = x ** a
24+
second_term = y ** b
25+
return float((first_term + second_term) / c)
26+
27+
28+
def _make_counter():
29+
calls = {"count": 0}
30+
by_caller = {}
31+
32+
def fn_w_counter() -> tuple[int, dict[str, int]]:
33+
name = sys._getframe(1).f_globals.get("__name__")
34+
calls["count"] += 1
35+
if name in by_caller:
36+
by_caller[name] += 1
37+
else:
38+
by_caller[name] = 1
39+
return calls["count"], by_caller
40+
41+
return fn_w_counter
42+
43+
44+
fn_w_counter = _make_counter()

0 commit comments

Comments
 (0)