Skip to the content.

3.4 Hacks

hacks

3.4 Popcorn Hack

string = "Premier League is the best league"
string2 = "The match was intense and thrilling"
print("String 1: Premier League is the best league")
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 Length:", average_word_length(string))

def palindrome(input_string):
    string = input_string.replace(" ", "").lower()
    return string == string[::-1]

print("Palindrome or Not?", palindrome(string))

# For the second string
print("String 2: The match was intense and thrilling")
length2 = len(string2)
print("Length:", length2)
print("Vowel Count:", count_vowels(string2))
print("Average Word Length:", average_word_length(string2))
print("Palindrome or Not?", palindrome(string2))
String 1: Premier League is the best league
Length: 33
Vowel Count: 14
Average Word Length: 4.666666666666667
Palindrome or Not? False
String 2: The match was intense and thrilling
Length: 35
Vowel Count: 9
Average Word Length: 5.0
Palindrome or Not? False

3.4 hw hack

function passwordValidator(password) {
    // Check if password length is less than 8
    if (password.length < 8) {
        return "Password too short. Must be at least 8 characters.";
    }

    // Check if the password is all uppercase or all lowercase
    if (password === password.toLowerCase() || password === password.toUpperCase()) {
        return "Password must contain both uppercase and lowercase letters.";
    }

    // Check if the password contains at least one number
    if (!/\d/.test(password)) {
        return "Password must contain at least one number.";
    }

    // Optional: Replace "123" with "abc"
    password = password.replace("123", "abc");

    // Split password by spaces and join with dashes
    const customizedPassword = password.split(" ").join("-");

    return `Password is valid! Here’s a fun version: ${customizedPassword}`;
}

// Example usage
const password = "HelloWorld123";
console.log(passwordValidator(password));