我目前正在尝试制作一个编码程序,模仿“ Babel图书馆”网站。现在基本上可以做的是,它基于一个kew生成一个伪随机的字母字符串,并允许用户在“库”中搜索一个字符串(目前,最多只允许四个字符)。搜索是我遇到麻烦的地方。基本上,我如何运行该函数(参见下文)是通过找到一个“键”值来输出某个字符串。但是有时,当搜索字母中更高的字符串时,键会返回更改后的字符串
(即“ lurz-> lury”,“ zzzz-> zzzy”)。
重要编辑::如果您尝试运行代码,请忽略随机字母:输入搜索词后出现错误(仅在输入原始键后才会显示)。如果您在键输入中重新输入从搜索词中获得的键,则您返回的随机字母的前四个字母应该是您的搜索输入。
在我看来,这似乎是一个舍入错误-我的代码附在下面。我该如何解决?
from __future__ import division
import math
__author__ = 'asaflebovic'
def getFactor(x, var):
facs = []
i = 0
while i < x/2:
if i % x ==0:
facs.append(i)
i = i+1
print(i)
print(i)
i= i + 1
var = max(facs)
def getRandStr(x,y):
x = float(x)
x = ((1664525*x + 1013904223)) % 4294967296
#print(x)
nums = [x]
for i in range(1,2000):
x = ((1664525*nums[i-1] + 1013904223)) % 4294967296
nums.append(x)
#print(nums)
hold = ''.join(str(e) for e in nums)
hold = (str(hold).replace('.', ''))
hold = str(hold)
hold2 = ""
count = 0
count2 = ""
#print(hold)
mssg = []
for i in range(1,(int(math.ceil(len(hold)/2)))):
count = hold[(2*i)-2]
count2 = count
#print("countval:" + count)
count = hold[(2 * i) - 1]
#print("countval:"+count)
count2 = count2 + count
#print("count2val:"+count2)
hold2 = str(count2)
hold2 = int(hold2) % len(abc)
#print("each letter:"+abc[int(hold2)])
mssg.append(abc[hold2])
print(''.join(str(e) for e in mssg))
def getAndCheck(x):
holding = []
mods = []
cells = {}
for i in range(0,len(x)):
if x[i] in abc:
holding.append(((((len(abc)))+((abc.index(x[i]))%len(abc)))))
#print(len(abc))
#print((len(abc))+(abc.index(x[i])))
#print("in abc, it is "+str(holding))
else:
print("error: not found")
#print("len: "+str(len(holding)))
for i in range(0, len(holding)):
cells[((i + 4) - (i % 4)) / 4] = ""
for i in range(0, len(holding)):
cells[((i+4)-(i%4))/4] = str(cells[((i+4)-(i%4))/4])+str(holding[i])
#print("dict is: "+str(cells))
holding = 0
print(holding)
newkey = (int(cells[1]) + 4294967296 - 1013904223) / 1664525
print(newkey)
newmul = {}
for i in range(1,len(cells)):
newmul[i]=[((1664525 * ((int(cells[i])+1013904223)) - 4294967296))]
print(newmul)
print("Your key is "+str(newkey)+". DON'T LOSE IT.")
exacts = {}
for i in range (1, int(math.ceil(len(newmul)/4))):
exacts[(i+4)/4] = ""
abc = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", " ", "?", "!", "-", ",", "."]
key = input("Key: ")
library = ""
#s = raw_input("Would you like a random, or exact location? (R or E)")
#if s.upper() == "E":
#section = input("section: ")
#shelf = input("shelf: ")
#volume = input("volume: ")
#page = input("page: ")
#library = section + shelf + volume + page
getRandStr(key, library)
key = input("Search")
getAndCheck(key)