Python Material Installation Dokumentation PythonKara Grundlagen Namensräume Funktionen Testen Strings Dictionary Listen Zeitmessungen Zufall Bits+Bytes Module Sockets xml serielle Schnittstelle Python in HTML Turtle xturtle Tkinter OOP Threads Zusicherungen exe Patterns GnuPlot Goto MySQL CGI Dateien Exceptions Grafik Mathematik Fischertechnik Unicode funktional Iterator Sound C Debugger regex Pfade Docstrings Django Bluetooth format Bytecode signal
Pfad: Startseite / Fächer / Informatik / Python / Dictionary
Autor: mk
19.01.2011 16:36
2536
Dictionary

Beispiel 1

Dict1 trainer.py

Anwendung

Problem

Rekursive Berechnungen mit hohem Aufwand - www.inf-schule.de

Lösung

Memoization - das Prinzip

def f(x):
    return x*x

fmemo = {}
def fm(x):
    if not x in fmemo:
        fmemo[x] = x*x
    return fmemo[x]

Tests

>>> f(5)
25
>>> fm(5)
25
>>> fm(3)
9
>>> fm(7)
49
>>> fmemo
{3: 9, 5: 25, 7: 49}
>>> fm(3.1)
9.610000000000001
>>> fm(-0.0071)
5.041000000000001e-05
>>> fmemo
{3.1: 9.610000000000001, 3: 9, -0.0071: 5.041000000000001e-05, 5: 25, 7: 49}
Aufgabe

Vergleiche Lösungen mit und ohne Memoizing durch Zeitmessungen.

memoization.py

Links