Assignment 2: Containers

Due: Tuesday the 13th of November 2018 20:00 p.m.

Please name your ipython notebook with the following naming convention: ASSIGNMENT_2_FIRSTNAME_LASTNAME.ipynb

Please submit your assignment using this google form

If you have questions about this topic, please post them in the Canvas forum.

Exercise 1: Beersong

99 Bottles of Beer is a traditional song in the United States and Canada. Write a python program that generates the lyrics to the song.

The song's simple lyrics are as follows:

99 bottles of beer on the wall,
99 bottles of beer.
Take one down, pass it around,
98 bottles of beer on the wall.

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero. After the last bottle is taken down and passed around, there is a special verse:

No more bottles of beer on the wall,
no more bottles of beer.
Go to the store and buy some more,
99 bottles of beer on the wall.

Notes:

  • Leave a blank line between verses.
  • Make sure that you print the singular form of "bottles" when the counter is at one.

Hint:

  • While debugging the program, start from a small number, and change it to 99 when you are done (as shown below).
  • You can substract from a number with number = number - 1
  • Use variables to prevent code repetition

You can use the following code snippet as a start:


In [ ]:
for number in reversed(range(1, 5)): # change 5 to 99 when you're done with debugging
    print(number, 'bottles of beer on the wall,')

Exercise 2: list methods

In this exercise, we will focus on the following list methods:

a.) append

b.) count

c.) index

d.) insert

e.) pop

For each of the aforementioned list methods:

  • explain the positional arugments
  • explain the keyword arguments
  • explain what the goal of the method is and what it returns
  • give a working example. Provide also an example with a keyword argument (assuming the method has one or more keyword arguments).

Exercise 3: set methods

In this exercise, we will focus on the following set methods:

  • update
  • pop
  • remove
  • clear

For each of the aforementioned set methods:

  • explain the positional arugments
  • explain the keyword arguments
  • explain what the goal of the method is and what it returns
  • give a working example.

Please fill in your answers here:

Exercise 4: Analyzing vocabulary using sets

Please consider the following two texts:

These stories were copied from here.


In [ ]:
a_story = """In a far away kingdom, there was a river. This river was home to many golden swans. The swans spent most of their time on the banks of the river. Every six months, the swans would leave a golden feather as a fee for using the lake. The soldiers of the kingdom would collect the feathers and deposit them in the royal treasury. 
One day, a homeless bird saw the river. "The water in this river seems so cool and soothing. I will make my home here," thought the bird. 
As soon as the bird settled down near the river, the golden swans noticed her. They came shouting. "This river belongs to us. We pay a golden feather to the King to use this river. You can not live here." 
"I am homeless, brothers. I too will pay the rent. Please give me shelter," the bird pleaded. "How will you pay the rent? You do not have golden feathers," said the swans laughing. They further added, "Stop dreaming and leave once." The humble bird pleaded many times. But the arrogant swans drove the bird away. 
"I will teach them a lesson!" decided the humiliated bird. 
She went to the King and said, "O King! The swans in your river are impolite and unkind. I begged for shelter but they said that they had purchased the river with golden feathers." 
The King was angry with the arrogant swans for having insulted the homeless bird. He ordered his soldiers to bring the arrogant swans to his court. In no time, all the golden swans were brought to the King’s court. 
"Do you think the royal treasury depends upon your golden feathers? You can not decide who lives by the river. Leave the river at once or you all will be beheaded!" shouted the King. 
The swans shivered with fear on hearing the King. They flew away never to return. The bird built her home near the river and lived there happily forever. The bird gave shelter to all other birds in the river. """
print(a_story)

In [ ]:
another_story = """Long time ago, there lived a King. He was lazy and liked all the comforts of life. He never carried out his duties as a King. “Our King does not take care of our needs. He also ignores the affairs of his kingdom." The people complained. 
One day, the King went into the forest to hunt. After having wandered for quite sometime, he became thirsty. To his relief, he spotted a lake. As he was drinking water, he suddenly saw a golden swan come out of the lake and perch on a stone. “Oh! A golden swan. I must capture it," thought the King. 
But as soon as he held his bow up, the swan disappeared. And the King heard a voice, “I am the Golden Swan. If you want to capture me, you must come to heaven." 
Surprised, the King said, “Please show me the way to heaven." “Do good deeds, serve your people and the messenger from heaven would come to fetch you to heaven," replied the voice. 
The selfish King, eager to capture the Swan, tried doing some good deeds in his Kingdom. “Now, I suppose a messenger will come to take me to heaven," he thought. But, no messenger came. 
The King then disguised himself and went out into the street. There he tried helping an old man. But the old man became angry and said, “You need not try to help. I am in this miserable state because of out selfish King. He has done nothing for his people." 
Suddenly, the King heard the golden swan’s voice, “Do good deeds and you will come to heaven." It dawned on the King that by doing selfish acts, he will not go to heaven. 
He realized that his people needed him and carrying out his duties was the only way to heaven. After that day he became a responsible King. 
"""

