diff --git a/aufgaben/einstellung/Pr_5/ha/münzwechsel.py b/aufgaben/einstellung/Pr_5/ha/münzwechsel.py new file mode 100644 index 0000000..d37c0ec --- /dev/null +++ b/aufgaben/einstellung/Pr_5/ha/münzwechsel.py @@ -0,0 +1,52 @@ +#unendlich viele münzen 1,2,5 + +from timing import timing + + +# Sophias +def coins(n): + if n == 0: + return 1 + if n < 0: + return 0 + return coins(n - 1) + coins(n - 2) + coins(n - 5) + + +# Test für n = 3 +print("Ergebnis für n = 3:", coins(3)) # Gibt 3 aus (eigentlich nur 2 Münz-Kombinationen) + +# Laufzeit messen +print("\nOhne Memoization:") +for n in [30, 35, 37]: + timing(coins)(n) + + +# Memoization +memo = {} + +def coins_memo(n): + if n == 0: + return 1 + if n < 0: + return 0 + if n not in memo: + memo[n] = coins_memo(n - 1) + coins_memo(n - 2) + coins_memo(n - 5) + return memo[n] + + +# Laufzeit messen +print("\nMit Memoization:") +for n in [30, 35, 37, 100]: + timing(coins_memo)(n) + +"""Ergebnis für n = 3: 3 +Ohne Memoization: +Dauer: 1.3143s +Dauer: 17.8723s +Dauer: 49.2706s + +Mit Memoization: +Dauer: 0.0000s +Dauer: 0.0000s +Dauer: 0.0000s +Dauer: 0.0000s""" \ No newline at end of file diff --git a/aufgaben/einstellung/Pr_5/ha/timing.py b/aufgaben/einstellung/Pr_5/ha/timing.py new file mode 100644 index 0000000..91cfbff --- /dev/null +++ b/aufgaben/einstellung/Pr_5/ha/timing.py @@ -0,0 +1,11 @@ +import time +import functools + +def timing(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + start = time.perf_counter() + result = func(*args, **kwargs) + print(f"Dauer: {time.perf_counter() - start:.4f}s") + return result + return wrapper \ No newline at end of file diff --git a/aufgaben/einstellung/Pr_5/pr/Quadratzahlen.py b/aufgaben/einstellung/Pr_5/pr/Quadratzahlen.py index e9346de..dccc57e 100644 --- a/aufgaben/einstellung/Pr_5/pr/Quadratzahlen.py +++ b/aufgaben/einstellung/Pr_5/pr/Quadratzahlen.py @@ -1,20 +1,22 @@ class Squares: - def __init__(self, numbers): - self.numbers = numbers - self.iterator = 1 + def __init__(self, n): + self.n = n + self.current = 1 + def __iter__(self): - self.iterator = 1 return self + def __next__(self): - if self.iterator > self.numbers: + if self.current > self.n: raise StopIteration - result = self.iterator * self.iterator - self.iterator += 1 + result = self.current ** 2 + self.current += 1 return result -if __name__ == "__main__": - #numbers = list[1,4,9,16] - sq = Squares(4) - print(list(sq)) - print(list(sq)) \ No newline at end of file +# 1. Prüfung mit list(Squares(4)) +it = Squares(4) +print(list(it)) # 1. Durchlauf -> Ausgabe: [1, 4, 9, 16] + +# 2. Was passiert beim zweiten Durchlauf desselben Iterators? +print(list(it)) # 2. Durchlauf -> Ausgabe: [] \ No newline at end of file diff --git a/aufgaben/einstellung/Pr_5/pr/laufzeit_messung.py b/aufgaben/einstellung/Pr_5/pr/laufzeit_messung.py deleted file mode 100644 index e69de29..0000000 diff --git a/aufgaben/einstellung/Pr_5/pr/treppenhaus.py b/aufgaben/einstellung/Pr_5/pr/treppenhaus.py index e69de29..616fda9 100644 --- a/aufgaben/einstellung/Pr_5/pr/treppenhaus.py +++ b/aufgaben/einstellung/Pr_5/pr/treppenhaus.py @@ -0,0 +1,37 @@ +#Warum rechnet ways(n) richtig? +#Der erste Schritt ist entweder 1 Stufe oder 2 Stufen +# Zusammen sind das ways(n-1) + ways(n-2) + +#Warum ist es ab n >= 40 langsam? +#wird millionenfach doppelt berechnet.exponentielle Laufzeit O(2^n) + +from Pr_5.ha.timing import timing + +# Reeeborg-Funktion +def ways(n): + if n <= 2: + return n + return ways(n - 1) + ways(n - 2) + + +# Laufzeit messen +print("Ohne Memoization:") +for n in [30, 35, 37]: + timing(ways)(n) + + +# MIT Memoization +memo = {} + +def ways_memo(n): + if n <= 2: + return n + if n not in memo: + memo[n] = ways_memo(n - 1) + ways_memo(n - 2) + return memo[n] + + +# 4. Laufzeit messen +print("Mit Memoization:") +for n in [30, 35, 37, 100]: + timing(ways_memo)(n) \ No newline at end of file diff --git a/aufgaben/einstellung/Pr_5/pr/ungeradezahlen.py b/aufgaben/einstellung/Pr_5/pr/ungeradezahlen.py index b9be417..c220a36 100644 --- a/aufgaben/einstellung/Pr_5/pr/ungeradezahlen.py +++ b/aufgaben/einstellung/Pr_5/pr/ungeradezahlen.py @@ -1,35 +1,8 @@ -def odd(limit): - """Generiert ungerade Zahlen bis zu einem exklusiven Limit.""" - zahl = 1 - while zahl < limit: - yield zahl - zahl += 2 - -# --- Code für die Ausgabe im Terminal --- - -print("--- Start der Schleife ---") - -# Wir rufen die Funktion mit dem Limit 10 auf -for ungerade_zahl in odd(10): - print(f"Gefundene Zahl: {ungerade_zahl}") - -print("--- Ende der Schleife ---") +def odd(n: int): + for i in range(1, n + 1, 2): + yield i - - - -"""сlass OddIterator: - def __init__(self, limit): - self.limit = limit - self.aktuelle_zahl = 1 - - def __iter__(self): - return self - - def __next__(self): - if self.aktuelle_zahl >= self.limit: - raise StopIteration - ergebnis = self.aktuelle_zahl - self.aktuelle_zahl += 2 - return ergebnis """ \ No newline at end of file +# Test +print(list(odd(7))) # [1, 3, 5, 7] +print(list(odd(8))) # [1, 3, 5, 7] \ No newline at end of file diff --git a/aufgaben/einstellung/Pr_5/pr/unpacking.py b/aufgaben/einstellung/Pr_5/pr/unpacking.py index e69de29..1f4d056 100644 --- a/aufgaben/einstellung/Pr_5/pr/unpacking.py +++ b/aufgaben/einstellung/Pr_5/pr/unpacking.py @@ -0,0 +1,13 @@ +t = (1, 2, 3) + +print(t) # Ausgabe: (1, 2, 3) +print(*t) # Ausgabe: 1 2 3 + +print(t, 4) # Ausgabe: (1, 2, 3) 4 +print(*t, 4) # Ausgabe: 1 2 3 4 + + +#Was ist dann *args in einer Funktion? +#Bei def wrapper(*args): der Stern sammelt beliebig viele einzelne Argumente beim Funktionsaufruf ein +# und packt sie in ein Tupel namens args. +#Und wenn man drinnen func(*args) aufruft, packt der Stern dieses Tupel wieder in einzelne Argumente aus \ No newline at end of file diff --git a/aufgaben/einstellung/pr_aufgaben/04_k_länge.py b/aufgaben/einstellung/pr_aufgaben/04_k_länge.py index db1f6fd..073e357 100644 --- a/aufgaben/einstellung/pr_aufgaben/04_k_länge.py +++ b/aufgaben/einstellung/pr_aufgaben/04_k_länge.py @@ -2,4 +2,4 @@ words = ["Apfel", "Banane", "Kirsche", "Kiwi", "Melone"] #summe der Wortlänge, #summe= sum(len(word) for word in words if word.startwith("K")) #print(summe) -summe = filter(lambda word: str.startswith("K"), words, ) \ No newline at end of file +summe = filter(lambda word: str.startswith("K"), words, )