Rhea Rajashekhar P4 Popcorn Hacks 3.10 variables
# Python Popcorn Hack 1: Reverse an Array
my_array = [1, 2, 3, 4, 5]
print("Original array:", my_array)
# Reverse the array
my_array.reverse()
print("Reversed array:", my_array)
# Python Popcorn Hack 2
my_array = [3, 4, 5]
# Use list concatenation to mimic the unshift effect
new_element = [2] # The element to add
my_array = new_element + my_array
print("Array after unshift:", my_array)
# Python Popcorn Hack 3
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Use filter to create a new array with only even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even numbers using filter:", even_numbers)
# Python Popcorn Hack 4
my_list = [1, 2, 3]
# Insert values at negative indexes
my_list.insert(-1, 1.5) # Inserts 1.5 before the last element
my_list.insert(-2, 1.2) # Inserts 1.2 before the second last element
print("List after inserting with negative indexes:", my_list)
# Python Popcorn Hack 5
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# Use extend to add list2 to list1
list1.extend(list2)
print("Combined list using extend:", list1)
# Python Popcorn Hack 6
my_list = ['apple', 'banana', 'cherry', 'strawberry', 'grape']
# Remove an item using remove()
my_list.remove('banana')
print("List after removing 'banana':", my_list)
# Remove an item using pop()
my_list.pop(2) # Removes 'cherry'
print("List after popping index 2:", my_list)
# Remove an item using del
del my_list[0] # Removes 'apple'
print("List after deleting index 0:", my_list)
%% js
// JS HW Hack 1
let array = [1, 2, 3, 4, 5];
// Display the array using console.log()
console.log("Original array:", array);
// Reverse the array using the reverse() method
array.reverse();
// Display the reversed array
console.log("Reversed array:", array);
%% js
// JS HW Hack 2
let sports = ["soccer", "football", "basketball", "wrestling", "swimming"];
// Access and display values using their indexes
console.log(sports[0]); // Output: soccer
console.log(sports[3]); // Output: wrestling
%% js
// JS HW Hack 3
let choresList = ["laundry", "dishes", "vacuuming", "grocery shopping"];
console.log("Initial chores list:", choresList);
// Add an item using push()
choresList.push("cleaning the bathroom");
console.log("After push:", choresList);
// Remove the first item using shift()
choresList.shift();
console.log("After shift:", choresList);
// Remove the last item using pop()
choresList.pop();
console.log("After pop:", choresList);
// Add an item to the beginning using unshift()
choresList.unshift("mopping");
console.log("After unshift:", choresList);
// Bonus: Add multiple values using push() and spread operator
choresList.push(...["taking out trash", "dusting", "organizing"]);
console.log("After adding multiple chores:", choresList);
%% js
// JS HW Hack 4
let randomNumbers = [12, 7, 19, 24, 35, 8, 42, 5, 14, 27];
console.log("Random Numbers:", randomNumbers);
// Function to count even numbers
function countEvenNumbers(arr) {
let count = 0; // Initialize count
for (let number of arr) {
if (number % 2 === 0) {
count++; // Increment count if even
}
}
return count;
}
// Call the function and display the count of even numbers
let evenCount = countEvenNumbers(randomNumbers);
console.log("Count of even numbers:", evenCount);