Assignment 1#

Topics

  • Numbers and operations

  • Strings

  • Lists

  • Dictionaries

  • Loops

  • Functions

For each of the problems below use either a code or markdown cell to answer.

Number and Operation#

In the problems below, compute the appropriate numerical operations and assign your solution to the given variable.

Problem 1: Sum the first 10 integers. Assign your solution to ans1 below.

### addition
ans1 = ''
print(ans1)

Problem 2: Multiply 1, 2, 3, 4, and 5. Assign your solution to ans2 below.

### multiplicatin
ans2 = ''
print(ans2)

Problem 3: Divide 9 by 3. Assign your solution to ans3 below.

### division
ans3 = ''
print(ans3)

Problem 4: What is the remainder when 7 is divided by 3? Use the % operator to determine this. Assign your solution to ans4 below.

### modulus
ans4 = ''
print(ans4)

Problem 5: What is the integer of the quotient when 12 is divided by 5? Use the // operator to determine this. Assign your solution to ans5 below.

### floor
ans5 = ''
print(ans5)

Strings and their methods#

Problem 6: Create a string named firstname below with your first name.

### define a string
firstname = ''
print(firstname)

Problem 7: Use the .upper() method to uppercase the string firstname. Assign your solution to ans7 below.

### upper
ans7 = ''

Problem 8: Below, the lyrics from the song Black Site by the American hardcore-punk band Ammo discography. Use the split method to split the lyrics and assign them to a variable tokens.

### split
lyrics = '''
Enhanced interrogation
Secret strangulation
Thirst for information
Human ruination
'''
tokens = ''

Problem 9: Use the list tokens to extract every other word from the lyrics and combine these to be a new string using the .join() function. Assign your answer as new_lyrics below.

### slice
new_lyrics = ''
print(new_lyrics)

Lists#

The questions below focus on the use of list objects. Also, they will introduce you to Ann Peebles song Trouble, Heartaches, and Sadness from the album, whose lyrics are assigned to trouble_heartaches_and_sadness below.

### create
trouble_heartaches_and_sadness = '''
Whoa
Old man trouble
Stop knockin' at my door
You used to be a good friend of mine
But you can't hang around me no more

Heartache
(heartache)
Stop knockin' at my window
I don't wanna hear what you have to say
You can go join Mister Trouble
And be on your merry way


'Cause I've found the love
I needed all the time
I've found the love
Help me ease my troubled mind

Dark clouds
Hanging over my head
Aw, but this time
Sure won't rain on me
He's my sunshine
And the love he gives
Gonna set me free
Yeah

Mmm
Sadness
I have no more use for you
I know you can easily
Find you another friend
Cos I've found someone
To take away my trouble
Bring my sadness to an end

I've found the love
I needed all the time
I've found the love
Help me ease my troubled mind
'''

Problem 10: Create a list of tokens from the lyrics and assign to the variable peebles_lyrics below. Extract the last word of the lyrics and assign to last_word.

### index
peebles_lyrics = []
last_word = ''
print(peebles_lyrics[:10])
print(last_word)
[]

Problem 11: Use the list peebles_lyrics to determine the number of characters in the lyrics. Assign your results to num_words below.

### length

Problem 12: There is a kind of collection that we didn’t discuss in class – that of a set. Read the documentation here, and create a set of words from peebles_lyrics. Assign the length of this set to num_unique_words below.

### set

Problem 13: One way to understand the notion of lexical diversity is as the percent of unique words in a text. What is the lexical diversity of Trouble, Heartaches, and Sadness? Assign your answer as lexical_diversity below.

### lexical diversity
lexical_diversity = ''

Problem 14: Perhaps you want to compare a different song and its lexical diversity. For this, you are to create a function lex_div that takes in a string, and return the lexical diversity of that string. Use the pseudocode to assist you in the function body.

### function
def lex_div(lyrics_string):
    '''
    This function takes in a string
    and returns the percent of unique words in 
    the string.
    '''
    ### create a list of lyrics
    
    ### create a set of lyrics
    
    ### return the quotient of appropriate lengths
    pass

Problem 15: Use your function to compare the lexical diversity of Troubles, Heartaches, and Sadness to that of the Drake single Hotline Bling, assigned to the variable hotline_bling below. Use the lex_div function to determine the lexical diversity and decide if Drake or Anne Peebles song had a higher lexical diversity.

