出于纯粹的无聊,我决定在python中编写一个散列算法。一切正常,但我有几个问题:
结果相似(例如hash(1234)
与[{1}}有一个字符差异)
形成重复序列(在较小的输入上更明显)
我使用的大部分值都是随机的,我没有编写哈希算法的经验,但我想尝试一下。我能解决我遇到的两个问题吗?
hash(4321)
请注意,def mash(n):
l = [int(x) for x in list(str(n))]
m = []
for i in range(len(l)):
if pow(len(l) >> 1, (i + 1) << 3) % (i + 1) == 0 and i != 0:
m[-1] = int(str(m[-1]) + str(l[i]))
else:
m.append(l[i])
return m
def hash(content, key=None):
"""Hashes the content (either a string or number"""
o = [0] * 128
if type(content) is float:
while content % 1 != 0:
content = content * 10
content = int(content)
if type(content) is str:
content = int(''.join([str(ord(x)) for x in list(content)]))
if type(content) is int:
content = mash(content)
_ = []
for i in range(len(content)):
_.append((chr(((content[i] * (i << 3)) % 94) + 33)))
# 33 - 126
content = ''.join(_)
elif type(content) is not str:
raise Exception
z = [ord(x) for x in list(content)]
for i in range(len(z)):
if z[i] % 2 == 0:
n = next((i for i, x in enumerate(o) if not x and i % 2 == 0), 0)
else:
n = next((i for i, x in enumerate(o) if not x and i % 2 == 1), 0)
o[n] = content[i]
o = [str(x) for x in o]
o.reverse()
init_n = o.count('0')
while o.count('0') > 5:
n = next((i for i, x in enumerate(o) if x == '0'), None)
o[n] = chr(((pow(init_n >> 1, o.count('0'))) % 94) + 33)
o.reverse()
return ''.join(o)
参数尚未实现。希望在尝试其他功能之前解决现有问题。