{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Assignment 2: Containers\n",
    "\n",
    "- Please name your files:\n",
    "    * ASSIGNMENT_2_FIRSTNAME_LASTNAME.ipynb \n",
    "    * assignment2_utils.py\n",
    "- Please store the two files in a folder called ASSIGNMENT_2_FIRSTNAME_LASTNAME\n",
    "- Please zip your folder and please follow the following naming convention for the zip file: ASSIGNMENT_2_FIRSTNAME_LASTNAME.zip\n",
    "- Please submit your assignment on Canvas: Assignment 2\n",
    "- If you have **questions** about this topic, please contact us **(cltl.python.course@gmail.com)**. Questions and answers will be collected on Piazza, so please check if your question has already been answered first.\n",
    "\n",
    "In this block, we covered the following chapters:\n",
    "- Chapter 05 - Core concepts of containers\n",
    "- Chapter 06 - Lists\n",
    "- Chapter 07 - Sets\n",
    "- Chapter 08 - Comparison of lists and sets\n",
    "- Chapter 09 - Looping over containers.\n",
    "- Chapter 10 - Dictionaries\n",
    "- Chapter 11 - Functions and scope\n",
    "\n",
    "In this assignment, you will be asked to show what you have learned from the topics above! \n",
    "\n",
    "**Finding solutions online**\n",
    "\n",
    "Very often, you can find good solutions online. We encourage you to use online resources when you get stuck. However, please always try to understand the code you find and indicate that it is not your own. Use the following format to mark code written by someone else:\n",
    "\n",
    "###Taken from [link] [date]\n",
    "\n",
    "[code]\n",
    "\n",
    "\\###\n",
    "\n",
    "Please use a similar format to indicate that you have worked with a classmate (e.g. mention the name instead of the link). \n",
    "\n",
    "*Please stick to this strategy for all course assignments.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1: Beersong\n",
    "*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."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The song's simple lyrics are as follows:\n",
    "\n",
    "    99 bottles of beer on the wall,\n",
    "    99 bottles of beer.\n",
    "    Take one down, pass it around,\n",
    "    98 bottles of beer on the wall.\n",
    "\n",
    "The same verse is repeated, each time with one fewer bottle. The song\n",
    "is completed when the singer or singers reach zero. After the last bottle\n",
    "is taken down and passed around, there is a special verse:\n",
    "\n",
    "    No more bottles of beer on the wall,\n",
    "    no more bottles of beer.\n",
    "    Go to the store and buy some more,\n",
    "    99 bottles of beer on the wall."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notes:\n",
    "\n",
    "* Leave a blank line between verses.\n",
    "* Make sure that you print the singular form of \"bottles\" when the counter is at one."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint:\n",
    "\n",
    "* While debugging the program, start from a small number, and\n",
    "change it to 100 when you are done (as shown below).\n",
    "* Use variables to prevent code repetition\n",
    "\n",
    "You can use the following code snippet as a start:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for number in range(4, 0, -1): # change 4 to 99 when you're done with debugging\n",
    "    print(number, 'bottles of beer on the wall,')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2: list methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this exercise, we will focus on the following **list methods**:\n",
    "\n",
    "a) append <br>\n",
    "b) count <br>\n",
    "c) index <br>\n",
    "d) insert <br>\n",
    "e) pop <br>\n",
    "\n",
    "For each of the aforementioned list methods:\n",
    "\n",
    "* explain the positional parameters\n",
    "* explain the keyword parameters\n",
    "* you can exclude *self* from your explanation\n",
    "* explain what the goal of the method is and what data type it returns, e.g., string, list, set, etc.\n",
    "* give a working example. Provide also an example by providing a value for keyword parameters (assuming the method has one or more keyword parameters)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Please fill in your answers here: \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3: set methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this exercise, we will focus on the following **set methods**:\n",
    "\n",
    "a) update <br>\n",
    "b) pop <br>\n",
    "c) remove <br>\n",
    "d) clear <br>\n",
    "\n",
    "For each of the aforementioned set methods:\n",
    "\n",
    "* explain the positional parameters\n",
    "* explain the keyword parameters\n",
    "* you can exclude *self* from your explanation\n",
    "* explain what the goal of the method is and what data type it returns, e.g., string, list, set, etc.\n",
    "* give a working example. Provide also an example by providing a value for keyword parameters (assuming the method has one or more keyword parameters)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Please fill in your answers here: \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4: Analyzing vocabulary using sets\n",
    "Please consider the following two texts:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These stories were copied from [here](http://www.english-for-students.com/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "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. \n",
    "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. \n",
    "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.\" \n",
    "\"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. \n",
    "\"I will teach them a lesson!\" decided the humiliated bird. \n",
    "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.\" \n",
    "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. \n",
    "\"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. \n",
    "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. \"\"\"\n",
    "print(a_story)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "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. \n",
    "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. \n",
    "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.\" \n",
    "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. \n",
    "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. \n",
    "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.\" \n",
    "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. \n",
    "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. \n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4a: preprocessing text\n",
    "Before analyzing the two texts, we are first going to preprocess them. \n",
    "Please use a particular string method multiple times to replace the following characters by empty strings in both **a_story** and **another_story**:\n",
    "* newlines: '\\n'\n",
    "* commas: ','\n",
    "* dots: '.'\n",
    "* quotes: '\"'\n",
    "\n",
    "\n",
    "Please assign the processed texts to the variables **cleaned_story** and **cleaned_another_story**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "#### Exercise 4b: from text to a list\n",
    "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. Please call the lists **list_cleaned_story** and **list_cleaned_another_story**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#you code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 4c: from a list to a vocabulary (a set)\n",
    "Please create a set for the words in each text by adding each word to a set. In the end, you should have two variables **vocab_a_story** and **vocab_another_story**, each containing the unique words in each story. Please use the output of Exercise 4b as the input for this exercise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vocab_a_story = set()\n",
    "for word in list_cleaned_story:\n",
    "    # insert your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "do the same for the other text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercise 4d: analyzing vocabularies\n",
    "Please analyze the vocabularies by using set methods to determine:\n",
    "* which words occur in both texts\n",
    "* which words only occur in **a_story**\n",
    "* which words only occur in **another_story**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5: counting\n",
    "Below you find a list called **words**, which is a list of strings. \n",
    "\n",
    "a) Please create a dictionary in which the **key** is the word, and the **value** is the frequency of the word. \n",
    "\n",
    "\n",
    "Exclude all words which meet at least one of the following requirements:\n",
    "\n",
    " * end with the letter e\n",
    " * start with the letter t\n",
    " * start with the letter c and end with the letter w (both conditions must be met)\n",
    " * have six or more letters\n",
    "\n",
    "You are not allowed to use the **collections** module to do this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "words = ['there',\n",
    " 'was',\n",
    " 'a',\n",
    " 'village',\n",
    " 'near',\n",
    " 'a',\n",
    " 'jungle',\n",
    " 'the',\n",
    " 'village',\n",
    " 'cows',\n",
    " 'used',\n",
    " 'to',\n",
    " 'go',\n",
    " 'up',\n",
    " 'to',\n",
    " 'the',\n",
    " 'jungle',\n",
    " 'in',\n",
    " 'search',\n",
    " 'of',\n",
    " 'food.',\n",
    " 'in',\n",
    " 'the',\n",
    " 'forest',\n",
    " 'there',\n",
    " 'lived',\n",
    " 'a',\n",
    " 'wicked',\n",
    " 'lion',\n",
    " 'he',\n",
    " 'used',\n",
    " 'to',\n",
    " 'kill',\n",
    " 'a',\n",
    " 'cow',\n",
    " 'now',\n",
    " 'and',\n",
    " 'then',\n",
    " 'and',\n",
    " 'eat',\n",
    " 'her',\n",
    " 'this',\n",
    " 'was',\n",
    " 'happening',\n",
    " 'for',\n",
    " 'quite',\n",
    " 'sometime',\n",
    " 'the',\n",
    " 'cows',\n",
    " 'were',\n",
    " 'frightened',\n",
    " 'one',\n",
    " 'day',\n",
    " 'all',\n",
    " 'the',\n",
    " 'cows',\n",
    " 'held',\n",
    " 'a',\n",
    " 'meeting',\n",
    " 'an',\n",
    " 'old',\n",
    " 'cow',\n",
    " 'said',\n",
    " 'listen',\n",
    " 'everybody',\n",
    " 'the',\n",
    " 'lion',\n",
    " 'eats',\n",
    " 'one',\n",
    " 'of',\n",
    " 'us',\n",
    " 'only',\n",
    " 'because',\n",
    " 'we',\n",
    " 'go',\n",
    " 'into',\n",
    " 'the',\n",
    " 'jungle',\n",
    " 'separately',\n",
    " 'from',\n",
    " 'now',\n",
    " 'on',\n",
    " 'we',\n",
    " 'will',\n",
    " 'all',\n",
    " 'be',\n",
    " 'together',\n",
    " 'from',\n",
    " 'then',\n",
    " 'on',\n",
    " 'all',\n",
    " 'the',\n",
    " 'cows',\n",
    " 'went',\n",
    " 'into',\n",
    " 'the',\n",
    " 'jungle',\n",
    " 'in',\n",
    " 'a',\n",
    " 'herd',\n",
    " 'when',\n",
    " 'they',\n",
    " 'heard',\n",
    " 'or',\n",
    " 'saw',\n",
    " 'the',\n",
    " 'lion',\n",
    " 'all',\n",
    " 'of',\n",
    " 'them',\n",
    " 'unitedly',\n",
    " 'moo',\n",
    " 'and',\n",
    " 'chased',\n",
    " 'him',\n",
    " 'away',\n",
    " 'moral',\n",
    " 'divided',\n",
    " 'we',\n",
    " 'fall',\n",
    " 'united',\n",
    " 'we',\n",
    " 'stand']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "b) Analyze your dicitionary by printing:\n",
    "* how many keys it has \n",
    "* what the highest word frequency is\n",
    "* the sum of all frequencies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "c) 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')\n",
    "* up\n",
    "* near\n",
    "* together\n",
    "* lion\n",
    "* cow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for word in ['up', 'near' , 'together', 'lion', 'cow']:\n",
    "    # print frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6: Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 6a: the beersong\n",
    "Please write a function that prints the beersong when it is called.\n",
    "The function:\n",
    "* is called `print_beersong`\n",
    "* has one positional parameter `start_number` (this is 99 in the original song) \n",
    "* prints the beer song"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_beersong(start_number):\n",
    "    \"\"\"\n",
    "    \"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 6b: the whatever can be in a bottle song\n",
    "There are other liquids than beer than can be placed in a bottle (e.g., *99 bottles of water on the wall.*).\n",
    "Please write a function that prints a variation of the beersong when it is called. All occurrences of **beer** will be replaced by what the user provides as an argument, e.g., *water*.\n",
    "\n",
    "The function:\n",
    "* is called `print_liquids`\n",
    "* has one positional parameter: `start_number` (this is **99** in the original song) \n",
    "* has one keyword parameter: `liquid` (set the default value to **beer**)\n",
    "* prints a liquids song"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# your code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 6c: preprocessing text\n",
    "Please write the answer to Exercise 4a as a function. The function replaces the following characters by empty strings in a text:\n",
    "* newlines: '\\n'\n",
    "* commas: ','\n",
    "* dots: '.'\n",
    "* quotes: '\"'\n",
    "\n",
    "The function is:\n",
    "* is called `clean_text`\n",
    "* has one positional parameter `text`\n",
    "* return a string, e.g., the cleaned text\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_text(text):\n",
    "    \"\"\"\n",
    "    \"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 6d: preprocessing text in a more general way\n",
    "Please write a function that replaces all characters that the user provides by empty strings.\n",
    "\n",
    "The function is:\n",
    "* is called `clean_text_general`\n",
    "* has one positional parameter `text`\n",
    "* has one keyword parameter `chars_to_remove`, which is a set (set the default to {'\\n', ',', '.', '\"'})\n",
    "* return a string, e.g., the cleaned text\n",
    "\n",
    "When the user provides a different value to `chars_to_remove`, e.g., {'a'}, then only those characters should be replaced by empty spaces in the text.\n",
    "\n",
    "Please store this function in a file called **assignment2_utils.py**. Please import the function and call it in this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_text_general(text, chars_to_remove=)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Exercise 6e: including and excluding words\n",
    "Please write Exercise 5a as a function. \n",
    "\n",
    "The function:\n",
    "* is called `exclude_and_count`\n",
    "* has one positional parameter `words`, which is a list of strings.\n",
    "* creates a dictionary in which the **key** is a word and the **value** is the frequency of that word. \n",
    "* words are excluded if they meet one of the following criteria:\n",
    "    * end with the letter e\n",
    "    * start with the letter t\n",
    "    * start with the letter c and end with the letter w (both conditions must be met)\n",
    "    * have six or more letters\n",
    "* returns a dictionary in which the **key** is the word and the **value** is the frequency of the word. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def exclude_and_count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7: Questions about Python\n",
    "Please answer the following questions about the Python language. You can give an examples if they help you.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a. What is the difference between positional parameters and keyword parameters?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "b. What is/are the type/types of containers that you define using curly brackets?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "c. When comparing the containers lists and sets, there are three properties that are unique for lists. Please name and explain them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "d. Explain the difference between **continue** and **break**. You can use an example for this. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}