Hash review
The more important nature:
irreversible: When you know the hash value of x, you cannot find X;
no conflict: When you know X, you cannot find a Y so that the hash value of x and Y is the same
1 #Hash Feature:2 #1. Enter Unlimited3 #2. The output has poor4 #3. Input the same ==> output must be the same5 #4. There may be different input result output as ==> hash collision6 #5. The input sample is large enough to almost evenly distribute the ==> hash on the output sample.7 #MD 5 Barrel structure hashcode die n ===> evenly distributed on 0--n-18 9 #Design Randompool StructureTen #"topic" design a structure in which the following three functions are available: One #Insert (Key): Adds a key to the structure so that it does not join again. A #Delete (key): Removes a key that was originally in the structure. - #getrandom (): Equal probability randomly returns any key in the structure. - #"Requirements" the time complexity of the Insert, delete, and Getrandom methods is O (1) the ImportRandom - - - classRandompool: + def __init__(self): -Self.dict1 = Dict ()#dictionary 1:key: Store key value: stores the order of the current join, nth +Self.dict2 = Dict ()#Dictionary 2:key: Store count value: Store joined key ASelf.size =0 at - definsert (self, key): - ifKey not inchSelf.dict1: -Self.dict1[key] =self.size -Self.dict2[self.size] =Key -Self.size + = 1 in - defGetrandom (self): to ifSelf.size = =0: + returnNone -index =Int (Random.randrange (self.size)) the returnSelf.dict2[index] * $ defDelete (self, key):#When the specified key is deleted, the count of the key corresponding to dictionary 2 is removed,Panax Notoginseng ifKeyinchSelf.dict1:#The value of the maximum count corresponding to count, convenient getrandom () function -Delindex =Self.dict1[key] theLastkey =Self.dict2[self.size] + Self.dict1.pop (Key) ASelf.dict1[lastkey] =Delindex theSelf.dict2[delindex] =Lastkey + Self.dict2.pop (self.size) -Self.size-= 1
Hashing algorithm Simple review + design Randompool structure