python,recursion , Python recursive function not recursing

## Question:

Tag: python,recursion

I'm trying to solve a puzzle, which is to reverse engineer this code, to get a list of possible passwords, and from those there should be one that 'stands out', and should work

``````function checkPass(password) {
var total = 0;
var charlist = "abcdefghijklmnopqrstuvwxyz";
for (var i = 0; i < password.length; i++) {
var counttwo = (charlist.indexOf(countone));
counttwo++;
total *= 17;
total += counttwo;
}
if (total == 248410397744610) {
//success//
} else {...
``````

I wrote something in python that I think should work, I reverse engineered the order of which it adds and multiplies, and has it try every character to see if it properly divides into an even number.

``````from random import shuffle

def shuffle_string(word):
word = list(word)
shuffle(word)
return ''.join(word)

charlist = "abcdefghijklmnopqrstuvwxyz"
total = 248410397744610
temp = total
runtime = 20

def runlist():
global charlist
global total
global temp
global runtime
for i in range(25):
if (temp - (i+1))  % 17 == 0:
temp -= i+1
temp = temp /17

if temp == 0:
temp = total
runtime += 1
charlist = shuffle_string(charlist)
if runtime < 21:
runlist()
else:
runlist()
``````

But when I try to run it I only get

``````deisx

Process finished with exit code 0
``````

I'm wondering why my function isn't recursing properly, because it looks like it should from what I see. try and run it yourself, and see what happens.

There should be multiple solutions for this puzzle (I think?), and I was planning on making it be able repeat until it gets all solutions, but I'm a little lost on why it just runs through every letter once, then dies.

EDIT: Revised code to actually recurse, but now I get no output, and still finish with exit code 0.

EDIT 2: Revised code again to fix a mistake

The encoding process repeats the following:

• multiply the current total by 17
• add a value (a = 1, b = 2, ..., z = 26) for the next letter to the total

So at any point, the total is a multiple of 17 plus the value of the final letter. So each step in the recursive solver must remove the final letter then divide the result by 17.

However, because the multiplier is 17 and there are 26 characters in the alphabet, some of the remainder values may be produced by more than one letter - this is where many passwords may give rise to the same solution.

For example:

• encoding "a" gives a total of 1, and 1 % 17 = 1
• encoding "r" gives a total of 18, and 18 % 17 = 1

So if the current remainder is 1, then the encoded letter may be either "a" or "r". I think in your solution you only ever look for the first of these cases, and miss the second.

In pseudo code, my function to solve this would look something like:

``````function decodePassword(total, password)
if total == 0
return
end if

var rem = total / 17       # integer division - truncate
var char1 = total % 17     # potential first character
var char2 = char1 + 17     # potential second character

# char1 values 1-16 map to letters a-p
if char1 >= 1
end if

# char2 values 17-26 map to letters q-z
if rem > 0 && char2 <= 26
end if

end function
``````

If it helps, the answer you are looking for is 12 chars long, and probably not printable in this forum!

HTH

# Related:

## How do variables inside python modules work?

python,module,python-module
I am coming from a Java background with Static variables, and I am trying to create a list of commonly used strings in my python application. I understand there are no static variables in python so I have written a module as follows: import os APP_NAME = 'Window Logger' APP_DATA_FOLDER_PATH...

## Python Popen - wait vs communicate vs CalledProcessError

python,python-2.7,error-handling,popen
Continuing from my previous question I see that to get the error code of a process I spawned via Popen in python I have to call either wait() or communicate() (which can be used to access the Popen stdout and stderr attributes): app7z = '/path/to/7z.exe' command = [app7z, 'a', dstFile.temp,...

## Python - Opening and changing large text files

python,replace,out-of-memory,large-files
I have a ~600MB Roblox type .mesh file, which reads like a text file in any text editor. I have the following code below: mesh = open("file.mesh", "r").read() mesh = mesh.replace("[", "{").replace("]", "}").replace("}{", "},{") mesh = "{"+mesh+"}" f = open("p2t.txt", "w") f.write(mesh) It returns: Traceback (most recent call last): File...