Skip to the content.

3.10 hacks

3.10 popcorn hack and homework hacks

JavaScript

#Popcorn hack 1:
# Step 1: Create a list of your choosing
my_list = ["Popcorn", "Soda", "Candy", "Nachos", "Chocolate"]

# Step 2: Remove the last element from the list
my_list.pop()

# Step 3: Remove the first element from the list
my_list.pop(0)

# Step 4: Remove an element from the middle of the list (e.g., index 1)
my_list.pop(1)

# Step 5: Print the final list
print(my_list)

['Soda', 'Nachos']
#Popcorn Hack 2:
# Step 1: Create a list
my_list = ["Popcorn", "Soda", "Candy", "Nachos", "Chocolate"]

# Step 2: Reverse the list in place
my_list.reverse()

# Step 3: Print the reversed list
print(my_list)

['Chocolate', 'Nachos', 'Candy', 'Soda', 'Popcorn']
#Python Hack
#  Step 1: Create a list of numbers
numbers = [1, 2, 3, 4, 5, 6]

# Step 2: Use list comprehension to filter even numbers
even_numbers = [num for num in numbers if num % 2 == 0]

# Step 3: Print the even numbers
print(even_numbers)  # Output: [2, 4, 6]

[2, 4, 6]
#Javascript Hack
%%javascript
// Create an array of numbers
const numbers = [1, 2, 3, 4, 5];

// Use reduce to sum all the numbers in the array
const sum = numbers.reduce((accumulator, current) => accumulator + current, 0);

// Output the sum
console.log("Sum:", sum);  // Output: 15

// Create a mapping of integers to their word equivalents
const numberWords = {
    1: "one",
    3: "three",
    4: "four",
    8: "eight",
    9: "nine",
    77: "seventy-seven",
    80: "eighty"
};

// Create an array of integers to be converted to words
const integers = [1, 3, 8, 9, 77, 80];

// Convert the integers to words
let words = integers.map(num => numberWords[num]);
console.log("Numbers as words:", words);  // Output: ["one", "three", "eight", "nine", "seventy-seven", "eighty"]

// Sort the words alphabetically
words.sort();
console.log("Alphabetically sorted words:", words);  // Output: ["eight", "eighty", "nine", "one", "seventy-seven", "three"]

  Cell In[5], line 3
    // Create an array of numbers
    ^
SyntaxError: invalid syntax