jeudi 18 octobre 2018

How to fix small but relevant Python errors?

I'm currently attempting to make a coding program, imitating the "Library of Babel" website. Basically what it does now, is it generates a pseudo random string of letters based on a kew, and allows the user to search the "library" for a string (right now, this only allows for a maximum of four characters). The searching is where I have the trouble. Basically how I run the function (see below) is by finding a "key" value which will output a certain string. However sometimes when search for strings higher up in the alphabet, the key returns an altered string (i.e. "lurz--> lury","zzzz--> zzzy"). This seems to me like a rounding error- my code is attached below. How can I fix this?

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 = raw_input("Key: ")
library = ""
s = raw_input("Would you like a random, or exact location? (R or E)")
#if s.upper() == "E":
    #section = raw_input("section: ")
    #shelf = raw_input("shelf: ")
    #volume = raw_input("volume: ")
    #page = raw_input("page: ")
    #library = section + shelf + volume + page
getRandStr(key, library)
key = raw_input("Search")
getAndCheck(key)




Aucun commentaire:

Enregistrer un commentaire