3.4 Homework and Popcorn Hacks
3.4 Homework and Popcorn Hacks
Popcorn Hack 4
# Sample song lyrics
lyrics = """
Just a young gun with a quick fuse
I was uptight, wanna let loose
I was dreaming of bigger things
And wanna leave my old life behind
Not a yes-sir, not a follower
Fit the box, fit the mold
Have a seat in the foyer, take a number
I lightning before the thunder
Thunder, thunder
Thunder, thun-, thunder
Thun-thun-thunder, thunder, thunder
Thunder, thun-, thunder
Thun-thun-thunder, thunder
Thunder, feel the thunder
Lightning then the thunder
Thunder, feel the thunder
Lightning then the thunder
Thunder, thunder
Thunder
Kids were laughing in my classes
While I was scheming for the masses
Who do you think you are?
Dreaming 'bout being a big star
They say you're basic, they say you're easy
You're always riding in the back seat
Now I'm smiling from the stage while
You were clapping in the nosebleeds
Thunder
Thunder, thun-, thunder
Thun-thun-thunder, thunder, thunder
Thunder, thun-, thunder
Thun-thun-thunder, thunder
Thunder, feel the thunder
Lightning then the thunder
Thunder, feel the thunder
Lightning then the thunder
Thunderrrrrrrr
"""
# 1. Define the title of the song
songTitle = "Thunder"
# 2. Count how many times the title appears in the lyrics
titleCount = lyrics.lower().count(songTitle.lower())
print(f"The title '{songTitle}' appears {titleCount} times in the lyrics.")
# 3. Find the index of the 50th word
words = lyrics.split() # Split the lyrics into individual words
print(lyrics.find(words[49]))
# 4. Replace the first verse with the last verse
lyrics = lyrics.replace("Just a young gun with a quick fuse", "Thunderrrrrrrr")
# 5. Concatnate two verses together to make new song
verse1 = "Thunder is loud"
verse2 = "lightning is cool"
new_lyrics = verse1 + ', ' + verse2
print(new_lyrics)
The title 'Thunder' appears 38 times in the lyrics.
232
Thunder is loud, lightning is cool
Popcorn Hack 5
%%js
// Define the flower and animal
let flower = "Rose";
let animal = "Tiger";
let newCreature = flower + animal; // Using concatenation with quotes
let newCreatureTemplate = `${flower}${animal}`; // Using template literals
// Short story
let story = `
Once upon a time, there lived a unique creature called the ${newCreatureTemplate}.
The ${newCreatureTemplate} had the colors of a ${flower} and the strength of a ${animal}.
One day, a curious kid named Bob stumbled upon the ${newCreatureTemplate} while exploring the forest.
"Who are you?" Bob asked.
"I am the ${newCreatureTemplate}," the creature replied with a roar. "I protect this forest and all its inhabitants."
Bob couldn't believe his eyes. "Wow!"
`;
console.log(story);
<IPython.core.display.Javascript object>
Extra Popcorn Hack
%%js
const lamborghiniUrusProperties = {
engine: 'V8',
power: '650 hp',
topSpeed: '190 mph',
acceleration: '0-60 mph in 3.6 seconds',
price: '$218,009'
}
// updating the dictionary
lamborghiniUrusProperties.price = '$220,000';
<IPython.core.display.Javascript object>
Homework Hacks
Text Analyzer
# Ask user to input some text
user_input = input("Enter some text: ")
# Splits the inputted text into words
words = user_input.split()
print("Analyzing the following texttext: " + user_input)
# Count the total characters in the text
num_chars = len(user_input)
longest_word = "" # Variable to store the longest word
for word in words: # For each word in the list of words checkts the length of the word
if len(word) > len(longest_word): # If the length of the word is greater than the length of the longest word that has been previously stored
longest_word = word # Replace the longest word with the current word that is being checked
print("The longest word is: " + longest_word)
# Printing reversed text
print("The reversed text is: " + user_input[::-1])
# Printing the middle word
middle_word = words[len(words) // 2]
print("The middle word is: " + middle_word)
Analyzing the following texttext: Hello. My name is Anvay and I am a sophomore at Del Norte High School. I am currently enrolled in AP Computer Science Principles in 4th period.
The longest word is: Principles
The reversed text is: .doirep ht4 ni selpicnirP ecneicS retupmoC PA ni dellorne yltnerruc ma I .loohcS hgiH etroN leD ta eromohpos a ma I dna yavnA si eman yM .olleH
The middle word is: High