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.4 Hacks
Python and Javascript Popcorn and Homework Hacks for 3.4
Python Popcorn and Homework Hacks
#Popcorn Hack 1
lyrics = "If you wanna run away with me, I know a galaxy And I can take you for a ride I had a premonition that we fell into a rhythm Where the music don't stop for life Glitter in the sky, glitter in my eyes Shining just the way I like If you're feeling like you need a little bit of company You met me at the perfect time \n You want me, I want you, baby My sugarboo, I'm levitating The Milky Way, we're renegading Yeah, yeah, yeah, yeah, yeah I got you, moonlight, you're my starlight I need you all night, come on, dance with me I'm levitating You, moonlight, you're my starlight (you're the moonlight) I need you all night, come on, dance with me I'm levitating \n I believe that you're for me, I feel it in our energy I see us written in the stars We can go wherever, so let's do it now or never, baby Nothing's ever, ever too far Glitter in the sky, glitter in our eyes Shining just the way we are I feel like we're forever, every time we get together But whatever, let's get lost on Mars \n You want me, I want you, baby My sugarboo, I'm levitating The Milky Way, we're renegading Yeah, yeah, yeah, yeah, yeah I got you, moonlight, you're my starlight I need you all night, come on, dance with me I'm levitating You, moonlight, you're my starlight (you're the moonlight) I need you all night, come on, dance with me I'm levitating (whoo) \n You can fly away with me tonight You can fly away with me tonight Baby, let me take you for a ride Yeah, yeah, yeah, yeah, yeah I'm levitating (whoo) You can fly away with me tonight You can fly away with me tonight Baby, let me take you for a ride Yeah, yeah, yeah, yeah, yeah (whoo) \n My love is like a rocket, watch it blast off And I'm feeling so electric, dance my __ off And even if I wanted to, I can't stop Yeah, yeah, yeah, yeah, yeah \n My love is like a rocket, watch it blast off And I'm feeling so electric, dance my __ off And even if I wanted to, I can't stop Yeah, yeah, yeah, yeah, yeah \n You want me, I want you, baby My sugarboo, I'm levitating The Milky Way, we're renegading I got you (yeah), moonlight, you're my starlight I need you all night (all night), come on, dance with me I'm levitating (whoo) \n You can fly away with me tonight (tonight) You can fly away with me tonight Baby, let me take you for a ride Yeah, yeah, yeah, yeah, yeah (take you for a ride) I'm levitating (whoo) You can fly away with me tonight (tonight) You can fly away with me tonight Baby, let me take you for a ride Yeah, yeah, yeah, yeah, yeah (let me take you for a ride) \n I got you, moonlight, you're my starlight (you are my starlight) I need you all night, come on, dance with me (come on, dance with me, baby) I'm levitating You, moonlight, you're my starlight (you're the moonlight) I need you all night, come on, dance with me I'm levitating"
song_title = "Levitating"
title_count = lyrics.lower().count(song_title.lower())
print(f"The title '{song_title}' appears {title_count} times.")
words = lyrics.split()
if len(words) >= 50:
word_50th = words[49]
print(f"The 50th word is '{word_50th}'.")
else:
print("The lyrics have fewer than 50 words.")
verses = lyrics.split("\n")
verses[0], verses[-1] = verses[-1], verses[0]
new_lyrics = "\n\n".join(verses)
print("First and last verse have been swapped.")
new_song_part = verses[0] + "\n\n" + verses[1]
print("New song part created by concatenating two verses.")
print(new_song_part)
The title 'Levitating' appears 12 times.
The 50th word is 'like'.
First and last verse have been swapped.
New song part created by concatenating two verses.
I got you, moonlight, you're my starlight (you are my starlight) I need you all night, come on, dance with me (come on, dance with me, baby) I'm levitating You, moonlight, you're my starlight (you're the moonlight) I need you all night, come on, dance with me I'm levitating
You want me, I want you, baby My sugarboo, I'm levitating The Milky Way, we're renegading Yeah, yeah, yeah, yeah, yeah I got you, moonlight, you're my starlight I need you all night, come on, dance with me I'm levitating You, moonlight, you're my starlight (you're the moonlight) I need you all night, come on, dance with me I'm levitating
Javascript Popcorn and Homework Hacks
%%javascript
/* Popcorn Hack 1 */
const flower = "Lily";
const animal = "Dragon";
const newCreature = `${flower}${animal}`;
console.log("The new creature is called a '" + newCreature + "'.");
const friend1 = "Vibha";
const friend2 = "Ava";
const creature = newCreature;
const forest = "CSP Forest";
const story = `
In the middle of the desolate ${forest}, ${friend1} and ${friend2} stumbled upon a strange computer. Naturally, they pressed a button on the computer in the middle of the abandoned forest!
"Did you hear that?" ${friend2} whispered.
"It looks like we summoned a ${creature}!" ${friend1} gasped, holding the computer. "I've read about them in the ancient Github archives, but I never thought I'd see one."
Suddenly, the ${creature} flew into view. Its shiny dragon-like scales gleamed in the light, and its mane bloomed like a field of lillies. Its eyes, sharp yet oddly peaceful, fixed on the two adventurers.
The ${creature} roared, and the two friends ran out of the ${forest}, never to return.
`;
console.log(story);
<IPython.core.display.Javascript object>
%%javascript
/* Homework Hack 1 */
function analyzeText() {
let inputText = document.getElementById('inputText').value.trim();
console.log(`Original String:\n${inputText}`);
let totalCharacters = inputText.length;
console.log(`Total Characters (including spaces): ${totalCharacters}`);
let words = inputText.match(/\b\w+\b/g);
if (!words) {
words = [];
}
let longestWord = "";
words.forEach(word => {
if (word.length > longestWord.length) {
longestWord = word;
}
});
console.log(`Longest Word: '${longestWord}' (Length: ${longestWord.length})`);
let reversedString = inputText.split('').reverse().join('');
console.log(`Reversed String:\n${reversedString}`);
let middleIndex = Math.floor(words.length / 2);
let middleWord = words[middleIndex];
console.log(`Middle Word: '${middleWord}'`);
document.getElementById('output').innerHTML = `
<p><strong>Original String:</strong><br>${inputText}</p>
<p><strong>Total Characters (including spaces):</strong> ${totalCharacters}</p>
<p><strong>Longest Word:</strong> '${longestWord}' (Length: ${longestWord.length})</p>
<p><strong>Reversed String:</strong><br>${reversedString}</p>
<p><strong>Middle Word:</strong> '${middleWord}'</p>
`;
}
<IPython.core.display.Javascript object>