Skip to the content.

3.10 (b) hacks

3.10.1

%%js
//POPCORN HACK 1

//array of numbers
let numbers = [10, 20, 30, 40, 50];

//reverse using reverse()
numbers.reverse();

// Print reversed array
console.log("Reversed array:", numbers);

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

//array of numbers
let numbers = [20, 30, 40, 50];

//use unshift() to add a new element at the beginning of the array
let new_number = 10;
numbers.unshift(new_number);

//Print updated array
console.log("Array after unshift operation:", numbers);

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

//array of numbers
let numbers = [10, 15, 20, 25, 30, 35, 40];

//Use filter() to filter only even numbers
let even_numbers = numbers.filter(number => number % 2 === 0);

// Print the filtered array (list of even numbers)
console.log("Filtered array with even numbers:", even_numbers);

<IPython.core.display.Javascript object>

3.10.2

#POPCORN HACK 1

# Create a list of strings/fruits
fruits = ["Apple", "Banana", "Mango"]

# Use the insert() method with negative indexes to add values
fruits.insert(-1, "Orange")  # Insert at the second-to-last position
fruits.insert(-2, "Grapes")  # Insert at the third-to-last position

# Print updated list
print("Updated list:", fruits)

Updated list: ['Apple', 'Banana', 'Grapes', 'Orange', 'Mango']
#POPCORN HACK 2

# Create two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Use the extend() method to add list2 to list1
list1.extend(list2)

# Print the combined list
print("Combined list:", list1)
Combined list: [1, 2, 3, 4, 5, 6]
#POPCORN HACK 3

# Create a list of items
my_list = ["Apple", "Banana", "Cherry", "Date", "Fig", "pinapple"]

# Method 1: Using remove()
my_list.remove("Banana")

# Method 2: Using pop()
my_list.pop(2)

# Method 3: Using del
del my_list[0]

# Print the updated list
print("Updated list:", my_list)

Updated list: ['Cherry', 'Elderberry', 'Fig']

HOMEWORK HACKS

3.10.3

# HOMEWORK 1

#Create an empty list to store grocery items
grocery_list = []

#Input three grocery items
for i in range(3):
    item = input(f"Enter grocery item {i+1}: ")
    grocery_list.append(item)

#Display the current grocery list
print("\nCurrent Grocery List:")
print(grocery_list)

#Sort the list alphabetically and print
grocery_list.sort()
print("\nSorted Grocery List:")
print(grocery_list)

#Remove one item with user input
item_to_remove = input("\nEnter the item you want to remove: ")
if item_to_remove in grocery_list:
    grocery_list.remove(item_to_remove)
    print("\nUpdated Grocery List after removal:")
    print(grocery_list)
else:
    print(f"\nItem '{item_to_remove}' not found in the list.")

Current Grocery List:
['apple', 'cereal', 'banana']

Sorted Grocery List:
['apple', 'banana', 'cereal']

Updated Grocery List after removal:
['banana', 'cereal']
#HOMEWORK 2

#Create a list of integers from 1 to 20
numbers = list(range(1, 21))

#Print original list
print("Original List:")
print(numbers)

#take out only even numbers
even_numbers = [num for num in numbers if num % 2 == 0]

#Print new list
print("\nList of Even Numbers:")
print(even_numbers)

Original List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

List of Even Numbers:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
#HOMEWORK 3

#Create empty list
grades = []

#Input three grades to add to list
for i in range(3):
    grade = int(input(f"Enter grade {i + 1}: "))
    grades.append(grade)

#Print the list of grades
print("All grades:", grades)

#Create a new list with only grades > 60
passing_grades = [grade for grade in grades if grade > 60]

#Print list of passing grades
print("Passing Grades:", passing_grades)

All grades: [50, 70, 99]
Passing Grades: [70, 99]
#HOMEWORK 4

#Create a list of numbers from 1 to 10
numbers = list(range(1, 11))

#Print original list
print("Original list:", numbers)

#Sort the list highest to lowest
numbers.sort(reverse=True)
print("List in descending order:", numbers)

#Slice the list to the first five numbers and print
first_five = numbers[:5]
print("First five numbers:", first_five)

#Sort the list from lowest to highest
numbers.sort()
print("List in ascending order:", numbers)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List in descending order: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
First five numbers: [10, 9, 8, 7, 6]
List in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3.10.4

%%js

//HOMEWORK 1

//Create an array of 5 values
let myArray = [10, 20, 30, 40, 50];

//Display original array
console.log("Original array:", myArray);

//Reverse the array with reverse()
myArray.reverse();

//Display reversed array
console.log("Reversed array:", myArray);

%%js
//HOMEWORK 2

//Initialize array
let sports = ["soccer", "football", "basketball", "wrestling", "swimming"];

//Display "soccer" and "wrestling" using their indexes
console.log(sports[0]);  // soccer at index 0
console.log(sports[3]);  // wrestling at index 3

%%js
//HOMEWORK 3

//Initialize the array of four items
let choresList = ["dishes", "laundry", "vacuum", "groceries"];

//Display original list
console.log("Original list:", choresList);

//Use push() to add item to list
choresList.push("take out trash");
console.log("After push (add 'take out trash'):", choresList);

//Use shift() to remove first item
choresList.shift();
console.log("After shift (remove first item):", choresList);

//Use pop() to remove the last item
choresList.pop();
console.log("After pop (remove last item):", choresList);

//Use unshift() to add a item to the beginning of the array
choresList.unshift("make bed");
console.log("After unshift (add 'make bed'):", choresList);

//Use push() to add multiple values at once
choresList.push(...["clean windows", "mop floor", "dust furniture"]);
console.log("After using push() and spread operator to add multiple items:", choresList);

%%js
//HOMEWORK 4

//Create array with ten numbers
let randomNumbers = [12, 5, 8, 21, 34, 7, 18, 13, 26, 9];

//Function to count how many even numbers are in the array
function countEvenNumbers(arr) {
    let evenCount = 0;
    
    // Iterate through the array and count even numbers
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 === 0) {
            evenCount++;
        }
    }
    
    //Return the count of even numbers
    return evenCount;
}

//Call the function and display the result
let evenNumberCount = countEvenNumbers(randomNumbers);
console.log("Count of even numbers:", evenNumberCount);

<IPython.core.display.Javascript object>