Exercise 4a: preprocessing text

Before analyzing the two texts, we are first going to preprocess them. Please use a particular string method multiple times to replace the following characters by empty strings in both a_story and another_story:

  • newlines: '\n'
  • commas: ','
  • dots: '.'
  • quotes: '"'

After preprocessing both texts, please call the cleaned stories cleaned_story and cleaned_another_story

Exercise 4b: from text to a list

For each text (cleaned_story and cleaned_another_story), please use a string method to convert cleaned_story and cleaned_another_story into lists by splitting using spaces.

Exercise 4c: from a list to a vocabulary (a set)

Please create a set for the words in each text by adding each word to a set. At the end, you should have two variables vocab_a_story and vocab_another_story, each containing the unique words in each story.


In [ ]:
vocab_a_story = set()
for word in cleaned_story:
    # insert your code here

do the same for the other text


In [ ]:
# you code

Exercise 4d: analyzing vocabularies

Please analyze the vocabularies by using set methods to determine:

  • which words occur in both texts
  • which words only occur in a_story
  • which words only occur in another_story

In [ ]:
# your code

Exercise 5: counting

Below you find a list called words, which is a list of strings.

a.) Please create a dictionary in which the key is the word and the value is the frequency of the word. However, do not include words that:

  • end with the letter e
  • start with the letter t
  • start with the letter c and end with the letter w (both conditions must be met)
  • have five or more letters

You are not allowed to use the collections module to do this.


In [ ]:
words = ['there',
 'was',
 'a',
 'village',
 'near',
 'a',
 'jungle',
 'the',
 'village',
 'cows',
 'used',
 'to',
 'go',
 'up',
 'to',
 'the',
 'jungle',
 'in',
 'search',
 'of',
 'food.',
 'in',
 'the',
 'forest',
 'there',
 'lived',
 'a',
 'wicked',
 'lion',
 'he',
 'used',
 'to',
 'kill',
 'a',
 'cow',
 'now',
 'and',
 'then',
 'and',
 'eat',
 'her',
 'this',
 'was',
 'happening',
 'for',
 'quite',
 'sometime',
 'the',
 'cows',
 'were',
 'frightened',
 'one',
 'day',
 'all',
 'the',
 'cows',
 'held',
 'a',
 'meeting',
 'an',
 'old',
 'cow',
 'said',
 'listen',
 'everybody',
 'the',
 'lion',
 'eats',
 'one',
 'of',
 'us',
 'only',
 'because',
 'we',
 'go',
 'into',
 'the',
 'jungle',
 'separately',
 'from',
 'now',
 'on',
 'we',
 'will',
 'all',
 'be',
 'together',
 'from',
 'then',
 'on',
 'all',
 'the',
 'cows',
 'went',
 'into',
 'the',
 'jungle',
 'in',
 'a',
 'herd',
 'when',
 'they',
 'heard',
 'or',
 'saw',
 'the',
 'lion',
 'all',
 'of',
 'them',
 'unitedly',
 'moo',
 'and',
 'chased',
 'him',
 'away',
 'moral',
 'divided',
 'we',
 'fall',
 'united',
 'we',
 'stand']

b.) Analyze your dicitionary by printing:

  • how many keys it has
  • what the highest word frequency is
  • the sum of all values.

In [ ]:

In addition, print the frequencies of the following words using your dictionary (if the word does not occur in the dictionary, print 'WORD does not occur')

  • up
  • near
  • together
  • lion
  • cow

In [ ]:
for word in ['up', 'near' , 'lion']:
    # print frequency

Exercise 6: Questions about Python

Please answer the following questions about the Python language. You can give an examples if they help you.

a. What is the difference between positional arguments and keyword arguments?


In [ ]:

b. What is/are the type/types of containers that you define using curly brackets?


In [ ]:

c. When comparing the containers lists and sets, there are three properties that are unique for lists. Please name and explain them.


In [ ]:

d. Explain the difference between continue and break? You can use an example for this.


In [ ]: