For one place that I interviewed at (for a Python developer position) I worked with one of the devs on two Project Euler problems, one being problem 79. We talked through different approaches and came up with solutions together, but we coded separately. He seemed impressed that we were able to get through two problems. After the interview, he asked me to clean up the code and submit it, which you can see below. He never got back to me after I sent him the code. I'd like to know what's wrong with my code and not make the same mistakes again.
Problem 79:
A common security method used for online banking is to ask the user for three random characters from a passcode. For example, if the passcode was 531278, they may ask for the 2nd, 3rd, and 5th characters; the expected reply would be: 317.
The text file, keylog.txt, contains fifty successful login attempts.
Given that the three characters are always asked for in order, analyse the file so as to determine the shortest possible secret passcode of unknown length.
# problem79.py
# Project Euler
# Nicolas Hahn
# list of keys -> passcode string
# at each iteration through list,
def passcodeDerivation(keys):
answer = ''
while len(keys) > 0:
firsts = []
rests = []
for key in keys:
# split first digits, rest of the key into two lists
f, r = key[0], key[1:]
if f not in firsts:
firsts += f
rests += r
for f in firsts:
# if f has never been seen except as the first digit of a key
# it must be the next one in the passcode
if f not in rests:
newkeys = []
# take out digit we just found from all keys
for key in keys:
newkeys.append(key.replace(f,''))
# remove empty keys
keys = [k for k in newkeys if k != '']
answer += f
break
return answer
# filename -> list of keys
def loadKeys(keyFile):
keys = []
with open(keyFile,'r') as f:
for line in f:
keys.append(line.replace('\n',''))
return keys
def main():
keys = loadKeys('problem79keylog.txt')
# correct answer = 73162890
print(passcodeDerivation(keys))
if __name__ == "__main__":
main()