Seite 1 von 1

Suche minimal perfect hash Funktion für Python

Verfasst: Freitag 4. Juli 2014, 11:29
von paupau90
Hallo an alle,
ich suche eine minimal perfect hash Funktion.

Wenn jemand zufällig was dazu weiß bitte posten :-). Danke

Re: Suche minimal perfect hash Funktion für Python

Verfasst: Freitag 4. Juli 2014, 12:19
von Hyperion
Wie wär 's damit:

Code: Alles auswählen

def hash_booleans(value):
    hash = int(value)
    if hash < 0 or hash > 1:
        raise Exception("{} is not hashable".format(value))
    return hash


hash_booleans(True)
> 1

hash_booleans(False)
> 0

hash_booleans(1)
> 1

hash_booleans(2)
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-19-872544668d15> in <module>()
----> 1 hash_booleans(2)

<ipython-input-15-24fbe305ed6d> in hash_booleans(value)
      2     hash = int(value)
      3     if hash < 0 or hash > 1:
----> 4         raise Exception("{} is not hashable".format(value))
      5     return hash
:mrgreen:

Re: Suche minimal perfect hash Funktion für Python

Verfasst: Freitag 4. Juli 2014, 12:31
von EyDu
Ich erweitere das mal auf Integer:

Code: Alles auswählen

def hash_int(i):
    return i
Oder falls der Hash-Wert >= 0 sein soll:

Code: Alles auswählen

def hash_int(i):
    return -2*i-1 if i < 0 else 2*i
:mrgreen:

Re: Suche minimal perfect hash Funktion für Python

Verfasst: Freitag 4. Juli 2014, 12:52
von cofi
@paupau90: Falls das aus den bisherigen Antworten nicht klar wird: Deine Frage laesst sich nicht beantworten.
PHF sind domaenenspezifisch ... oder eben eine Identitaetsfunktion.