In: Computer Science
PYTHON CODE
Step one: Copy and paste these lyrics into a multistring line
"Do you ever feel like a plastic bag
Drifting through the wind
Wanting to start again?
Do you ever feel, feel so paper-thin
Like a house of cards, one blow from caving in?
Do you ever feel already buried deep
Six feet under screams but no one seems to hear a thing
Do you know that there's still a chance for you
'Cause there's a spark in you?
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
You don't have to feel like a wasted space
You're original, cannot be replaced
If you only knew what the future holds
After a hurricane comes a rainbow
Maybe a reason why all the doors are closed
So you could open one that leads you to the perfect road
Like a lightning bolt your heart will glow
And when it's time you'll know
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
It's always been inside of you, you, you
And now it's time to let it through, -ough, -ough
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
Boom, boom, boom
Even brighter than the moon, moon, moon"
Step two: Then display and record the following statistics...
Python code:
from collections import Counter
from string import punctuation
import re
str1 = '''Do you ever feel like a plastic bag
Drifting through the wind
Wanting to start again?
Do you ever feel, feel so paper-thin
Like a house of cards, one blow from caving in?
Do you ever feel already buried deep
Six feet under screams but no one seems to hear a thing
Do you know that there's still a chance for you
'Cause there's a spark in you?
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
You don't have to feel like a wasted space
You're original, cannot be replaced
If you only knew what the future holds
After a hurricane comes a rainbow
Maybe a reason why all the doors are closed
So you could open one that leads you to the perfect road
Like a lightning bolt your heart will glow
And when it's time you'll know
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
It's always been inside of you, you, you
And now it's time to let it through, -ough, -ough
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
Boom, boom, boom
Even brighter than the moon, moon, moon'''
# Display the lyrics
print(str1)
# Total words
total_words = (len(str1.replace('\\', '').split()))
print("\nThe total words are: ", total_words)
# Total characters
total_chars = len(str1)
print("\nThe total characters are: ", total_chars)
# Average word length
words = str1.split()
avg = sum(map(len, words))/len(words)
avg = round(avg,2)
print("\nThe average word length is: ", avg)
# Average sentence length (if every period/full stop is considered as sentence)
#sents = str1.split('.')
# Average sentence length (if every line break is considered as sentence)
sents = str1.split('\n')
avg_len = sum(len(x.split()) for x in sents) / len(sents)
avg_len = round(avg_len,2)
print("\nThe average sentence length is: ", avg_len)
# Word distribution of all words
s1 = re.sub(r'[^\w\s]','',str1)
word = []
word = s1.split()
wfreq =[word.count(w) for w in word]
wlist = (dict(zip(word,wfreq)))
print("\nThe word distribution of all words are: ", wlist)
# Word distribution of words ending in "ly"
table = str.maketrans(punctuation, ' ' * len(punctuation))
x = str1.translate(table).lower()
c = Counter(i for i in x.split() if i.endswith('ly'))
print("\nThe word distribution of words ending with 'ly' are: ",c)
# Top 10 longest words
s = re.sub(r'[^\w\s]','',str1)
w1 = []
w1 = s.split()
wfreq1 =[len(w) for w in w1]
wlist1 = (dict(zip(w1,wfreq1)))
sorted_d = sorted(wlist1.items(), key=lambda x: x[1], reverse=True)
print("\nThe top 10 longest words are: ", sorted_d[0:10])
Output:
Do you ever feel like a plastic bag
Drifting through the wind
Wanting to start again?
Do you ever feel, feel so paper-thin
Like a house of cards, one blow from caving in?
Do you ever feel already buried deep
Six feet under screams but no one seems to hear a thing
Do you know that there's still a chance for you
'Cause there's a spark in you?
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
You don't have to feel like a wasted space
You're original, cannot be replaced
If you only knew what the future holds
After a hurricane comes a rainbow
Maybe a reason why all the doors are closed
So you could open one that leads you to the perfect road
Like a lightning bolt your heart will glow
And when it's time you'll know
You just gotta ignite the light and let it shine
Just own the night like the 4th of July
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
It's always been inside of you, you, you
And now it's time to let it through, -ough, -ough
'Cause, baby, you're a firework
Come on, show 'em what you're worth
Make 'em go, "Ah, ah, ah"
As you shoot across the sky
Baby, you're a firework
Come on, let your colors burst
Make 'em go, "Ah, ah, ah"
You're gonna leave 'em all in awe, awe, awe
Boom, boom, boom
Even brighter than the moon, moon, moon
Boom, boom, boom
Even brighter than the moon, moon, moon
The total words are: 364
The total characters are: 1863
The average word length is: 4.09
The average sentence length is: 5.52
The word distribution of all words are: {'Do': 4, 'you': 15, 'ever': 3, 'feel': 5, 'like': 4, 'a': 16, 'plastic': 1, 'bag': 1, 'Drifting': 1, 'through': 2, 'the': 16, 'wind': 1, 'Wanting': 1, 'to': 5, 'start': 1, 'again': 1, 'so': 1, 'paperthin': 1, 'Like': 2, 'house': 1, 'of': 4, 'cards': 1, 'one': 3, 'blow': 1, 'from': 1, 'caving': 1, 'in': 5, 'already': 1, 'buried': 1, 'deep': 1, 'Six': 1, 'feet': 1, 'under': 1, 'screams': 1, 'but': 1, 'no': 1, 'seems': 1, 'hear': 1, 'thing': 1, 'know': 2, 'that': 2, 'theres': 2, 'still': 1, 'chance': 1, 'for': 1, 'Cause': 4, 'spark': 1, 'You': 3, 'just': 2, 'gotta': 2, 'ignite': 2, 'light': 2, 'and': 2, 'let': 6, 'it': 3, 'shine': 2, 'Just': 2, 'own': 2, 'night': 2, '4th': 2, 'July': 2, 'baby': 3, 'youre': 9, 'firework': 6, 'Come': 6, 'on': 6, 'show': 3, 'em': 12, 'what': 4, 'worth': 3, 'Make': 6, 'go': 6, 'Ah': 6, 'ah': 12, 'As': 3, 'shoot': 3, 'across': 3, 'sky': 3, 'Baby': 3, 'your': 4, 'colors': 3, 'burst': 3, 'Youre': 4, 'gonna': 3, 'leave': 3, 'all': 4, 'awe': 9, 'dont': 1, 'have': 1, 'wasted': 1, 'space': 1, 'original': 1, 'cannot': 1, 'be': 1, 'replaced': 1, 'If': 1, 'only': 1, 'knew': 1, 'future': 1, 'holds': 1, 'After': 1, 'hurricane': 1, 'comes': 1, 'rainbow': 1, 'Maybe': 1, 'reason': 1, 'why': 1, 'doors': 1, 'are': 1, 'closed': 1, 'So': 1, 'could': 1, 'open': 1, 'leads': 1, 'perfect': 1, 'road': 1, 'lightning': 1, 'bolt': 1, 'heart': 1, 'will': 1, 'glow': 1, 'And': 2, 'when': 1, 'its': 2, 'time': 2, 'youll': 1, 'Boom': 3, 'boom': 6, 'Even': 3, 'brighter': 3, 'than': 3, 'moon': 9, 'Its': 1, 'always': 1, 'been': 1, 'inside': 1, 'now': 1, 'ough': 2}
The word distribution of words ending with 'ly' are: Counter({'july': 2, 'only': 1})
The top 10 longest words are: [('paperthin', 9), ('hurricane', 9), ('lightning', 9), ('Drifting', 8), ('firework', 8), ('original', 8), ('replaced', 8), ('brighter', 8), ('plastic', 7), ('through', 7)]
How to run the code:
1) Copy paste the code in a file and save it as "test.py"
2) Then to run the code got to the command prompt and change the directory to where the file is saved and type "python test.py"
or
If using IDLE, press F5
3) Get the output.