Arrays and Lists!
js popcorn hacks:
//3.10.1 js popcorn hack 1: reverse an array
var names = ["kushi", "nora", "maryam", "joanna"];
names.reverse();
console.log(names); //output: ["joanna", "maryam", "nora", "kushi"]
//3.10.1 js popcorn hack 2: push and spread operator
var fruit = ["apple", "banana", "kiwi"];
var moreFruit = ["mango", "pineapple"];
fruit.push(...moreFruit);
console.log(fruit); // Output: ["apple", "banana", "kiwi", "mango", "pineapple"]
var moreMoreFruit = ["orange", "dragonfruit"]
fruit.unshift(...moreMoreFruit);
console.log(fruit); // output: ["orange", "dragonfruit", "apple", "banana", "kiwi"]
//3.10.1 js popcorn hack 3: filter() method
var animals = ["giraffee", "dog", "cat", "koala", "kangaroo", "shark", "mouse", "elephant"];
var longNameCount = animals.filter(animal => animal.length > 4).length;
console.log(longNameCount); // Output: [6]
js homework hacks:
//3.10.4 js homework hacks
// Problem #1: Basic Array Creation
var silverware = ["fork", "spoon", "plate", "cup", "bowl", "knife"];
console.log(silverware);
// Reverse bonus
silverware.reverse();
console.log("Reversed silverware:", silverware);
// Problem #2: Accessing Elements
var sports = ["soccer", "football", "basketball", "wrestling", "swimming"];
var first = sports[0]; // "soccer"
var second = sports[3]; // "wrestling"
console.log("First sport:", first);
console.log("Second sport:", second);
// Problem #3: Adding and Removing Items
var choresList = ["laundry", "dishes", "clean room", "take out trash"];
console.log("Original choresList:", choresList);
// Adding first chore using unshift
choresList.unshift("wake up");
console.log("After unshift:", choresList);
// Adding last chore using push
choresList.push("brush teeth");
console.log("After push:", choresList);
// Removing the first chore using shift
choresList.shift();
console.log("After shift:", choresList);
// Removing the last chore using pop
choresList.pop();
console.log("After pop:", choresList);
// Problem #4: Iteration and Conditionals
var randomNumbers = [9, 27, 45, 48, 34, 29, 14, 28, 43, 13];
// Function to count even numbers
function countEvenNumbers(numbers) {
var evenCount = 0; // Initialize a counter for even numbers
// Iterate through the array
for (var i = 0; i < numbers.length; i++) {
// Check if the number is even
if (numbers[i] % 2 === 0) {
evenCount++; // Increment the counter if even
}
}
return evenCount; // Return the count of even numbers
}
var evenCount = countEvenNumbers(randomNumbers);
console.log("Amount of even numbers: " + evenCount);
js output screenshot:
python popcorn hacks:
#3.10.2 python popcorn hack 1: insert method
numbers = [ 4, 5, 6]
print("original list:", numbers)
numbers.insert (-1,9)
numbers.insert(-2,8)
numbers.insert(-3,7)
print("new list:", numbers)
original list: [4, 5, 6]
new list: [4, 5, 7, 8, 9, 6]
#3.10.2 python popcorn hack 2: extend
numbers = [1, 2, 3]
more_numbers = [4, 5, 6]
print("original numbers:", numbers)
numbers.extend(more_numbers)
print("using extend: ", numbers)
original numbers: [1, 2, 3]
using extend: [1, 2, 3, 4, 5, 6]
#3.10.2 python popcorn hack 3: removing using del
large_numbers = [100, 200, 300, 400, 500, 600, 700, 800, 900]
print("original numbers: ", large_numbers)
del large_numbers[1]
del large_numbers[2]
del large_numbers[3]
print("after deletion:", large_numbers)
original numbers: [100, 200, 300, 400, 500, 600, 700, 800, 900]
after deletion: [100, 300, 500, 700, 800, 900]
python homework hacks:
#3.10.3 python homework hacks
#problem 1: grocery list
grocery_list = []
items = ["lettuce", "bread", "milk"]
# Insert each item into the grocery list individually
for item in items:
grocery_list.insert(len(grocery_list), item)
print("Original grocery list:", grocery_list)
grocery_list.sort() #sort alphabetically
print("Alphabetical grocery list:", grocery_list)
#problem 2: even numbers
integers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print("original list:", integers)
even_numbers = [num for num in integers if num % 2 == 0]
print("even numbers: ", even_numbers)
#problem 3: student grades
student_grades = []
grades = [85, 58, 72]
#print all grades:
for item in grades:
student_grades.insert(len(student_grades), item)
print("student grades: ", student_grades)
#print only grades above 60:
passing_grades = [grade for grade in student_grades if grade > 60]
print ("grades above 60: ", passing_grades)
#problem 4: list operations
numberss = [1,2,3,4,5,6,7,8,9,10]
print("original list: ", numberss)
first_five = numberss[:5]
print("first five: ", first_five)
numberss.sort(reverse = True)
print("decending order: ", numberss)
numberss.sort()
print("ascending order: ", numberss)
Original grocery list: ['lettuce', 'bread', 'milk']
Alphabetical grocery list: ['bread', 'lettuce', 'milk']
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]
student grades: [85, 58, 72]
grades above 60: [85, 72]
original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
first five: [1, 2, 3, 4, 5]
decending order: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]