Skip to the content.

3.2 Homework and Popcorn hacks

3.2 Hacks

Popcorn Hacks

Hack #1 (with bonus)

# Step 1: Create a Set
my_set = {1, 2, 3, 4, 5}
print("Initial Set:", my_set)

# Step 2: Add an Element
my_set.add(6)
print("Set after adding 6:", my_set)

# Step 3: Remove an Element
my_set.remove(2)
print("Set after removing 2:", my_set)

# Step 4: Union of Sets
union_set = my_set.union({7, 8, 9})
print("Union of sets:", union_set)

# Step 5: Clear the Set
my_set.clear()
print("Set after clearing:", my_set)

# Bonus: Create a set with duplicates
duplicate_set = {1, 2, 2, 3, 3, 4}
print("Set with duplicates removed:", duplicate_set)
print("Length of the set:", len(duplicate_set))

Hack #2 (with bonus)

# Step 1: Create a String
my_string = "Learning Python is not fun"
print("Original String:", my_string)

# Step 2: String Length
length_of_string = len(my_string)
print("Length of the string:", length_of_string)

# Step 3: String Slicing
python_substring = my_string[9:15]
print("Extracted 'Python':", python_substring)

# Step 4: String Uppercase
uppercase_string = my_string.upper()
print("Uppercase String:", uppercase_string)

# Step 5: String Replace
replaced_string = my_string.replace("fun", "good")
print("String after replacement:", replaced_string)

# Bonus: Reverse the string using slicing
reversed_string = my_string[::-1]
print("Reversed String:", reversed_string)

Hack #3 (with bonus)

# Step 1: Create a List
my_list = [3, 5, 7, 9, 11]
print("Original List:", my_list)

# Step 2: Access an Element
third_element = my_list[2]
print("Third element:", third_element)

# Step 3: Modify an Element
my_list[1] = 6
print("List after modifying second element:", my_list)

# Step 4: Add an Element
my_list.append(13)
print("List after appending 13:", my_list)

# Step 5: Remove an Element
my_list.remove(9)
print("List after removing 9:", my_list)

# Bonus: Sort the list in descending order
my_list.sort(reverse=True)
print("List sorted in descending order:", my_list)

Hack #4 (with bonus)

# Step 1: Create a Dictionary
personal_info = {
    "name": "John Doe",
    "email": "johndoe@example.com",
    "phone number": "123-456-7890"
}

# Step 2: Print out the Dictionary
print("Personal Info Dictionary:", personal_info)

# Step 3: Print out the name from the dictionary
print("My Name is:", personal_info["name"])

# Step 4: Print the Length
print("Length of the Dictionary:", len(personal_info))

# Step 5: Print the Type
print("Type of the Dictionary:", type(personal_info))

# Bonus: Add another dictionary and print their values
another_person_info = {
    "name": "Jane Smith",
    "email": "janesmith@example.com",
    "phone number": "987-654-3210"
}

print("\nAnother Person's Info:")
print("Name:", another_person_info["name"])
print("Email:", another_person_info["email"])
print("Phone Number:", another_person_info["phone number"])

Homework hacks

Hack #1

# Part 1: Create Personal Info (dict)
personal_info = {
    "full_name": "John Doe",
    "years": 30,
    "location": "New York",
    "favorite_food": "Pizza"
}
print("Part 1 - Personal Info:", personal_info)

# Part 2: Create a List of Activities (list)
activities = ["Reading", "Cycling", "Cooking"]
print("\nPart 2 - Activities:", activities)

# Part 3: Add Activities to Personal Info (dict and list)
personal_info["activities"] = activities
print("\nPart 3 - Updated Personal Info:", personal_info)

# Part 4: Check Availability of an Activity (bool)
activity_available = False  # Example: "Cycling" is not available today
print(f"\nPart 4 - Is Cycling available today? {activity_available}")

# Part 5: Total Number of Activities (int)
total_activities = len(activities)
print(f"\nPart 5 - I have {total_activities} activities.")

# Part 6: Favorite Activities (tuple)
favorite_activities = ("Reading", "Cycling")  # Top 2 favorite activities
print("\nPart 6 - Favorite Activities:", favorite_activities)

# Part 7: Add a New Set of Skills (set)
skills = {"Python", "Public Speaking", "Photography"}
print("\nPart 7 - Skills Set:", skills)

# Part 8: Consider a New Skill (NoneType)
new_skill = None
print("\nPart 8 - New Skill:", new_skill)

# Part 9: Calculate Total Hobby and Skill Cost (float)
cost_per_activity = 5
cost_per_skill = 10
total_cost = total_activities * cost_per_activity + len(skills) * cost_per_skill
print(f"\nPart 9 - Total Cost for Hobbies and Skills: ${total_cost:.2f}")

JavaScript popcorn hacks

Hack #1

%%js

// Step 1: Create a set called Set1 of 3 numbers
let Set1 = new Set([1, 2, 3]);

// Step 2: Create a set called Set2 of different 3 numbers
let Set2 = new Set([4, 5, 6]);

// Log both sets separately
console.log("Set1:", Set1);
console.log("Set2:", Set2);

// Step 3: Add a number to Set1 and remove the 1st number (1 in this case)
Set1.add(7);
Set1.delete(1);

// Log the updated Set1
console.log("Updated Set1:", Set1);

// Step 4: Log the union of both Set1 and Set2
let unionSet = new Set([...Set1, ...Set2]);
console.log("Union of Set1 and Set2:", unionSet);

Hack #2

%%js

// Step 1: Create a Dictionary called "application"
let application = {
    name: "Alice Smith",
    age: 28,
    experiences: ["Software Developer", "Project Manager", "UX Designer"],
    money: 1500
};

// Step 2: Log the dictionary to make sure it works
console.log("Application Dictionary:", application);

// Step 3: Log only the dictionary's money
console.log("Money:", application.money);

Homework Hacks

Hack #1

%%js

// Function to collect applicant information
function collectApplicantInfo() {
    // Step 1: Ask the applicant for their name
    let name = prompt("Please enter your name:");

    // Step 2: Ask the applicant for their age
    let age = prompt("Please enter your age:");

    // Step 3: Ask the applicant for their experiences
    let experiences = prompt("Please list your experiences (separated by commas):");

    // Step 4: Store inputs in a dictionary (object)
    let application = {
        name: name,
        age: age,
        experiences: experiences.split(',').map(exp => exp.trim()) // Split experiences into an array
    };

    // Step 5: Log the user's input
    console.log("Application Dictionary:", application);
}

// Call the function to run the application
collectApplicantInfo();