hotline_bling = '''
You used to call me on my
You used to, you used to
Yeah

You used to call me on my cell phone
Late-night when you need my love
Call me on my cell phone
Late-night when you need my love
And I know when that hotline bling
That can only mean one thing
I know when that hotline bling
That can only mean one thing


Ever since I left the city, you
Got a reputation for yourself now
Everybody knows and I feel left out
Girl, you got me down, you got me stressed out
'Cause ever since I left the city, you
Started wearing less and goin' out more
Glasses of champagne out on the dance floor
Hangin' with some girls I never seen before


You used to call me on my cell phone
Late-night when you need my love
Call me on my cell phone
Late-night when you need my love
I know when that hotline bling
That can only mean one thing
I know when that hotline bling
That can only mean one thing


Ever since I left the city, you, you, you
You and me, we just don't get along
You make me feel like I did you wrong
Going places where you don't belong
Ever since I left the city, you
You got exactly what you asked for
Running out of pages in your passport
Hangin' with some girls I've never seen before
'''

Dictionaries#

Problem 17: Create a dictionary with keys heartache and hotline with values as the string representation of the lyrics from the respective songs above. Assign your solution to lyrics_dict below.

### create lyrics dictionary
lyrics_dict = {}
print(lyrics_dict.keys())
dict_keys([])

Problem 18: Add a third entry to the dictionary using the Band of Horses song The Funeral assigned to the string the_funeral below. Use the key funeral to add these to lyrics_dict.

the_funeral = '''
I'm coming up only to hold you under
And coming up only to show you're wrong
And to know you is hard, we wonder
To know you all wrong, we were

Ooh, ooh
Ooh, ooh

Really too late to call, so we wait for
Morning to wake you is all we got
But to know me as hardly golden
Is to know me all wrong, they were

At every occasion, I'll be ready for the funeral
At every occasion once more, it's called the funeral
At every occasion, oh, I'm ready for the funeral
At every occasion of one billion day funeral

I'm coming up only to you show you down for
And coming up only to you show you're wrong
To the outside, the dead leaves lay on the lawn
'Fore they died, and had trees to hang there upon

Ooh, ooh
Ooh, ooh

At every occasion, I'll be ready for the funeral
At every occasion once more, it's called the funeral
At every occasion, oh, I'm ready for the funeral
At every occasion of one billion day funeral
'''

Problem 19: Loop over the lyrics_dict and print the lexical diversity of each of the three songs.

### looping over dictionary

Problem 20: Write a function lexical_div_from_dict that takes in a dictionary with keys that represent the song name and values that are the lyrics of the song – like lyrics_dict – and returns a dictionary of song names and their respective lexical diversity.

### lexical function for dictionaries
def lexical_div_from_dict(lyric_dict):
    '''
    The function accepts a dictionary of lyrics 
    --
    (keys song name and values string of lyrics)
    --
    and returns a dictionary with the songs lexical diversity.
    '''
    pass

Loops#

Problem 21: Below, two lists are given to represent albums and reviews from the magazine Pitchfork. Loop over each of these and print:

The album {album_name} by {artist} received a score of {review_score}

Be sure to use an f-string for this.

Hint: You should use the zip function for this. Checkout the documentation here.

albums = ['God Did', 'Black Vladimir', 'Pulse of the Early Brain', '11:11']
artists = ['DJ Khaled', 'Meyhem Lauren/Daringer', 'Stereolab', 'Regina Spektor']
ratings = [4.0, 7.1, 6.6, 7.7]
### loop over lists and print

Functions#

The problems below review functions.

Problem 22: Write a function that takes in a number n and returns the sum of the numbers 1 through n.

Problem 23: Write a function that takes a number (a person’s height) and returns “Tall enough” if they are at least 5 feet (60 inches) or “Too short” if they are below 5 feet.

  • Example: is_tall(72) => "Tall enough"

  • Example: is_tall(52) => "Too short"

Problem 24: Write a function that takes in a numeric grade (0 to 100) and returns the letter grade (no pluses or minuses). In a traditional rubric, that would be as follows:

  • 90 or above = A

  • 80 - 89 = B

  • 70 - 79 = C

  • 60 - 69 = D

  • 59 or below = F

Example: letter_grade(85) => "B"

Problem 25: Write a function that takes in a string and returns that string with its vowels removed.

Example: disemvowel("Python is my favorite language") => "Pythn s my fvrt lngg"

Further Problems#

One great resource for practice are sites like code wars. They have problems organized by difficulty in Kata’s. Head over to code wars and get set up to practice. Explore some different problems and locate two that you understand and are able to solve. State the problems and their solutions below.

Problem 26: Code Wars Problem I

Problem 27: Code Wars Problem II