3.1 Hacks | 3.2 Hacks | 3.3 Hacks | 3.4 Hacks | 3.5 Hacks | 3.8 Hacks | 3.10a Hacks | 3.10b Hacks | Sprint 2 Personal Learnings Blog | Sprint 2 Team Blog |
3.10b Hacks
Python and Javascript Popcorn and Homework Hacks for 3.10b
Python Popcorn and Homework Hacks
#Popcorn Hack 1
my_list = ['apple', 'banana', 'strawberry']
my_list.insert(-1, 'orange') # Inserts 'orange' at the second-to-last position
# Output the updated list
print(my_list)
['apple', 'banana', 'orange', 'strawberry']
# Popcorn Hack 2
# First list
list1 = ['apple', 'banana', 'strawberry']
# Second list
list2 = ['orange', 'pear', 'grape']
# Combine lists using the extend() method (popcorn hack)
list1.extend(list2)
# Output the combined list
print(list1)
['apple', 'banana', 'strawberry', 'orange', 'pear', 'grape']
# Popcorn Hack 2
# First list
list1 = ['apple', 'banana', 'strawberry']
# Second list
list2 = ['orange', 'pear', 'grape']
# Combine lists using the extend() method (popcorn hack)
list1.extend(list2)
# Output the combined list
print(list1)
['apple', 'banana', 'strawberry', 'orange', 'pear', 'grape']
# Homework Hack 1
# Step 1: Create an empty list to store grocery items
grocery_list = []
# Step 2: Input three grocery items and add them to the list
for i in range(3):
item = input(f"Enter grocery item {i + 1}: ")
grocery_list.append(item)
# Step 3: Display the current grocery list
print("\nCurrent Grocery List:", grocery_list)
# Step 4: Sort the list alphabetically and print the sorted list
grocery_list.sort()
print("\nSorted Grocery List:", grocery_list)
# Step 5: Remove one item specified by the user
item_to_remove = input("\nEnter an item to remove from the list: ")
if item_to_remove in grocery_list:
grocery_list.remove(item_to_remove)
print("\nUpdated Grocery List:", grocery_list)
else:
print(f"\n'{item_to_remove}' is not in the grocery list.")
Current Grocery List: ['veggies', 'milk', 'bread']
Sorted Grocery List: ['bread', 'milk', 'veggies']
Updated Grocery List: ['bread', 'milk']
# Homework Hack 2
original_list = list(range(1, 21))
print("Original List:", original_list)
even_numbers = [num for num in original_list if num % 2 == 0]
print("Even Numbers:", even_numbers)
Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Even Numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Homework Hack 3
grades = []
for i in range(3):
while True: # Loop until a valid integer is entered
try:
grade = int(input(f"Enter grade {i + 1}: "))
grades.append(grade)
break # Exit the loop if input is valid
except ValueError: # Handle invalid input
print("Please enter a valid integer.")
print("\nList of Grades:", grades)
passing_grades = [grade for grade in grades if grade > 60]
# Print this list
print("Grades above 60:", passing_grades)
List of Grades: [98, 40, 98]
Grades above 60: [98, 98]
# Homework Hack 4
numbers = list(range(1, 11))
print("Original List:", numbers)
numbers.sort(reverse=True)
print("Sorted in Descending Order:", numbers)
first_five = numbers[:5]
print("First Five Numbers:", first_five)
numbers.sort()
print("Sorted in Ascending Order:", numbers)
Original List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sorted in Descending Order: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
First Five Numbers: [10, 9, 8, 7, 6]
Sorted in Ascending Order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Javascript Popcorn and Homework Hacks
%%javascript
/* Popcorn Hack 1 */
// Create an array
let myArray = [1, 2, 3, 4, 5];
// Reverse the array
myArray.reverse();
// Output the reversed array
console.log(myArray);
<IPython.core.display.Javascript object>
%%javascript
// Create an empty array
let myArray = [];
// Add elements using unshift()
myArray.unshift(3); // [3]
myArray.unshift(2); // [2, 3]
myArray.unshift(1); // [1, 2, 3]
// Use the spread operator to add more elements
myArray = [0, ...myArray]; // [0, 1, 2, 3]
// Output the array
console.log(myArray);
<IPython.core.display.Javascript object>
%%javascript
// Original array
let numbers = [10, 25, 30, 47, 50, 65];
// Use filter() to create a new array with only even numbers
let evenNumbers = numbers.filter(num => num % 2 === 0);
// Output the new array
console.log(evenNumbers);
<IPython.core.display.Javascript object>
%%javascript
/* Homework Hack 1 */
// Step 1: Create an array with at least 5 values
let myArray = ['apple', 'banana', 'cherry', 'date', 'fig'];
// Step 2: Display the array using console.log()
console.log("Original Array:", myArray);
// Bonus: Use the reverse() popcorn hack to reverse the array
myArray = [...myArray].reverse(); // Create a copy and reverse it
// Display the reversed array
console.log("Reversed Array:", myArray);
<IPython.core.display.Javascript object>
%%javascript
/* Homework Hack 2 */
// Given array
const sports = ["soccer", "football", "basketball", "wrestling", "swimming"];
// Display values "soccer" and "wrestling" using their indexes
console.log("First sport:", sports[0]); // Accessing 'soccer'
console.log("Fourth sport:", sports[3]); // Accessing 'wrestling'
%%javascript
/* Homework Hack 3 */
// Create an array called choresList initialized with four items
let choresList = ["laundry", "dishes", "vacuuming", "grocery shopping"];
// Display the initial list
console.log("Initial chores list:", choresList);
// Using push() to add an item
choresList.push("dusting");
console.log("After push:", choresList);
// Using shift() to remove the first item
choresList.shift();
console.log("After shift:", choresList);
// Using pop() to remove the last item
choresList.pop();
console.log("After pop:", choresList);
// Using unshift() to add an item to the beginning
choresList.unshift("clean windows");
console.log("After unshift:", choresList);
// Bonus: Use the push() and spread operator popcorn hack to add multiple values
choresList.push(...["mopping", "organizing", "watering plants"]);
console.log("After pushing multiple values:", choresList);
%%javascript
/* Homework Hack 3 */
// Step 1: Create an array with ten random numbers (both even and odd)
const randomNumbers = [12, 7, 19, 24, 33, 8, 5, 10, 21, 40];
// Step 2: Function to count even numbers in the array
function countEvenNumbers(arr) {
let count = 0; // Initialize count to zero
for (let num of arr) {
if (num % 2 === 0) { // Check if the number is even
count++; // Increment the count if the number is even
}
}
return count; // Return the final count
}
// Step 3: Call the function and display the output
const evenCount = countEvenNumbers(randomNumbers);
console.log("Count of even numbers:", evenCount);