new
This commit is contained in:
1 parent
571cddedba
commit
1934fdfc63
8 files changed
+134
-46
No files matched your search
@@ -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"""
|
||||
Reference in new issue
Block a user