about summary refs log tree commit diff
path: root/scratch/data_structures_and_algorithms/memo.py
blob: 8195f32c931c0ae6af0dae119ec92472be10bb83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)]