Hallo an alle,
ich suche eine minimal perfect hash Funktion.
Wenn jemand zufällig was dazu weiß bitte posten . Danke
Suche minimal perfect hash Funktion für Python
- Hyperion
- Moderator
- Beiträge: 7478
- Registriert: Freitag 4. August 2006, 14:56
- Wohnort: Hamburg
- Kontaktdaten:
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
encoding_kapiert = all(verstehen(lesen(info)) for info in (Leonidas Folien, Blog, Folien & Text inkl. Python3, utf-8 everywhere))
assert encoding_kapiert
assert encoding_kapiert
Ich erweitere das mal auf Integer:
Oder falls der Hash-Wert >= 0 sein soll:
Code: Alles auswählen
def hash_int(i):
return i
Code: Alles auswählen
def hash_int(i):
return -2*i-1 if i < 0 else 2*i
Das Leben ist wie ein Tennisball.
- cofi
- Python-Forum Veteran
- Beiträge: 4432
- Registriert: Sonntag 30. März 2008, 04:16
- Wohnort: RGFybXN0YWR0
@paupau90: Falls das aus den bisherigen Antworten nicht klar wird: Deine Frage laesst sich nicht beantworten.
PHF sind domaenenspezifisch ... oder eben eine Identitaetsfunktion.
PHF sind domaenenspezifisch ... oder eben eine Identitaetsfunktion.
Michael Markert ❖ PEP 8 Übersetzung ❖ Tutorial Übersetzung (3.x) ⇒ Online-Version (Python 3.3) ❖ Deutscher Python-Insider ❖ Projekte