Skip to the content.
3.1 and 3.4 3.2 3.3 and 3.5 3.8 3.10

3.1 Hacks

hacks

DevOps

Popcorn Hack 3.1

var1 = input("How would you greet someone? e.g. 'Hello'")
var2 = input("What is your name?:")
var3 = input("What is your age?")

my_list = [var1, var2, var3]

my_dict = {
    var1,
    var2,
    var3
}

print(my_list)
print(my_dict)

print(var1 + ", " + "my name is " + var2 + " and I am " + var3 + " years old.")
['Hey', 'Gavin', '16']
{'Gavin', 'Hey', '16'}
Hey, my name is Gavin and I am 16 years old.

Popcorn Hack 3.4

string = "This is a string number 1"
string2 = "This is a string number 2"
print("String 1: This is a string number 1")
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: This is a string number 2")
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: This is a string number 1
Length: 25
Vowel Count: 6
Average Word Count: 3.3333333333333335
Palindrome or Not? False
String 2: This is a string number 2
Length: 25
Vowel Count: 6
Average Word Count: 3.3333333333333335
Palindrome or Not? False

Homework Hack 3.1

%%js
let fullName = "John Doe";
let age = 28;
let email = "john.doeh@gmail.com";
let hobby = "Food Tasting";
let dietaryPreferences = "Vegan";

// Generate a unique ID: use the length of fullName and age, combined with the first two letters of the hobby.
let uniqueID = fullName.length + age + hobby.slice(0, 2).toUpperCase();

// Store data in an object
let userInfo = {
    fullName: fullName,
    age: age,
    email: email,
    hobby: hobby,
    dietaryPreferences: dietaryPreferences,
    uniqueID: uniqueID
};

// Display the information
let formattedInfo = `
Personal Info:
- Full Name: ${userInfo.fullName}
- Age: ${userInfo.age}
- Email: ${userInfo.email}
- Hobby: ${userInfo.hobby}
- Dietary Preferences: ${userInfo.dietaryPreferences}
- Unique ID: ${userInfo.uniqueID}
`;

console.log(formattedInfo);
<IPython.core.display.Javascript object>

Homework Hack 3.4

%%html
<html>
<head>
    <title>Password Validator</title>
    <style>
        .result { color: blue; }
        .error { color: red; }
    </style>
</head>
<body>
    <h1>Password Validator</h1>
    
    <form id="passwordForm">
        <label for="password">Enter Password:</label>
        <input type="password" id="password" name="password" required>
        <button type="submit">Validate</button>
    </form>
    
    <div id="result" class="result"></div>

    <script>
        function passwordValidator(password) {
            // check for min length of 8 characters
            if (password.length < 8) {
                return "Password too short. Must be at least 8 characters.";
            }

            // check for both uppercase and lowercase letters
            if (password === password.toLowerCase() || password === password.toUpperCase()) {
                return "Password must contain both uppercase and lowercase letters.";
            }

            // check for at least one digit
            if (!/\d/.test(password)) {
                return "Password must contain at least one number.";
            }

            // check for at least one special character
            if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
                return "Password must contain at least one special character.";
            }

            // disallow common weak passwords
            const commonPasswords = ['password', '123456', 'qwerty', 'letmein', 'welcome'];
            if (commonPasswords.includes(password.toLowerCase())) {
                return "Password is too common. Choose a more secure password.";
            }

            // check for repeated characters
            if (/(\w)\1\1/.test(password)) {
                return "Password contains repeated characters (e.g., aaa or 111). Avoid repetition.";
            }

            let customizedPassword = words.join("-");
            return `Password is valid! This is your password: ${customizedPassword}`;
        }

        // form submission
        document.getElementById('passwordForm').addEventListener('submit', function(event) {
            event.preventDefault();

            const password = document.getElementById('password').value;
            
            const result = passwordValidator(password);
            
            const resultElement = document.getElementById('result');
            if (result.includes('Password is valid')) {
                resultElement.classList.remove('error');
                resultElement.classList.add('valid');
            } else {
                resultElement.classList.remove('valid');
                resultElement.classList.add('error');
            }
            resultElement.textContent = result;
        });
    </script>
</body>
</html>
Password Validator

Password Validator