Algorithms For Dummies


Understanding the Need to Sort and Search



Download 7,18 Mb.
Pdf ko'rish
bet274/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   270   271   272   273   274   275   276   277   ...   651
Bog'liq
Algorithms

 Understanding the Need to Sort and Search

However, you can combine the output of multiple hash functions when working 

with complex applications that rely on a large dataset. Simply sum the results of 

the various outputs after having done a multiplication on one or more of them. 

The sum of two hash functions treated in this way retains the qualities of the 

original  hash  functions  even  though  the  result  is  different  and  impossible  to 

recover as the original elements of the sum. Using this approach means that you 

have a brand-new hash function to use as your secret hash recipe for algorithms 

and applications.

The following code snippet relies on the 

hashlib

 package and the 

md5

 and 


sha1

 

hash algorithms. You just provide a number to use for the multiplication inside 



the hash sum. (Because numbers are infinite, you have a function that can pro-

duce infinite hashes.)

from hashlib import md5, sha1

  

def hash_f(element, i, length):



    """ Function to create many hash functions """

    h1 = int(md5(element.encode('ascii')).hexdigest(),16)

    h2 = int(sha1(element.encode('ascii')).hexdigest(),16)

    return (h1 

+ i*h2) % length

  

print (hash_f("CAT", 1, 10**5))



64018

  

print (hash_f("CAT", 2, 10**5))



43738

If  you  wonder  where  to  find  other  uses  of  hash  tables  around  you,  check  out 

Python’s dictionaries. Dictionaries are, in fact, hash tables, even though they have 

a smart way to deal with collisions and you won’t lose your data because two 

hashed keys casually have the same result. The fact that the dictionary index uses 

a hash is also the reason for its speed in checking whether a key is present. In 

addition, the use of a hash explains why you can’t use every data type as a key. The 

key you choose must be something that Python can turn into a hash result. Lists, 

for instance, are unhashable because they are mutable; you can change them by 

adding or removing elements. Nevertheless, if you transform your list into a 

string, you can use it as a key for a dictionary in Python.




Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   270   271   272   273   274   275   276   277   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish