Skip to content

Commit

Permalink
Implemented next_password function
Browse files Browse the repository at this point in the history
  • Loading branch information
micknudsen committed Dec 10, 2023
1 parent fbc9b37 commit 86d7361
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 2015/11/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def is_valid(password: str) -> bool:
)


def increment_password(password: str) -> str:
letters = list(password[::-1])
for i, letter in enumerate(letters):
if letter == "z":
letters[i] = "a"
else:
letters[i] = chr(ord(letter) + 1)
break
return "".join(letters[::-1])


def next_password(password: str) -> str:
while True:
password = increment_password(password)
if is_valid(password):
return password


class TestCode(unittest.TestCase):
def test_contains_increasing_straight(self) -> None:
self.assertTrue(contains_increasing_straight(password="hijklmmn"))
Expand All @@ -62,6 +80,12 @@ def test_is_valid(self) -> None:
self.assertTrue(is_valid(password="abcdffaa"))
self.assertTrue(is_valid(password="ghjaabcc"))

def test_increment_password(self) -> None:
self.assertEqual(increment_password("xx"), "xy")
self.assertEqual(increment_password("xy"), "xz")
self.assertEqual(increment_password("xz"), "ya")
self.assertEqual(increment_password("ya"), "yb")

def test_next_password(self) -> None:
self.assertEqual(next_password(password="abcdefgh"), "abcdffaa")
self.assertEqual(next_password(password="ghijklmn"), "ghjaabcc")

0 comments on commit 86d7361

Please sign in to comment.