Hi I started learning python a week ago. I am trying to create a dictionary to replace characters. eg(dictionary["a"] = "%").
I have first created a list containng all characters order wise from ASCII 033 to 126. Then I tried to create a list of random characters in order to match the characters from the 1st list. But it is on an infinite loop. Does it take very long or is it my code?
Here is the code:
def replacement():
s=0
while s in range(93):
rep.append(chr(randint(33,126)))
if(letters[s] != rep[s]):
k=0
for replace in rep:
if replace == rep[s]:
k+=1
if(k<2):
s+=1
print(rep)
letters = []
rep = []
i=33
while i <= 126:
letters.append(chr(i))
i+=1
replacement()
letters = []onwards is supposed to be inside the function.swill be always betweenrange(93). Better using afor loop.repwith all the chars fromchr(33)tochr(126)in a random order. Sid's algorithm currently has some flaws, but even when those flaws are fixed this approach is very slow.repgets longer, it takes longer and longer to scan throughrepto check if the new char is already in it, and the odds of randomly generating a char that isn't inrepget lower and lower. Trying to randomly select the last couple of chars takes a very long time.