Skip to the content.

3.2 hacks

PYTHON POPCORN HACKS

#POPCORN HACK 1

#Create set and preform opperations
my_set = {1, 2, 3, 4, 5}
my_set.add(6)
my_set.remove(2)
union_set = my_set.union({7, 8, 9})
my_set.clear()
set_with_duplicates = {1, 2, 2, 3, 3, 4}
length_of_set_with_duplicates = len(set_with_duplicates)

#Print result
print("Union with {7, 8, 9}: ", union_set)
print("Length of set with duplicates {1, 2, 2, 3, 3, 4}: ", length_of_set_with_duplicates)

Union with {7, 8, 9}:  {1, 3, 4, 5, 6, 7, 8, 9}
Length of set with duplicates {1, 2, 2, 3, 3, 4}:  4
#POPCORN HACK 2

#define all the functions to find the length, and replace and string and stuff
my_string = "Learning python is not fun"
string_length = len(my_string)
python_substring = my_string[9:15]
uppercase_string = my_string.upper()
replaced_string = my_string.replace("fun", "amazing")
reversed_string = my_string[::-1]

#print results from each action!
print("String Length: ", string_length)
print("Extracted 'Python': ", python_substring)
print("Uppercase String: ", uppercase_string)
print("Replaced String: ", replaced_string)
print("Reversed String: ", reversed_string)

String Length:  26
Extracted 'Python':  python
Uppercase String:  LEARNING PYTHON IS NOT FUN
Replaced String:  Learning python is not amazing
Reversed String:  nuf ton si nohtyp gninraeL
#POPCORN HACK 3

#preform operations
my_list = [3, 5, 7, 9, 11]
third_element = my_list[2]
my_list[1] = 6
my_list.append(13)
my_list.remove(9)
my_list.sort(reverse=True)

#print results
print("Third Element: ", third_element)
print("Modified List: ", my_list)

Third Element:  7
Modified List:  [13, 11, 7, 6, 3]
#POPCORN HACK 4

info1 = {
    "name": "Nora Ahadian",
    "email": "email@gmail.com",
    "phone number": "858-858-8558"
}

info2 = {
    "name": "Bob bobbie",
    "email": "bob@bobbie.com",
    "phone number": "123-456-7890"
}

print("Personal Info:", info1)
print("My Name is:", info1["name"])
print("Length of the Dictionary:", len(info1))
print("Type of the Dictionary:", type(info1))
print("Second Person's Info:", info2)

Personal Info: {'name': 'Nora Ahadian', 'email': 'email@gmail.com', 'phone number': '858-858-8558'}
My Name is: Nora Ahadian
Length of the Dictionary: 3
Type of the Dictionary: <class 'dict'>
Second Person's Info: {'name': 'Bob bobbie', 'email': 'bob@bobbie.com', 'phone number': '123-456-7890'}

PYTHON HOMEWORK HACK parts 1-9

def manage_personal_info():
    # Part 1: Create Personal Info (dict)
    personal_info = {
        "name": "John Doe",
        "age": 25,
        "location": "San Diego, CA"
    }
    
    # Part 2: Create a List of Activities (list) with corresponding costs
    activities_with_costs = {
        "hiking": 0.0,       # free
        "reading": 10.0,     # books cost
        "volleyball": 20.0,  # equipment/venue cost
        "swimming": 5.0      # pool cost
    }

    # Extract activities as a list from the dictionary
    activities = list(activities_with_costs.keys())

    # Part 3: Add Activities to Personal Info (dict and list)
    personal_info["activities"] = activities

    # Part 4: Check Availability of an Activity (bool)
    def is_activity_available(activity):
        return activity in activities
    
    # Example usage: checking if "hiking" is available
    hiking_available = is_activity_available("hiking")
    
    # Part 5: Total Number of Activities (int)
    total_activities = len(activities)
    
    # Part 6: Favorite Activities (tuple)
    favorite_activities = ("hiking", "volleyball")
    
    # Part 7: Add a New Set of Skills (set)
    skills = {"Map Reading", "Survival Skills", "First Aid", "Strength Training", "Coordination Drills", "Team Strategy"}
    
    # Part 8: Consider a New Skill (NoneType)
    new_skill = None
    
    # Part 9: Calculate Total Hobby and Skill Cost (float)
    skill_training_costs = {
        "Map Reading": 50.0,        # for hiking
        "Survival Skills": 100.0,   # for hiking
        "First Aid": 75.0,          # for all sports
        "Strength Training": 30.0,  # for voleyball
        "Coordination Drills": 20.0,# for volleybal
        "Team Strategy": 40.0       # for volleybal
    }
    
    # Calculate total hobby and skill cost
    total_cost = sum(activities_with_costs.values()) + sum(skill_training_costs.values())
    
    #summary of costs and skills 
    print(f"Total Hobby and Skill Cost: ${total_cost:.2f}")
    
    return {
        "personal_info": personal_info,
        "hiking_available": hiking_available,
        "total_activities": total_activities,
        "favorite_activities": favorite_activities,
        "skills": skills,
        "new_skill": new_skill,
        "total_cost": total_cost
    }

#print cost and dictonary
output = manage_personal_info()
print(output)
Total Hobby and Skill Cost: $350.00
{'personal_info': {'name': 'John Doe', 'age': 25, 'location': 'San Diego, CA', 'activities': ['hiking', 'reading', 'volleyball', 'swimming']}, 'hiking_available': True, 'total_activities': 4, 'favorite_activities': ('hiking', 'volleyball'), 'skills': {'Strength Training', 'Map Reading', 'First Aid', 'Coordination Drills', 'Team Strategy', 'Survival Skills'}, 'new_skill': None, 'total_cost': 350.0}

JAVASCRIPT POPCORN HACKS

%%js
//POPCORN HACK 1

//Create Set1
let Set1 = new Set([1, 5, 6]);

//Create Set2
let Set2 = new Set([4, 8, 11]);

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

//Add a number and remove the 1st number from Set1
Set1.add(7); 
Set1.delete(11);

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

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

<IPython.core.display.Javascript object>
%%js
//POPCORN HACK 2

//application
let application = {
    name: "Nora Ahadian", 
    age: 15,            
    experiences: ["CEO", "Manager"],
    money: 99999999
};

//Log dictionary
console.log("Application Dictionary:", application);

//Log the money
console.log("Money in the application:", application.money);

<IPython.core.display.Javascript object>

JAVASCRIPT HOMEWORK HACK

%%js

//Prompt for name, age, and experience
let name = prompt("What is your name?");
let age = prompt("How old are you?");
let experiences = prompt("List your experiences (separate by commas)");

//Store the inputs in a dictionary
let application = {
    name: name,
    age: age,
    experiences: experiences.split(",")  // Convert the experiences to an array
};

//Log user's input (the application dictionary)
console.log("User's Application:", application);

<IPython.core.display.Javascript object>