Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.4 String Operations
3.4 Team Teach String Operations
- Measuring String Length
- Sample Hack - String Analyzation
- Sample Hack - Password Validator
- Simple Password Validator
- Advanced Password Validator
____________________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________________
Measuring String Length
Sample Hack - String Analyzation
- Determine metrics about strings (length, chars, palidrome(?), etc.)
string = "Brawl Stars is a fun game!"
string2 = "Star Brawl war Brats"
print("String 1: Brawl Stars is a fun game!")
length = len(string)
print("Length:", length)
def count_vowels(input_string):
vowels = 'aeiouAEIOU'
count = 0
for char in input_string:
if char in vowels:
count += 1
return count
print("Vowel Count:", count_vowels(string))
def average_word_length(input_string):
words = input_string.split()
if not words:
return 0
total_length = sum(len(word) for word in words)
average_length = total_length / len(words)
return average_length
print("Average Word Count:", average_word_length(string))
def palindrome(input_string):
string = input_string.replace(" ","").lower()
return string == string[::-1]
print("Palindrome or Not?", palindrome(string))
print("String 2: Star Brawl war Brats")
length2 = len(string2)
print("Length:", length2)
print("Vowel Count:", count_vowels(string2))
print("Average Word Count:", average_word_length(string2))
print("Palindrome or Not?", palindrome(string2))
String 1: Brawl Stars is a fun game!
Length: 26
Vowel Count: 7
Average Word Count: 3.5
Palindrome or Not? False
String 2: Star Brawl war Brats
Length: 20
Vowel Count: 4
Average Word Count: 4.25
Palindrome or Not? True
Sample Hack - Password Validator
The goal of this homework hack is to create a password validator. A couple examples are given below (Simple and Advanced)
Simple Password Validator
def password_validator(password):
if len(password) < 8:
return "Password too short. Must be at least 8 characters."
if password == password.lower() or password == password.upper():
return "Password must contain both uppercase and lowercase letters."
if not any(char.isdigit() for char in password):
return "Password must contain at least one number."
# Optional
password = password.replace("123", "abc")
words = password.split(" ")
customized_password = "-".join(words)
return f"Password is valid! Here’s a fun version: {customized_password}"
# Example usage
password = "HelloWorld123"
print(password_validator(password))
Password is valid! Here’s a fun version: HelloWorldabc
print(password_validator("HELLO123"))
Password must contain both uppercase and lowercase letters.
print(password_validator("Hello123"))
Password is valid! Here’s a fun version: Helloabc
Advanced Password Validator
import re
def password_validator(password):
if len(password) < 8:
return "Password too short. Must be at least 8 characters."
if password == password.lower() or password == password.upper():
return "Password must contain both uppercase and lowercase letters."
if not any(char.isdigit() for char in password):
return "Password must contain at least one number."
if not re.search(r"[!@#$%^&*()_+]", password):
return "Password must contain at least one special character (e.g. !, @, #, etc.)"
common_passwords = ["password", "123456", "letmein", "qwerty"]
if password.lower() in common_passwords:
return "Password is too common. Choose something less predictable."
sequential_patterns = ["123", "abc", "xyz"]
for pattern in sequential_patterns:
if pattern in password.lower():
return "Password should not contain sequential characters like '123' or 'abc'."
score = 0
if len(password) >= 10:
score += 1
if re.search(r"[A-Z]", password) and re.search(r"[a-z]", password):
score += 1
if re.search(r"\d", password):
score += 1
if re.search(r"[!@#$%^&*()_+]", password):
score += 1
strength = "Weak"
if score == 2:
strength = "Medium"
elif score >= 3:
strength = "Strong"
password = password.replace("Hello", "Hi")
words = password.split(" ")
customized_password = "-".join(words)
return f"Password is valid and {strength}! Here’s a fun version: {customized_password}"
# Example usage
password = "HelloWorld13475!"
print(password_validator(password))
Password is valid and Strong! Here’s a fun version: HiWorld13475!