Skip to content

Commit 983ca3d

Browse files
Add custom power and equation functions with examples
This file contains implementations of custom power and equation functions, as well as a function to count calls to it. Examples of usage are also provided.
1 parent 9a399e7 commit 983ca3d

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

‎Week04/functions_ayse_arsla.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# custom_power
2+
custom_power = lambda x, /, e=1: x ** e
3+
4+
5+
# custom_equation
6+
def custom_equation(
7+
x: int = 0,
8+
y: int = 0,
9+
/,
10+
a: int = 1,
11+
b: int = 1,
12+
*,
13+
c: int = 1
14+
) -> float:
15+
"""
16+
Calculates the custom equation.
17+
18+
:param x: First number.
19+
:param y: Second number.
20+
:param a: First exponent.
21+
:param b: Second exponent.
22+
:param c: Divisor.
23+
:returns: The result of the equation.
24+
:rtype: float
25+
"""
26+
return (x ** a + y ** b) / c
27+
28+
29+
# fn_w_counter
30+
import inspect
31+
32+
def fn_w_counter() -> tuple:
33+
caller = inspect.currentframe().f_back.f_code.co_name
34+
35+
fn_w_counter.counter += 1
36+
37+
if caller in fn_w_counter.callers:
38+
fn_w_counter.callers[caller] += 1
39+
else:
40+
fn_w_counter.callers[caller] = 1
41+
42+
return fn_w_counter.counter, fn_w_counter.callers.copy()
43+
44+
45+
fn_w_counter.counter = 0
46+
fn_w_counter.callers = {}
47+
48+
49+
# Examples
50+
print(custom_power(2))
51+
print(custom_power(2, 3))
52+
print(custom_power(2, e=2))
53+
54+
print(custom_equation(2, 3))
55+
print(custom_equation(2, 3, 2))
56+
print(custom_equation(2, 3, 2, 3))
57+
print(custom_equation(3, 5, a=2, b=3, c=4))
58+
print(custom_equation(3, 5, 2, b=3, c=4))
59+
print(custom_equation(3, 5, 2, 3, c=4))
60+
61+
for i in range(10):
62+
fn_w_counter()
63+
64+
print(fn_w_counter())

0 commit comments

Comments
 (0)