about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--scratch/data_structures_and_algorithms/memo.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/scratch/data_structures_and_algorithms/memo.py b/scratch/data_structures_and_algorithms/memo.py
new file mode 100644
index 000000000000..8195f32c931c
--- /dev/null
+++ b/scratch/data_structures_and_algorithms/memo.py
@@ -0,0 +1,19 @@
+import time
+import random
+
+memo = {}
+
+
+def f(x):
+    if x in memo:
+        print("Hit.\t\tf({})".format(x))
+        return memo[x]
+    else:
+        print("Computing...\tf({})".format(x))
+        time.sleep(0.25)
+        res = random.randint(0, 10)
+        memo[x] = res
+        return res
+
+
+[f(random.randint(0, 10)) for _ in range(10)]