aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-02-06 09:50:00 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-02-06 09:50:00 +0200
commit5e297261b05ed2c3feef0a892fc47e5fa06dbc4c (patch)
tree2ae9783dd2fc0efe42bc31e52e9984157c49d8a0
parent6fdd8b6737ed7e018900ceaf2fcec3d926839912 (diff)
parenta2289833e59c852b79532b47aebf17a33b95cb9c (diff)
downloadcs-y13-5e297261b05ed2c3feef0a892fc47e5fa06dbc4c.tar.gz
cs-y13-5e297261b05ed2c3feef0a892fc47e5fa06dbc4c.tar.bz2
cs-y13-5e297261b05ed2c3feef0a892fc47e5fa06dbc4c.zip
Merge branch 'main' of github.com:stvnliu/cs-y13
-rw-r--r--oop/oop.py32
-rw-r--r--recursion/recursion.py101
2 files changed, 83 insertions, 50 deletions
diff --git a/oop/oop.py b/oop/oop.py
new file mode 100644
index 0000000..4ef15d7
--- /dev/null
+++ b/oop/oop.py
@@ -0,0 +1,32 @@
+import math
+class shape:
+ def __init__(self):
+ pass
+ def area(self):
+ pass
+class circle(shape):
+ def __init__(self, r):
+ shape.__init__(self)
+ self._radius = r
+ def area(self):
+ return self._radius * self._radius * math.pi
+class rectangle(shape):
+ def __init__(self, side1, side2):
+ shape.__init__(self)
+ self._a = side1
+ self._b = side2
+ def area(self):
+ return self._a * self._b
+class square(shape):
+ def __init__(self, side):
+ shape.__init__(self)
+ self._side = side
+ def area(self):
+ return self._side ** 2
+
+sq = square(4)
+cir = circle(7)
+rect = rectangle(3,6)
+print(sq.area())
+print(cir.area())
+print(rect.area()) \ No newline at end of file
diff --git a/recursion/recursion.py b/recursion/recursion.py
index dd51531..f46be80 100644
--- a/recursion/recursion.py
+++ b/recursion/recursion.py
@@ -1,50 +1,51 @@
-def fact(n: int): return n * fact(n-1) if n != 0 else 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)
- print_next_fib(a, b, l-3)
-
-def print_next_fib(a: int, b: int, l: int):
- if l == 0:
- print(a+b)
- 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)
-
-def draw_underline(l: int):
- return '-'*l
-
-def test():
- range_end = 15
- print(f"Printing series of factorials 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
-
- while remaining > 0:
- remaining -= salary
- years += 1
- remaining *= rate
- print(f"Year {years}: remaining: {remaining}")
- print(years)
-
-if __name__ == "__main__":
- test()
+def fact(n: int): return n * fact(n-1) if n != 0 else 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)
+ print_next_fib(a, b, l-3)
+
+def print_next_fib(a: int, b: int, l: int):
+ if l == 0:
+ print(a+b)
+ 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)
+
+def draw_underline(l: int):
+ return '-'*l
+
+def test():
+ range_end = 15
+ print(f"Printing series of factorials 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
+
+ while remaining > 0:
+ remaining -= salary
+ years += 1
+ remaining *= rate
+ print(f"Year {years}: remaining: {remaining}")
+ print(years)
+
+if __name__ == "__main__":
+ test()
+