about summary refs log tree commit diff
path: root/scratch
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-01T13·40+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-01T13·40+0100
commitec7c8516f7443e76233aa0f44d06c074f88498f0 (patch)
tree98270fbb36c1d9556cedf5ab5395d5c4d460e6b7 /scratch
parent011f7aeaecd716cb2cb94b7fa0d19be86d81cf0a (diff)
Implement part 1/3 for "Memo"
After hearing from a Jane Street recruiter, I decided to dust off some of the
DS&As knowledge. I found this article online, which outlines an example problem
called "Memo":

https://blog.janestreet.com/what-a-jane-street-dev-interview-is-like/

Here's part 1 of the solution in Python.
Diffstat (limited to 'scratch')
-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 0000000000..8195f32c93
--- /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)]