diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-12-09 12:22:43 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2024-12-09 12:22:43 +0200 |
commit | 30c2d7c3a0c51cc3f07ec1b4c9c8ac0168078e9c (patch) | |
tree | 723e8c9122defdc0d3a99e7314342dd42cb262b0 | |
parent | 5527cda74cf6425eb3a3bc4a299bce75e227ca54 (diff) | |
download | cs-y13-30c2d7c3a0c51cc3f07ec1b4c9c8ac0168078e9c.tar.gz cs-y13-30c2d7c3a0c51cc3f07ec1b4c9c8ac0168078e9c.tar.bz2 cs-y13-30c2d7c3a0c51cc3f07ec1b4c9c8ac0168078e9c.zip |
feat: add recursion example from test
-rw-r--r-- | main.py | 1 | ||||
-rw-r--r-- | py_test_example.py | 7 | ||||
-rw-r--r-- | recursion/recursion.py | 29 |
3 files changed, 28 insertions, 9 deletions
@@ -1 +0,0 @@ - diff --git a/py_test_example.py b/py_test_example.py new file mode 100644 index 0000000..3386a11 --- /dev/null +++ b/py_test_example.py @@ -0,0 +1,7 @@ +def x(n: int): + if ( n == 0 ) or ( n == 1 ): + print(n, end="") + return + x(n // 2) + print(n % 2, end="") +x(255) diff --git a/recursion/recursion.py b/recursion/recursion.py index 11678b3..792fede 100644 --- a/recursion/recursion.py +++ b/recursion/recursion.py @@ -1,5 +1,8 @@ def fact(n: int): return n * fact(n-1) if n != 0 else 1
-def compound(p: int, r: float, y: int): return p if y == 0 else compound(p*r, r, y-1)
+
+def compound(p: float, r: float, y: int) -> float:
+ return p if y == 0 else compound(p*r, r, y-1)
+
def print_fib(a, b, l):
print(a)
print(b)
@@ -11,17 +14,27 @@ def print_next_fib(a: int, b: int, l: int): return
print(a+b)
return print_next_fib(b, a+b, l-1)
+
def fib_nth(n: int):
if n == 1:
return 0
if n == 2:
return 1
return fib_nth(n-1) + fib_nth(n-2)
-for i in range(1, 10):
- print(fact(i))
-print_fib(0, 1, 10)
-wage = 3000
-print(fib_nth(5))
+
+def draw_underline(l: int):
+ return '-'*l
+
+def test():
+ range_end = 15
+ print(f"Printing series of factorias from 1 to {range_end}\n{draw_underline(20)}")
+ for i in range(1, range_end):
+ print(fact(i), end=", " if i < range_end-1 else "\n")
+ fib_until = 15
+ print(f"\n\nPrinting fibonacci sequence to {fib_until}\n{draw_underline(20)}")
+ print_fib(0, 1, fib_until)
+ print(fib_nth(5))
+ print(compound(36800*4, 1.06, 10) / (43129-3170*12))
def payback(tuition: int, rate: int, salary: int):
years = 0
remaining = tuition * 4
@@ -32,6 +45,6 @@ def payback(tuition: int, rate: int, salary: int): remaining *= rate
print(f"Year {years}: remaining: {remaining}")
print(years)
-#payback(36800, 1.06, (43129-3170*12))
-print(compound(36800*4, 1.06, 10) / (43129-3170*12))
\ No newline at end of file +if __name__ == "__main__":
+ test()
|