{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Third Assignment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1) Create a function named **swap()**, which receives two tuples **a** and **b**, with two elements each. Your code should cause the last elements of the two tuples to be exchanged, and then return the two tuples. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2) Create the function **dist()** that takes as an argument two tuples that represent Cartesian coordinates of two points. Your function must return a number that corresponds to the Cartesian distance between these two points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3) Create the class **Ball**, to represent a sphere filled with water (weighing 1000g per cubic meter) with a radius **r**. Depending on the surface painting, it can have different weights: blue - weight 1g per square meter: yellow - 2g per square meter or red - 3g per square meter. The class parameter is a tuple **(r, color)**, an integer and a string, respectively. The **weight()** method of the class should return the total weight in kg of the ball (the weight of the water with the external weight of the surface). See example:\n", "\n", ">```python\n", ">>>> Ball((2, \"red\")).weight()\n", "> 33.66111808566343\n", ">\n", ">>>> Ball((3, \"blue\")).weight()\n", "> 113.21043286476177\n", "\n", "Hints: \n", "- Use $\\pi$ = 3.14159\n", "- Sphere volume: $\\frac43 \\pi r^3$\n", "- Surface area: $4\\pi r^2$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4) Roman numbers are occasionally used for time stamps or other purposes, but their manual conversion is somewhat laborious. Your task is to implement two functions, ***int_to_Roman()*** which converts an integer to Roman (in string) and **Roman_to_int()** which does the reverse. See examples below:\n", "\n", ">```python\n", ">>>> int_to_Roman(1)\n", "> I\n", ">>>> int_to_Roman(3000)\n", "> MMM\n", ">\n", ">>>> Roman_to_int('MMMCMLXXXVI')\n", "> 3986\n", ">>>> Roman_to_int('C')\n", "> 100\n", "\n", "Note: All test cases will be less than 4000, so you do not have to worry about the characters with bars above them, used in some versions of the Roman numbering system." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 5) The **area()** function receives a list **l** of ordered pairs. These ordered pairs represent the vertices of a convex polygon in the Cartesian plane traversed in a single direction. There is a mathematical method that, given the coordinates of the vertices of a polygon, the area can be calculated. You can find more details about the method by clicking [here](https://www.mathopenref.com/coordpolygonarea.html). Your code should make the function return the number that corresponds to the area of the polygon represented by the entry (rounded to two decimal places). See the following examples:\n", "\n", ">```python\n", ">>>> area([(0,0),(5,0),(13,8)])\n", "> 20.00\n", ">\n", ">>>> area([(2,0),(6,0),(10,4),(0,4)])\n", "> 28.00\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Challenge\n", "\n", "#### 6) The function **matches()** receives a list **l** of ordered distinct integers and an integer **n**. Its must return a list of tuples, each tuple with **n** elements, containing all possible combinations **n** by **n** (ordered, without repetitions) of the elements in the list **l**. You should only return a list **r** containing the generated tuples. Make sure that tuples are ordered in the list. See the examples below:\n", "\n", ">```python\n", ">>>> matches([1,2,3,4],2)\n", "> [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]\n", ">\n", ">>>> matches([1,2,3,4],3)\n", "> [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ".......\n", "----------------------------------------------------------------------\n", "Ran 7 tests in 0.006s\n", "\n", "OK\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import itertools, unittest\n", "\n", "class UnitTests(unittest.TestCase):\n", " \n", " def test_swap(self):\n", " self.assertEqual(swap((1,5),(9,0)),((1,0),(9,5)))\n", " self.assertEqual(swap((17,8),(1,13)),((17,13),(1,8)))\n", " self.assertEqual(swap((91,49),(36,25)),((91,25),(36,49)))\n", "\n", " def test_Ball(self):\n", " self.assertAlmostEqual(Ball((2,'blue')).weight(),Ball((2,'blue')).weight(),delta=0.3)\n", " self.assertAlmostEqual(Ball((4,'blue')).weight(),Ball((4,'blue')).weight(),delta=0.3)\n", " self.assertAlmostEqual(Ball((5,'red')).weight(),Ball((5,'red')).weight(),delta=0.3)\n", " self.assertAlmostEqual(Ball((10,'red')).weight(),Ball((10,'red')).weight(),delta=0.3)\n", " self.assertAlmostEqual(Ball((5,'yellow')).weight(),Ball((5,'yellow')).weight(),delta=0.3)\n", " self.assertAlmostEqual(Ball((10,'yellow')).weight(),Ball((10,'yellow')).weight(),delta=0.3)\n", "\n", " def test_int_to_Roman(self):\n", " self.assertEqual(int_to_Roman(1997),'MCMXCVII')\n", " self.assertEqual(int_to_Roman(2222),'MMCCXXII')\n", " self.assertEqual(int_to_Roman(1443),'MCDXLIII')\n", " self.assertEqual(int_to_Roman(101),'CI')\n", " self.assertEqual(int_to_Roman(999),'CMXCIX')\n", "\n", " def test_Roman_to_int(self):\n", " self.assertEqual(1997,Roman_to_int('MCMXCVII'))\n", " self.assertEqual(2222,Roman_to_int('MMCCXXII'))\n", " self.assertEqual(1443,Roman_to_int('MCDXLIII'))\n", " self.assertEqual(101,Roman_to_int('CI'))\n", " self.assertEqual(999,Roman_to_int('CMXCIX'))\n", "\n", " def test_dist(self):\n", " self.assertAlmostEqual(dist((3,2),(5,-1)),13**(0.5),delta=0.3)\n", " self.assertAlmostEqual(dist((6,1),(-7,-10)),290**(0.5),delta=0.3)\n", " self.assertAlmostEqual(dist((13,20),(17,0)),4*26**(0.5),delta=0.3)\n", " self.assertAlmostEqual(dist((4,0),(0,2)),2*5**(0.5),delta=0.3)\n", " self.assertAlmostEqual(dist((8,9),(2,2)),85**(0.5),delta=0.3)\n", "\n", " def test_area(self):\n", " l1=[(9,5),(5,6),(3,7),(1,6),(0,3),(1,1),(4,0),(9,1)]\n", " l2=[(1,-4),(-4,-5),(-6,-4),(-6,0),(-5,4),(-3,6),(1,8),(5,7),(8,2),(5,-2)]\n", " l3=[(3,10),(7,5),(2,1),(-3,0),(-5,3),(-3,8),(-1,10)]\n", " self.assertEqual(area(l1),46.5)\n", " self.assertEqual(area(l2),127)\n", " self.assertEqual(area(l3),78.5)\n", " \n", " def test_matches(self):\n", " l1,n1 = [0,1,3,4,6,8,9],3\n", " l2,n2 = [1,2,5,6,7,9,13,17,21], 5\n", " l3,n3 = [0,10,14,16,21,33,67,91,153,205],2\n", " self.assertEqual(matches(l1,n1),[el for el in itertools.combinations(l1, n1)])\n", " self.assertEqual(matches(l2,n2),[el for el in itertools.combinations(l2, n2)])\n", " self.assertEqual(matches(l3,n3),[el for el in itertools.combinations(l3, n3)])\n", " \n", "\n", "unittest.main(argv=['first-arg-is-ignored'], exit=False)" ] } ], "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 }