player_one = "Rhea"
player_two = "Hannah"
player_three = "Rowan"
player_four = "Gaheera"
3.1.2 Popcorn hack Python:
fruits = {
"apple": {
"color": "red",
"taste": "sweet",
"season": "fall"
},
"banana": {
"color": "yellow",
"taste": "sweet",
"season": "year-round"
},
"orange": {
"color": "orange",
"taste": "citrus",
"season": "winter"
},
"grape": {
"color": "purple",
"taste": "sweet",
"season": "summer"
},
"lemon": {
"color": "yellow",
"taste": "sour",
"season": "year-round"
}
}
# Example usage: print details about apple
print(fruits["apple"])
3.1.4 Popcorn Hack Javascript:
// Create an array with mixed data types
let studentInfo = ["Rhea", 21, true, 3.8];
// Step 1: Use console.log() to display the entire array
console.log("Student Info Array:", studentInfo);
// Step 2: Use individual index access to display each element in the array
console.log("Name:", studentInfo[0]); // Outputs: Name: Rhea (String)
console.log("Age:", studentInfo[1]); // Outputs: Age: 21 (Number)
console.log("Is Enrolled:", studentInfo[2]); // Outputs: Is Enrolled: true (Boolean)
console.log("GPA:", studentInfo[3]); // Outputs: GPA: 3.8 (Number)
3.1.3 Homework Python:
// Declare variables using let and const
// 'let' is used for variables that can change
// 'const' is used for variables that should not change
const firstName = "Rhea"; // const because the name won't change
let age = 20; // let because the age might change later
const favoriteSubject = "Math"; // const since the subject remains the same for this example
// Define another variable using let
let averageGrade = 86; // let because the average grade can change
// Step 1: Using console.log() to print out the values of the variables
console.log("Name:", firstName); // Outputs: Name: Rhea
console.log("Age:", age); // Outputs: Age: 20
console.log("Favorite Subject:", favoriteSubject); // Outputs: Favorite Subject: Math
console.log("Average Grade:", averageGrade); // Outputs: Average Grade: 86
// Step 2: Create a short function that strings these variables together into a sentence
function studentIntroduction() {
// Using template literals (backticks) to insert variables into a string
let introduction = `Hi, my name is ${firstName}. I am ${age} years old, my favorite subject is ${favoriteSubject}, and my average grade is ${averageGrade}.`;
console.log(introduction); // Output the introduction sentence
}
// Call the function to see the sentence in the console
studentIntroduction();
// Step 3: Let's update the 'age' and 'averageGrade' variables
age = 21; // Updating the age
averageGrade = 90; // Updating the average grade
// Output the updated values
console.log("Updated Age:", age); // Outputs: Updated Age: 21
console.log("Updated Average Grade:", averageGrade); // Outputs: Updated Average Grade: 90
// Call the function again to see the updated sentence
studentIntroduction();
3.1.4 Homework Python:
# Input scores as variables
score_1 = 85
score_2 = 90
score_3 = 78
score_4 = 92
score_5 = 88
# Step 1: Add the scores together using basic addition
total_score = score_1 + score_2 + score_3 + score_4 + score_5
# Step 2: Calculate the average grade by dividing the total by the number of scores
average_grade = total_score // 5 # Using integer division (//) to get the average as an integer
# Step 3: Create a list to store student information
student_info = ["Rhea", 20, average_grade] # Name, Age, Average Grade
# Step 4: Print the student information
print("Student Name:", student_info[0])
print("Student Age:", student_info[1])
print("Average Grade:", student_info[2])
# Another example student with a different set of scores
# Input scores as variables
score_6 = 95
score_7 = 82
score_8 = 89
score_9 = 76
score_10 = 91
# Step 1: Add the scores together
total_score_2 = score_6 + score_7 + score_8 + score_9 + score_10
# Step 2: Calculate the average grade
average_grade_2 = total_score_2 // 5
# Step 3: Create a list for another student
student_info_2 = ["Rowan", 22, average_grade_2]
# Step 4: Print the second student's information
print("\nStudent Name:", student_info_2[0])
print("Student Age:", student_info_2[1])
print("Average Grade:", student_info_2[2])
3.4.2 Popcorn Javascript:
// Lyrics of "Espresso"
let lyrics = `
Now he's thinkin' 'bout me every night, oh
Is it that sweet? I guess so
Say you can't sleep, baby, I know
That's that me espresso
Move it up, down, left, right, oh
Switch it up like Nintendo
Say you can't sleep, baby, I know
That's that me espresso
I can't relate to desperation
My 'give-a-damns' are on vacation
And I got this one boy, and he won't stop calling
When they act this way, I know I got 'em
Too bad your ex don't do it for ya
Walked in and dream-came-trued it for ya
Soft skin and I perfumed it for ya (yes)
I know I Mountain Dew it for ya (yes)
That morning coffee, brewed it for ya (yes)
One touch and I brand-newed it for ya
Now he's thinkin' 'bout me every night, oh
Is it that sweet? I guess so
Say you can't sleep, baby, I know
That's that me espresso
Move it up, down, left, right, oh
Switch it up like Nintendo
Say you can't sleep, baby, I know
That's that me espresso
Is it that sweet? I guess so
I'm working late 'cause I'm a singer
Oh, he looks so cute wrapped around my finger
My twisted humor make him laugh so often
My honey bee, come and get this pollen
Too bad your ex don't do it for ya
Walked in and dream-came-trued it for ya
Soft skin and I perfumed it for ya (yes)
I know I Mountain Dew it for ya (yes)
That morning coffee, brewed it for ya (yes)
One touch and I brand-newed it for ya (stupid)
Now he's thinkin' 'bout me every night, oh
Is it that sweet? I guess so
Say you can't sleep, baby, I know
That's that me espresso
Move it up, down, left, right, oh
Switch it up like Nintendo
Say you can't sleep, baby, I know
That's that me espresso (yes)
Thinkin' 'bout me every night, oh
Is it that sweet? I guess so (yes)
Say you can't sleep, baby, I know
That's that me espresso (yes)
Move it up, down, left, right, oh
Switch it up like Nintendo (yes)
Say you can't sleep, baby, I know
That's that me espresso
Is it that sweet? I guess so
Mm, that's that me espresso`;
// Step 1: Count how many times the title appears in the lyrics
let title = "That's that me espresso";
let titleCount = (lyrics.match(new RegExp(title, "g")) || []).length;
console.log("The title appears", titleCount, "times.");
// Step 2: Find the index of the 50th word
let words = lyrics.split(/\s+/); // Split lyrics into words
let indexOf50thWord = (words.length >= 50) ? lyrics.indexOf(words[49]) : -1; // Indexing starts at 0
if (indexOf50thWord !== -1) {
console.log("The index of the 50th word is:", indexOf50thWord);
} else {
console.log("There are less than 50 words in the lyrics.");
}
// Step 3: Replace the first verse with the last verse
let verses = lyrics.split("\n\n"); // Split the lyrics into verses
if (verses.length > 1) {
verses[0] = verses[verses.length - 1]; // Replace the first verse with the last verse
lyrics = verses.join("\n\n"); // Join the verses back together
}
// Log the updated lyrics
console.log("\nUpdated Lyrics:\n", lyrics);
// Step 4: Concatenate two verses to make a custom song
let customSong = verses[0] + "\n\n" + verses[1]; // Concatenate the first and second verses
console.log("\nCustom Song:\n", customSong);
3.4.3 Popcorn Javascript:
// Step 1: Create the new creature
let flower = "Flower";
let cow = "Cow";
let newCreature = `${flower}Cow`; // Concatenating using template literals
console.log("The new creature is called:", newCreature);
// Step 2: Create a short story about the new creature
let creatureName = newCreature; // Using the newly created creature name
let characterName = "Bessie"; // Name of the cow
let location = "the magical meadow";
let food = "rainbow grass";
let story = `Once upon a time in ${location}, there lived a creature named ${creatureName}.
This unique creature had the body of a cow and the vibrant petals of a flower blooming all over its back.
Bessie, the cow, would often say, "I love frolicking in the ${food} and soaking up the sun!"
One day, while exploring the meadow, Bessie met a butterfly named Bella. Bella fluttered around, amazed by the sight.
"Bessie! You look so beautiful! How do you maintain such colorful petals?" asked Bella.
Bessie replied with a smile, "Oh, it’s all thanks to the magical ${food} I eat. It keeps my petals bright and cheerful!"
From that day on, Bessie and Bella became the best of friends, spreading joy and beauty wherever they went in the magical meadow.
Together, they made sure that every day was filled with laughter and fun!`;
console.log("\nShort Story:\n", story);
3.4.4
// JavaScript Text Analyzer
function textAnalyzer() {
// Get user input
let userInput = prompt("Enter your text:");
// Step 2: Display original string
console.log("\nOriginal String:");
console.log(userInput);
// Step 3: Count total characters (including spaces)
const totalCharacters = userInput.length;
console.log("\nTotal Characters (including spaces):", totalCharacters);
// Step 4: Find the longest word (includes symbols and numbers)
const words = userInput.split(/\s+/);
const longestWord = words.reduce((a, b) => a.length >= b.length ? a : b, "");
console.log("Longest Word:", longestWord, "with", longestWord.length, "characters");
// Step 5: Display the string reversed
const reversedString = userInput.split('').reverse().join('');
console.log("\nReversed String:");
console.log(reversedString);
// Step 6: Find the middle word/character (ignoring spaces)
const strippedString = userInput.replace(/\s/g, ''); // Remove spaces
const midIndex = Math.floor(strippedString.length / 2);
const middleCharacter = strippedString.length % 2 === 0
? strippedString[midIndex - 1] + strippedString[midIndex]
: strippedString[midIndex];
console.log("\nMiddle Character(s):", middleCharacter);
// Unique Function: Count occurrences of each character (case insensitive)
const charCount = {};
for (let char of userInput.toLowerCase()) {
if (char !== ' ') {
charCount[char] = (charCount[char] || 0) + 1;
}
}
console.log("\nCharacter Occurrences:");
console.log(charCount);
// Bonus: Replace words
const replaceWord = prompt("\nEnter the word you want to replace:");
if (userInput.includes(replaceWord)) {
const newWord = prompt("Enter the new word:");
const newString = userInput.replace(new RegExp(replaceWord, 'g'), newWord);
console.log("\nNew String After Replacement:");
console.log(newString);
} else {
console.log("The word is not in the original string.");
}
}
// Run the text analyzer
textAnalyzer();
# Python Text Analyzer
def text_analyzer():
# Step 1: Get user input
user_input = input("Enter your text: ")
# Step 2: Display original string
print("\nOriginal String:")
print(user_input)
# Step 3: Count total characters (including spaces)
total_characters = len(user_input)
print("\nTotal Characters (including spaces):", total_characters)
# Step 4: Find the longest word (includes symbols and numbers)
words = user_input.split()
longest_word = max(words, key=len)
print("Longest Word:", longest_word, "with", len(longest_word), "characters")
# Step 5: Display the string reversed
reversed_string = user_input[::-1]
print("\nReversed String:")
print(reversed_string)
# Step 6: Find the middle word/character (ignoring spaces)
stripped_string = user_input.replace(" ", "") # Remove spaces
mid_index = len(stripped_string) // 2
if len(stripped_string) % 2 == 0:
middle_character = stripped_string[mid_index - 1:mid_index + 1]
else:
middle_character = stripped_string[mid_index]
print("\nMiddle Character(s):", middle_character)
# Unique Function: Count occurrences of each character (case insensitive)
char_count = {}
for char in user_input.lower():
if char != ' ':
char_count[char] = char_count.get(char, 0) + 1
print("\nCharacter Occurrences:")
for char, count in char_count.items():
print(f"{char}: {count}")
# Bonus: Replace words
replace_word = input("\nEnter the word you want to replace: ")
if replace_word in user_input:
new_word = input("Enter the new word: ")
new_string = user_input.replace(replace_word, new_word)
print("\nNew String After Replacement:")
print(new_string)
else:
print("The word is not in the original string.")
# Run the text analyzer
text_analyzer()