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.
This commit is contained in:
parent
011f7aeaec
commit
ec7c8516f7
1 changed files with 19 additions and 0 deletions
19
scratch/data_structures_and_algorithms/memo.py
Normal file
19
scratch/data_structures_and_algorithms/memo.py
Normal file
|
@ -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)]
|
Loading…
Reference in a new issue