Hashing algorithm Simple review + design Randompool structure

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.