summaryrefslogtreecommitdiff
path: root/Assignment 5/2a.py
blob: 0b55ceeafc6e1836a5d18af8240cfb1267e32744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import random, hashlib, collections

def duplicates(table):
    return [key for key, values in table.items() if len(values) > 1]

def hash(msg):                              # we use only the first 3 bytes, as a toy example
    return hashlib.md5(str(msg).encode('utf-8')).digest()[:3]

lookup = collections.defaultdict(list)      # lookup table
while duplicates(lookup) == []:             # as long as there are no duplicates 
    input = random.getrandbits(64)          # 64 bits is sufficient to assume no input occurs twice
    lookup[hash(input)].append(input)

print(len(lookup))