{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Fourth Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1) Suppose that in this term you have only two courses: Calculus II and Programming Languages. Create a function **grades()** that receives two tuples (or lists) of three elements representing the grades of the assigments A1, A2 and A3 in that order. The first tuple (or list) refers to the grades of Calculus II and the second to the grades of Programming Languages. Return a dictionary whose keys are the names of the course and the values are another dict with keys 'A1', 'A2' and 'A3' and values being the corresponding grades. See example below.\n", "\n", ">```python\n", ">>>> grades((4,8,7),(9,8,0))\n", ">{'Calculus II': {'A1': 4, 'A2': 8, 'A3': 7}, 'Programming Languages': {'A1': 9, 'A2': 8, 'A3': 0}}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2) Create the function **sorts()**, which takes as a single argument a dictionary **d**, and sorts that dictionary based on its values, returning the ordered dictionary. Test cases will not have equal values. See examples:\n", "\n", ">```python\n", ">>>> sorts({1: 2, 3: 4, 4: 3, 2: 1, 0: 0})\n", ">{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}\n", ">\n", ">>>> sorts({\"fish\":1, \"chicken\":10, \"beef\":5, \"pork\":8})\n", ">{\"fish\":1, \"cow\":5, \"pork\":8, \"chicken\":10}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3) Create the function **concatenate()** so that it receives as arguments an indefinite number of dictionaries. Hence, concatenate them in the given order, generating a single dictionary that must be returned as the function result. Test cases will not have keys in common. See the examples:\n", "\n", ">```python\n", ">>>> concatenate({1:'a',3:'c'},{2:'b',4:'d'},{5:'e',6:'f'})\n", ">{1:'a',3:'c',2:'b',4:'d',5:'e',6:'f'}\n", ">\n", ">>>> concatenate({'a':1,'b':2,'e':5},{'d':4,'c':3,'f':6})\n", ">{'a':1,'b':2,'e':5,'d':4,'c':3,'f':6}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4) Create a class called **Triangle**, whose constructor receives the measurements from its three sides (in any order). Implement a method called `type`, which returns:\n", "- 0 if the measurements do not form a triangle\n", "- 1 if the triangle is equilateral\n", "- 2 if the triangle is isosceles\n", "- 3 if the triangle is scalene\n", "\n", "See the example:\n", "\n", ">```python\n", ">>>> tri = Triangle(5,5,5) #Equilateral\n", ">>>> tri.type() \n", ">1\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5) Create the `Point` class representing a point object in the Cartesian plane, with methods `show()`, `move()` and `dist()`. The first must return a tuple with the coordinates of the point. The second must take as arguments the displacements in each of the axes and update the position of the object. The latter, on the other hand, must take another point as an argument and return the Euclidean distance between the two points. \n", "\n", "#### Obs: The unitary tests for `dist()` will accept a margin of error of 0.1. Do not change the existing lines in this question and do not create or remove functions or classes. With the class properly implemented, it should be possible to execute the following series of commands:\n", "\n", ">```python\n", ">>>> p1 = Point(2, 3)\n", ">>>> p2 = Point(3, 3)\n", ">>>> p1.show()\n", ">(2, 3)\n", ">>>> p2.show()\n", ">(3, 3)\n", ">>>> p1.move(10, -10)\n", ">>>> p1.show()\n", ">(12, -7)\n", ">>>> p2.show()\n", ">(3, 3)\n", ">>>> p1.dist(p2)\n", ">13.45362404707371\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6) Create two classes `employee` and `manager` so that manager is a subclass of employee. The employee attributes are name, ssid, salary and department. In addition to these, the manager class must also include the password and the number of employees he/she manages. Make sure your constructors are consistent. In addition, the employee class must have the method `bonus()`, which does not receive parameters and increases the employee's salary by 10%. The manager class must have the methods `authenticate_password(password)`, which returns a Boolean resulting from the validation of the password against the entry, and the method `bonus()`, which increases your salary by 15%. Do not change the existing lines in this question and do not create or remove functions or classes.\n", "\n", "```python\n", ">>> f1=employee(\"John\",12345678900,2500,\"TI\")\n", ">>> f2=employee(\"Paul\",12345678901,1800,\"TI\")\n", ">>> f3=employee(\"Marta\",23456789012,6000,\"TI\",101101,2)\n", ">>> f1.name()\n", "John\n", ">>> f2.ssid()\n", "12345678901\n", ">>> f3.department()\n", "IT\n", ">>> f2.bonus()\n", ">>> f2.salary()\n", "1980.00\n", ">>> f3.bonus()\n", "6900.00\n", ">>> f3.authenticate_password(101101)\n", "True\n", ">>> f3.authenticate_password(123456)\n", "False\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Challenge\n", "\n", "#### 7) There is a file called \"alice.txt\", which is the first chapter of the book Alice in Wonderland. The text has already been properly cleaned, punctuation was removed, as well as special characters and unnecessary spacing. There is a semi-ready function that reads the file and loads the text into the string-type variable called \"alice\"; you have to modify this function to return a dictionary whose keys are the unique words in the text, and the values are the number of times each word is repeated in the chapter (frequency distribution) - do not use the method collections.Counter.\n", "\n", "#### Extra: Try to discover the top 10 most used words. See the image below to get an idea of the answer (The bigger the word, the more frequent it appears).\n", "\n", "![](../Data/Figs/alice.png)\n", "\n", "Book: http://www.gutenberg.org/files/11/11-0.txt\n", "\n", "Image: https://pypi.org/project/wordcloud/" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def read_text():\n", " with open('../Data/TXT/alice.txt','r') as f:\n", " alice = f.read()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }