3.10 HACKS
Student led teaching on Abstraction. Teaching how various data types can use abstraction for computational efficiency.
python popcorn hack 1;
# Popcorn Hack: Accessing and Deleting Elements
# Create an empty list
a_list = []
# Function to add items to the list
def add_items():
while True:
user_input = input("Enter an item to add to the list (or type 'done' to finish): ")
if user_input.lower() == 'done': # Check if user wants to finish
break
a_list.append(user_input) # Append item to the list
# Function to display each item in the list
def display_items():
print("Items in the list:")
for item in a_list: # Iterate through each item in the list
print(item)
# Execute the functions
add_items()
display_items()
Items in the list:
hello
world
hello
javascript popcorn hack 1;
// Popcorn Hack: Accessing and Deleting Elements
let aList = []; // Create an empty array
let input; // Variable to store user input
// Function to simulate user adding items to the list
function addItems() {
do {
input = prompt("Enter an item to add to the list (or type 'done' to finish):");
if (input !== 'done') {
aList.push(input); // Add the item to the list
}
} while (input !== 'done');
console.log("Items in the list:", aList);
}
// Function to display the second element if it exists
function displaySecondElement() {
if (aList.length >= 2) {
console.log("The second element is:", aList[1]);
} else {
console.log("There is no second element.");
}
}
// Function to delete the second element if it exists and display updated list
function deleteSecondElement() {
if (aList.length >= 2) {
aList.splice(1, 1); // Remove the second element (at index 1)
console.log("Second element deleted. Updated list:", aList);
} else {
console.log("No second element to delete.");
}
}
// Execute the functions
addItems();
displaySecondElement();
deleteSecondElement();
python popcorn hack 2;
# Popcorn Hack: Assigning Values to a List and Finding Its Length
# Create a list of five favorite foods
favorite_foods = ["Pizza", "Sushi", "Ice Cream", "Tacos", "Pasta"]
# Add two more items to the list
favorite_foods.append("Burgers") # Adding the first new item
favorite_foods.append("Salad") # Adding the second new item
# Find and print the total number of items in the list
total_items = len(favorite_foods)
print("Total number of favorite foods:", total_items)
# Optional: Print the list of favorite foods
print("Favorite foods:", favorite_foods)
Total number of favorite foods: 7
Favorite foods: ['Pizza', 'Sushi', 'Ice Cream', 'Tacos', 'Pasta', 'Burgers', 'Salad']
javascript pop hack 2;
// Popcorn Hack: Look for the element "banana" in the list "fruits"
let fruits = ["apple", "banana", "orange"];
// Check if "banana" is in the fruits array
if (fruits.includes("banana")) {
console.log("Banana is in the list!");
} else {
console.log("Banana is not in the list.");
}
Homework Hacks:
// Hack: Use if else statements for an efficient way to determine the presence of a certain element
let numbers = [10, 20, 30, 40, 50];
let elementToFind = 30;
if (numbers.includes(elementToFind)) {
console.log(elementToFind + " is in the array.");
} else {
console.log(elementToFind + " is not in the array.");
}
# Python program that creates a list of numbers and prints the second element
numbers = [10, 20, 30, 40, 50]
print("The second element in the list is:", numbers[1])
The second element in the list is: 20
// JavaScript program that creates an array of numbers and logs the second element
let numbers = [10, 20, 30, 40, 50];
console.log("The second element in the array is:", numbers[1]);
# Python program to create a to-do list where users can add, remove, and view items
to_do_list = []
def display_list():
if to_do_list:
print("Your To-Do List:")
for index, item in enumerate(to_do_list, start=1):
print(f"{index}. {item}")
else:
print("Your to-do list is empty.")
def add_item(item):
to_do_list.append(item)
print(f"'{item}' has been added to your to-do list.")
def remove_item(item):
if item in to_do_list:
to_do_list.remove(item)
print(f"'{item}' has been removed from your to-do list.")
else:
print(f"'{item}' is not in your to-do list.")
# Example usage
add_item("Finish homework")
add_item("Go grocery shopping")
display_list()
remove_item("Finish homework")
display_list()
'Finish homework' has been added to your to-do list.
'Go grocery shopping' has been added to your to-do list.
Your To-Do List:
1. Finish homework
2. Go grocery shopping
'Finish homework' has been removed from your to-do list.
Your To-Do List:
1. Go grocery shopping
// JavaScript program to create a workout tracker
let workoutTracker = [];
function addWorkout(type, duration, calories) {
workoutTracker.push({ type, duration, calories });
console.log(`Workout added: ${type}, Duration: ${duration} mins, Calories burned: ${calories}`);
}
function displayWorkouts() {
if (workoutTracker.length > 0) {
console.log("Your Workout Log:");
workoutTracker.forEach((workout, index) => {
console.log(`${index + 1}. Type: ${workout.type}, Duration: ${workout.duration} mins, Calories burned: ${workout.calories}`);
});
} else {
console.log("No workouts logged.");
}
}
// Example usage
addWorkout("Running", 30, 300);
addWorkout("Cycling", 45, 400);
displayWorkouts();