In [26]:
alphabet = list('abcdefghijklmnopqrstuvwxyz')
N = len(alphabet)
def to_num(s):
return [ord(ch) - 97 for ch in list(s)]
def to_char(n):
return ''.join([chr(int(d) + 97) for d in n])
def num_increment(l):
if l[-1] == N - 1:
return(num_increment(l[:-1]) + [0])
else:
l[-1] += 1
return l
def increment(s):
return to_char(num_increment(to_num(s)))
In [50]:
def straight(s):
l = to_num(s)
for i in range(len(s[:-2])):
if l[i] == l[i + 1] - 1 == l[i + 2] - 2:
return True
return False
def iol(s):
if ('i' in s) or ('l' in s) or ('o' in s):
return False
return True
def non_overlapping(s):
i = 0
count = 0
while (i < len(s) - 1):
if s[i] == s[i + 1]:
count += 1
i += 2
else:
i += 1
return (count >= 2)
In [53]:
def next_password(password):
p = increment(password)
while (not straight(p)) or (not iol(p)) or (not non_overlapping(p)):
p = increment(p)
return p
In [58]:
def test():
p = 'hijklmmn'
assert(straight(p) and not iol(p))
p = 'abbceffg'
assert(non_overlapping(p) and not straight(p))
p = 'abbcegjk'
assert(not non_overlapping(p))
p = 'abcdefgh'
assert(next_password(p) == 'abcdffaa')
p = 'ghijklmn'
assert(next_password(p) == 'ghjaabcc')
In [59]:
%%time
test()
In [60]:
%%time
next_password('cqjxjnds')
Out[60]:
In [61]:
%%time
next_password('cqjxxyzz')
Out[61]: