phase 2
Phase 1 was writing a password check program. This phase was using more Python conditions to shorten the code
#!/usr/bin/env python3
import re
import textwrap
def window(seq, window_size=3):
size = len(seq) - window_size + 1
for i in range(size):
yield seq[i:i + window_size]
def no_repeating_three_characters(s):
return not any(
all(x == chunk[0] for x in chunk)
for chunk in window(s)
)
conditions = (
("12+ characters long", lambda x: len(x) >= 12),
("2+ vowels", re.compile(r"[aeiouAEIOU].*[aeiouAEIOU ]").search),
("no 3+ repeating letters", no_repeating_three_characters),
("2+ consonants", re.compile(r"[bcdfghjklmnpqrstvwxyzBCDFGHIJKLMNPQRSTVWXYZ].*[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ ]").search),
("2+ numbers", re.compile(r"[0-9].*[0-9]").search),
("2+ special characters", re.compile(r"[!@#$%^&*()<>?].*[!@#$%^&*()<>?]").search)
)
tests = (
'ShortTest77',
'CorrectSize12',
'ThreeeRepeating',
'UPPERCASETESt12',
'UppercaseTest12',
'UppErcaseTest12',
'UppErcaseTest12',
'UppErcASeTests2',
'UpPercAsETest34',
'Up%PercAsETest34',
'Up%PercAsE#Test34'
)
def main():
for test in tests:
print(f"Testing: {test}")
messages = []
for label, fn in conditions:
check = fn(test)
message = textwrap.indent(
f"{label}: {'Passed' if bool(check) else 'Failed'}",
prefix=" + "
)
messages.append(message)
if not check:
break
print("\n".join(messages))
if __name__ == '__main__':
main()
/home/michael/PycharmProjects/Justin/venv/bin/python /home/michael/PycharmProjects/Justin/Justin1.py
Testing: ShortTest77
+ 12+ characters long: Failed
Testing: CorrectSize12
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: ThreeeRepeating
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Failed
Testing: UPPERCASETESt12
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: UppercaseTest12
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: UppErcaseTest12
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: UppErcaseTest12
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: UppErcASeTests2
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Failed
Testing: UpPercAsETest34
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: Up%PercAsETest34
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Failed
Testing: Up%PercAsE#Test34
+ 12+ characters long: Passed
+ 2+ vowels: Passed
+ no 3+ repeating letters: Passed
+ 2+ consonants: Passed
+ 2+ numbers: Passed
+ 2+ special characters: Passed
Process finished with exit code 0