diff --git a/.ipynb_checkpoints/2-checkpoint.ipynb b/.ipynb_checkpoints/2-checkpoint.ipynb new file mode 100644 index 0000000..45e5df4 --- /dev/null +++ b/.ipynb_checkpoints/2-checkpoint.ipynb @@ -0,0 +1,1346 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2: Sets\n", + "\n", + "There are a lot to learn about Python Sets and the information presented in the lesson is limited due to its length. To learn Python Sets in depth you are strongly encouraged to review the W3Schools tutorial on [Python Sets Examples and Methods](https://www.w3schools.com/python/python_sets.asp) before you work on this lab. Some difficult questions in this lab have their solutions in the W3Schools tutorial.\n", + "\n", + "#### First, import the Python `random` libary" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In the cell below, create a list named `sample_list_1` with 80 random values. \n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* Each value in the list is unique.\n", + "\n", + "Print `sample_list_1` to review its values\n", + "\n", + "*Hint: use `random.sample` ([reference](https://docs.python.org/3/library/random.html#random.sample)).*" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[56,\n", + " 67,\n", + " 71,\n", + " 83,\n", + " 7,\n", + " 59,\n", + " 39,\n", + " 51,\n", + " 99,\n", + " 3,\n", + " 13,\n", + " 57,\n", + " 35,\n", + " 42,\n", + " 43,\n", + " 38,\n", + " 63,\n", + " 6,\n", + " 44,\n", + " 48,\n", + " 37,\n", + " 74,\n", + " 27,\n", + " 46,\n", + " 76,\n", + " 5,\n", + " 85,\n", + " 40,\n", + " 82,\n", + " 80,\n", + " 9,\n", + " 4,\n", + " 66,\n", + " 86,\n", + " 0,\n", + " 77,\n", + " 84,\n", + " 58,\n", + " 8,\n", + " 90,\n", + " 29,\n", + " 2,\n", + " 34,\n", + " 33,\n", + " 52,\n", + " 15,\n", + " 89,\n", + " 92,\n", + " 79,\n", + " 73,\n", + " 21,\n", + " 69,\n", + " 95,\n", + " 28,\n", + " 53,\n", + " 49,\n", + " 65,\n", + " 45,\n", + " 75,\n", + " 68,\n", + " 64,\n", + " 47,\n", + " 72,\n", + " 19,\n", + " 17,\n", + " 94,\n", + " 96,\n", + " 10,\n", + " 31,\n", + " 23,\n", + " 97,\n", + " 14,\n", + " 81,\n", + " 50,\n", + " 62,\n", + " 93,\n", + " 60,\n", + " 78,\n", + " 22,\n", + " 41]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = list(random.sample(range(100),80))\n", + "sample_list_1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "56 1\n", + "67 1\n", + "71 1\n", + "83 1\n", + "7 1\n", + "59 1\n", + "39 1\n", + "51 1\n", + "99 1\n", + "3 1\n", + "13 1\n", + "57 1\n", + "35 1\n", + "42 1\n", + "43 1\n", + "38 1\n", + "63 1\n", + "6 1\n", + "44 1\n", + "48 1\n", + "37 1\n", + "74 1\n", + "27 1\n", + "46 1\n", + "76 1\n", + "5 1\n", + "85 1\n", + "40 1\n", + "82 1\n", + "80 1\n", + "9 1\n", + "4 1\n", + "66 1\n", + "86 1\n", + "0 1\n", + "77 1\n", + "84 1\n", + "58 1\n", + "8 1\n", + "90 1\n", + "29 1\n", + "2 1\n", + "34 1\n", + "33 1\n", + "52 1\n", + "15 1\n", + "89 1\n", + "92 1\n", + "79 1\n", + "73 1\n", + "21 1\n", + "69 1\n", + "95 1\n", + "28 1\n", + "53 1\n", + "49 1\n", + "65 1\n", + "45 1\n", + "75 1\n", + "68 1\n", + "64 1\n", + "47 1\n", + "72 1\n", + "19 1\n", + "17 1\n", + "94 1\n", + "96 1\n", + "10 1\n", + "31 1\n", + "23 1\n", + "97 1\n", + "14 1\n", + "81 1\n", + "50 1\n", + "62 1\n", + "93 1\n", + "60 1\n", + "78 1\n", + "22 1\n", + "41 1\n" + ] + } + ], + "source": [ + "# Count the number of times a number appears in sample_list\n", + "\n", + "for number in sample_list_1:\n", + " count = sample_list_1.count(number)\n", + " print(number, count)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create another list named `sample_list_2` with 80 random values.\n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* The values in the list don't have to be unique.\n", + "\n", + "*Hint: Use a FOR loop.*" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5,\n", + " 71,\n", + " 62,\n", + " 77,\n", + " 86,\n", + " 37,\n", + " 21,\n", + " 0,\n", + " 23,\n", + " 76,\n", + " 28,\n", + " 9,\n", + " 30,\n", + " 51,\n", + " 9,\n", + " 98,\n", + " 84,\n", + " 44,\n", + " 18,\n", + " 98,\n", + " 100,\n", + " 17,\n", + " 42,\n", + " 98,\n", + " 62,\n", + " 7,\n", + " 99,\n", + " 40,\n", + " 96,\n", + " 56,\n", + " 87,\n", + " 4,\n", + " 60,\n", + " 39,\n", + " 6,\n", + " 51,\n", + " 3,\n", + " 46,\n", + " 3,\n", + " 62,\n", + " 40,\n", + " 0,\n", + " 27,\n", + " 9,\n", + " 10,\n", + " 28,\n", + " 73,\n", + " 92,\n", + " 50,\n", + " 15,\n", + " 51,\n", + " 21,\n", + " 4,\n", + " 62,\n", + " 81,\n", + " 62,\n", + " 45,\n", + " 37,\n", + " 13,\n", + " 13,\n", + " 72,\n", + " 76,\n", + " 73,\n", + " 41,\n", + " 99,\n", + " 1,\n", + " 21,\n", + " 53,\n", + " 13,\n", + " 83,\n", + " 82,\n", + " 57,\n", + " 12,\n", + " 65,\n", + " 48,\n", + " 40,\n", + " 83,\n", + " 89,\n", + " 33,\n", + " 97]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80): \n", + " sample_list_2.append(random.randint(0,100))\n", + " \n", + "sample_list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n", + "71 1\n", + "62 5\n", + "77 1\n", + "86 1\n", + "37 2\n", + "21 3\n", + "0 2\n", + "23 1\n", + "76 2\n", + "28 2\n", + "9 3\n", + "30 1\n", + "51 3\n", + "9 3\n", + "98 3\n", + "84 1\n", + "44 1\n", + "18 1\n", + "98 3\n", + "100 1\n", + "17 1\n", + "42 1\n", + "98 3\n", + "62 5\n", + "7 1\n", + "99 2\n", + "40 3\n", + "96 1\n", + "56 1\n", + "87 1\n", + "4 2\n", + "60 1\n", + "39 1\n", + "6 1\n", + "51 3\n", + "3 2\n", + "46 1\n", + "3 2\n", + "62 5\n", + "40 3\n", + "0 2\n", + "27 1\n", + "9 3\n", + "10 1\n", + "28 2\n", + "73 2\n", + "92 1\n", + "50 1\n", + "15 1\n", + "51 3\n", + "21 3\n", + "4 2\n", + "62 5\n", + "81 1\n", + "62 5\n", + "45 1\n", + "37 2\n", + "13 3\n", + "13 3\n", + "72 1\n", + "76 2\n", + "73 2\n", + "41 1\n", + "99 2\n", + "1 1\n", + "21 3\n", + "53 1\n", + "13 3\n", + "83 2\n", + "82 1\n", + "57 1\n", + "12 1\n", + "65 1\n", + "48 1\n", + "40 3\n", + "83 2\n", + "89 1\n", + "33 1\n", + "97 1\n", + "80\n" + ] + } + ], + "source": [ + "for number in sample_list_2:\n", + " count = sample_list_2.count(number)\n", + " print(number, count)\n", + "print(len(sample_list_2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(sample_list_2))\n", + "print(len(set2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 8,\n", + " 14,\n", + " 19,\n", + " 22,\n", + " 29,\n", + " 31,\n", + " 34,\n", + " 35,\n", + " 38,\n", + " 43,\n", + " 47,\n", + " 49,\n", + " 52,\n", + " 58,\n", + " 59,\n", + " 63,\n", + " 64,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 74,\n", + " 75,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 85,\n", + " 90,\n", + " 93,\n", + " 94,\n", + " 95}" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set3 = set1.difference(set2)\n", + "set3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 12, 18, 30, 87, 98, 100}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 21,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 37,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 48,\n", + " 50,\n", + " 51,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 60,\n", + " 62,\n", + " 65,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 76,\n", + " 77,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 86,\n", + " 89,\n", + " 92,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "set5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the relationship among the following values:\n", + "\n", + "* len(set1)\n", + "* len(set2)\n", + "* len(set3)\n", + "* len(set4)\n", + "* len(set5)\n", + "\n", + "Use a math formular to represent that relationship. Test your formular with Python code." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n", + "32\n", + "7\n", + "48\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(set1))\n", + "print(len(set2))\n", + "print(len(set3))\n", + "print(len(set4))\n", + "print(len(set5))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(set4) < len(set3) < len(set5) < len(set2) < len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set6 = set()\n", + "type(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class set in module builtins:\n", + "\n", + "class set(object)\n", + " | set() -> new empty set object\n", + " | set(iterable) -> new set object\n", + " | \n", + " | Build an unordered collection of unique elements.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __and__(self, value, /)\n", + " | Return self&value.\n", + " | \n", + " | __contains__(...)\n", + " | x.__contains__(y) <==> y in x.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iand__(self, value, /)\n", + " | Return self&=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __ior__(self, value, /)\n", + " | Return self|=value.\n", + " | \n", + " | __isub__(self, value, /)\n", + " | Return self-=value.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __ixor__(self, value, /)\n", + " | Return self^=value.\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self size of S in memory, in bytes\n", + " | \n", + " | __sub__(self, value, /)\n", + " | Return self-value.\n", + " | \n", + " | __xor__(self, value, /)\n", + " | Return self^value.\n", + " | \n", + " | add(...)\n", + " | Add an element to a set.\n", + " | \n", + " | This has no effect if the element is already present.\n", + " | \n", + " | clear(...)\n", + " | Remove all elements from this set.\n", + " | \n", + " | copy(...)\n", + " | Return a shallow copy of a set.\n", + " | \n", + " | difference(...)\n", + " | Return the difference of two or more sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in this set but not the others.)\n", + " | \n", + " | difference_update(...)\n", + " | Remove all elements of another set from this set.\n", + " | \n", + " | discard(...)\n", + " | Remove an element from a set if it is a member.\n", + " | \n", + " | If the element is not a member, do nothing.\n", + " | \n", + " | intersection(...)\n", + " | Return the intersection of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in both sets.)\n", + " | \n", + " | intersection_update(...)\n", + " | Update a set with the intersection of itself and another.\n", + " | \n", + " | isdisjoint(...)\n", + " | Return True if two sets have a null intersection.\n", + " | \n", + " | issubset(...)\n", + " | Report whether another set contains this set.\n", + " | \n", + " | issuperset(...)\n", + " | Report whether this set contains another set.\n", + " | \n", + " | pop(...)\n", + " | Remove and return an arbitrary set element.\n", + " | Raises KeyError if the set is empty.\n", + " | \n", + " | remove(...)\n", + " | Remove an element from a set; it must be a member.\n", + " | \n", + " | If the element is not a member, raise a KeyError.\n", + " | \n", + " | symmetric_difference(...)\n", + " | Return the symmetric difference of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in exactly one of the sets.)\n", + " | \n", + " | symmetric_difference_update(...)\n", + " | Update a set with the symmetric difference of itself and another.\n", + " | \n", + " | union(...)\n", + " | Return the union of sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in either set.)\n", + " | \n", + " | update(...)\n", + " | Update a set with the union of itself and others.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | __class_getitem__(...) from builtins.type\n", + " | See PEP 585\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "help(set)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 17, 19, 21, 22, 23, 27, 28, 29, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 89, 90, 92, 93, 94, 95, 96, 97, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set5)\n", + "set6.update(set3)\n", + "print(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 == set6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set2.issubset(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.issubset(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "\n", + "#### Check if the aggregated values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set3.union(set4,set5))\n", + "print(set1.union(set2))" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.union(set4,set5) == set1.union(set2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the `pop` method, remove the first element from `set1`." + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 19,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 31,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 51,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 59,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 89,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1.pop()\n", + "set1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\n", + "```\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 50,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 80,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97}" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for number in list_to_remove:\n", + " if number in set1:\n", + " set1.remove(number)\n", + " \n", + "set1" + ] + }, + { + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/2.ipynb b/2.ipynb new file mode 100644 index 0000000..45e5df4 --- /dev/null +++ b/2.ipynb @@ -0,0 +1,1346 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2: Sets\n", + "\n", + "There are a lot to learn about Python Sets and the information presented in the lesson is limited due to its length. To learn Python Sets in depth you are strongly encouraged to review the W3Schools tutorial on [Python Sets Examples and Methods](https://www.w3schools.com/python/python_sets.asp) before you work on this lab. Some difficult questions in this lab have their solutions in the W3Schools tutorial.\n", + "\n", + "#### First, import the Python `random` libary" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In the cell below, create a list named `sample_list_1` with 80 random values. \n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* Each value in the list is unique.\n", + "\n", + "Print `sample_list_1` to review its values\n", + "\n", + "*Hint: use `random.sample` ([reference](https://docs.python.org/3/library/random.html#random.sample)).*" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[56,\n", + " 67,\n", + " 71,\n", + " 83,\n", + " 7,\n", + " 59,\n", + " 39,\n", + " 51,\n", + " 99,\n", + " 3,\n", + " 13,\n", + " 57,\n", + " 35,\n", + " 42,\n", + " 43,\n", + " 38,\n", + " 63,\n", + " 6,\n", + " 44,\n", + " 48,\n", + " 37,\n", + " 74,\n", + " 27,\n", + " 46,\n", + " 76,\n", + " 5,\n", + " 85,\n", + " 40,\n", + " 82,\n", + " 80,\n", + " 9,\n", + " 4,\n", + " 66,\n", + " 86,\n", + " 0,\n", + " 77,\n", + " 84,\n", + " 58,\n", + " 8,\n", + " 90,\n", + " 29,\n", + " 2,\n", + " 34,\n", + " 33,\n", + " 52,\n", + " 15,\n", + " 89,\n", + " 92,\n", + " 79,\n", + " 73,\n", + " 21,\n", + " 69,\n", + " 95,\n", + " 28,\n", + " 53,\n", + " 49,\n", + " 65,\n", + " 45,\n", + " 75,\n", + " 68,\n", + " 64,\n", + " 47,\n", + " 72,\n", + " 19,\n", + " 17,\n", + " 94,\n", + " 96,\n", + " 10,\n", + " 31,\n", + " 23,\n", + " 97,\n", + " 14,\n", + " 81,\n", + " 50,\n", + " 62,\n", + " 93,\n", + " 60,\n", + " 78,\n", + " 22,\n", + " 41]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = list(random.sample(range(100),80))\n", + "sample_list_1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "56 1\n", + "67 1\n", + "71 1\n", + "83 1\n", + "7 1\n", + "59 1\n", + "39 1\n", + "51 1\n", + "99 1\n", + "3 1\n", + "13 1\n", + "57 1\n", + "35 1\n", + "42 1\n", + "43 1\n", + "38 1\n", + "63 1\n", + "6 1\n", + "44 1\n", + "48 1\n", + "37 1\n", + "74 1\n", + "27 1\n", + "46 1\n", + "76 1\n", + "5 1\n", + "85 1\n", + "40 1\n", + "82 1\n", + "80 1\n", + "9 1\n", + "4 1\n", + "66 1\n", + "86 1\n", + "0 1\n", + "77 1\n", + "84 1\n", + "58 1\n", + "8 1\n", + "90 1\n", + "29 1\n", + "2 1\n", + "34 1\n", + "33 1\n", + "52 1\n", + "15 1\n", + "89 1\n", + "92 1\n", + "79 1\n", + "73 1\n", + "21 1\n", + "69 1\n", + "95 1\n", + "28 1\n", + "53 1\n", + "49 1\n", + "65 1\n", + "45 1\n", + "75 1\n", + "68 1\n", + "64 1\n", + "47 1\n", + "72 1\n", + "19 1\n", + "17 1\n", + "94 1\n", + "96 1\n", + "10 1\n", + "31 1\n", + "23 1\n", + "97 1\n", + "14 1\n", + "81 1\n", + "50 1\n", + "62 1\n", + "93 1\n", + "60 1\n", + "78 1\n", + "22 1\n", + "41 1\n" + ] + } + ], + "source": [ + "# Count the number of times a number appears in sample_list\n", + "\n", + "for number in sample_list_1:\n", + " count = sample_list_1.count(number)\n", + " print(number, count)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create another list named `sample_list_2` with 80 random values.\n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* The values in the list don't have to be unique.\n", + "\n", + "*Hint: Use a FOR loop.*" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5,\n", + " 71,\n", + " 62,\n", + " 77,\n", + " 86,\n", + " 37,\n", + " 21,\n", + " 0,\n", + " 23,\n", + " 76,\n", + " 28,\n", + " 9,\n", + " 30,\n", + " 51,\n", + " 9,\n", + " 98,\n", + " 84,\n", + " 44,\n", + " 18,\n", + " 98,\n", + " 100,\n", + " 17,\n", + " 42,\n", + " 98,\n", + " 62,\n", + " 7,\n", + " 99,\n", + " 40,\n", + " 96,\n", + " 56,\n", + " 87,\n", + " 4,\n", + " 60,\n", + " 39,\n", + " 6,\n", + " 51,\n", + " 3,\n", + " 46,\n", + " 3,\n", + " 62,\n", + " 40,\n", + " 0,\n", + " 27,\n", + " 9,\n", + " 10,\n", + " 28,\n", + " 73,\n", + " 92,\n", + " 50,\n", + " 15,\n", + " 51,\n", + " 21,\n", + " 4,\n", + " 62,\n", + " 81,\n", + " 62,\n", + " 45,\n", + " 37,\n", + " 13,\n", + " 13,\n", + " 72,\n", + " 76,\n", + " 73,\n", + " 41,\n", + " 99,\n", + " 1,\n", + " 21,\n", + " 53,\n", + " 13,\n", + " 83,\n", + " 82,\n", + " 57,\n", + " 12,\n", + " 65,\n", + " 48,\n", + " 40,\n", + " 83,\n", + " 89,\n", + " 33,\n", + " 97]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80): \n", + " sample_list_2.append(random.randint(0,100))\n", + " \n", + "sample_list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n", + "71 1\n", + "62 5\n", + "77 1\n", + "86 1\n", + "37 2\n", + "21 3\n", + "0 2\n", + "23 1\n", + "76 2\n", + "28 2\n", + "9 3\n", + "30 1\n", + "51 3\n", + "9 3\n", + "98 3\n", + "84 1\n", + "44 1\n", + "18 1\n", + "98 3\n", + "100 1\n", + "17 1\n", + "42 1\n", + "98 3\n", + "62 5\n", + "7 1\n", + "99 2\n", + "40 3\n", + "96 1\n", + "56 1\n", + "87 1\n", + "4 2\n", + "60 1\n", + "39 1\n", + "6 1\n", + "51 3\n", + "3 2\n", + "46 1\n", + "3 2\n", + "62 5\n", + "40 3\n", + "0 2\n", + "27 1\n", + "9 3\n", + "10 1\n", + "28 2\n", + "73 2\n", + "92 1\n", + "50 1\n", + "15 1\n", + "51 3\n", + "21 3\n", + "4 2\n", + "62 5\n", + "81 1\n", + "62 5\n", + "45 1\n", + "37 2\n", + "13 3\n", + "13 3\n", + "72 1\n", + "76 2\n", + "73 2\n", + "41 1\n", + "99 2\n", + "1 1\n", + "21 3\n", + "53 1\n", + "13 3\n", + "83 2\n", + "82 1\n", + "57 1\n", + "12 1\n", + "65 1\n", + "48 1\n", + "40 3\n", + "83 2\n", + "89 1\n", + "33 1\n", + "97 1\n", + "80\n" + ] + } + ], + "source": [ + "for number in sample_list_2:\n", + " count = sample_list_2.count(number)\n", + " print(number, count)\n", + "print(len(sample_list_2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(sample_list_2))\n", + "print(len(set2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 8,\n", + " 14,\n", + " 19,\n", + " 22,\n", + " 29,\n", + " 31,\n", + " 34,\n", + " 35,\n", + " 38,\n", + " 43,\n", + " 47,\n", + " 49,\n", + " 52,\n", + " 58,\n", + " 59,\n", + " 63,\n", + " 64,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 74,\n", + " 75,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 85,\n", + " 90,\n", + " 93,\n", + " 94,\n", + " 95}" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set3 = set1.difference(set2)\n", + "set3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 12, 18, 30, 87, 98, 100}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 21,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 37,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 48,\n", + " 50,\n", + " 51,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 60,\n", + " 62,\n", + " 65,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 76,\n", + " 77,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 86,\n", + " 89,\n", + " 92,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "set5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the relationship among the following values:\n", + "\n", + "* len(set1)\n", + "* len(set2)\n", + "* len(set3)\n", + "* len(set4)\n", + "* len(set5)\n", + "\n", + "Use a math formular to represent that relationship. Test your formular with Python code." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n", + "32\n", + "7\n", + "48\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(set1))\n", + "print(len(set2))\n", + "print(len(set3))\n", + "print(len(set4))\n", + "print(len(set5))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(set4) < len(set3) < len(set5) < len(set2) < len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set6 = set()\n", + "type(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class set in module builtins:\n", + "\n", + "class set(object)\n", + " | set() -> new empty set object\n", + " | set(iterable) -> new set object\n", + " | \n", + " | Build an unordered collection of unique elements.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __and__(self, value, /)\n", + " | Return self&value.\n", + " | \n", + " | __contains__(...)\n", + " | x.__contains__(y) <==> y in x.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iand__(self, value, /)\n", + " | Return self&=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __ior__(self, value, /)\n", + " | Return self|=value.\n", + " | \n", + " | __isub__(self, value, /)\n", + " | Return self-=value.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __ixor__(self, value, /)\n", + " | Return self^=value.\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self size of S in memory, in bytes\n", + " | \n", + " | __sub__(self, value, /)\n", + " | Return self-value.\n", + " | \n", + " | __xor__(self, value, /)\n", + " | Return self^value.\n", + " | \n", + " | add(...)\n", + " | Add an element to a set.\n", + " | \n", + " | This has no effect if the element is already present.\n", + " | \n", + " | clear(...)\n", + " | Remove all elements from this set.\n", + " | \n", + " | copy(...)\n", + " | Return a shallow copy of a set.\n", + " | \n", + " | difference(...)\n", + " | Return the difference of two or more sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in this set but not the others.)\n", + " | \n", + " | difference_update(...)\n", + " | Remove all elements of another set from this set.\n", + " | \n", + " | discard(...)\n", + " | Remove an element from a set if it is a member.\n", + " | \n", + " | If the element is not a member, do nothing.\n", + " | \n", + " | intersection(...)\n", + " | Return the intersection of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in both sets.)\n", + " | \n", + " | intersection_update(...)\n", + " | Update a set with the intersection of itself and another.\n", + " | \n", + " | isdisjoint(...)\n", + " | Return True if two sets have a null intersection.\n", + " | \n", + " | issubset(...)\n", + " | Report whether another set contains this set.\n", + " | \n", + " | issuperset(...)\n", + " | Report whether this set contains another set.\n", + " | \n", + " | pop(...)\n", + " | Remove and return an arbitrary set element.\n", + " | Raises KeyError if the set is empty.\n", + " | \n", + " | remove(...)\n", + " | Remove an element from a set; it must be a member.\n", + " | \n", + " | If the element is not a member, raise a KeyError.\n", + " | \n", + " | symmetric_difference(...)\n", + " | Return the symmetric difference of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in exactly one of the sets.)\n", + " | \n", + " | symmetric_difference_update(...)\n", + " | Update a set with the symmetric difference of itself and another.\n", + " | \n", + " | union(...)\n", + " | Return the union of sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in either set.)\n", + " | \n", + " | update(...)\n", + " | Update a set with the union of itself and others.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | __class_getitem__(...) from builtins.type\n", + " | See PEP 585\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "help(set)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 17, 19, 21, 22, 23, 27, 28, 29, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 89, 90, 92, 93, 94, 95, 96, 97, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set5)\n", + "set6.update(set3)\n", + "print(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 == set6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set2.issubset(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.issubset(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "\n", + "#### Check if the aggregated values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set3.union(set4,set5))\n", + "print(set1.union(set2))" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.union(set4,set5) == set1.union(set2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the `pop` method, remove the first element from `set1`." + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 19,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 31,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 51,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 59,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 89,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1.pop()\n", + "set1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\n", + "```\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 50,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 80,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97}" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for number in list_to_remove:\n", + " if number in set1:\n", + " set1.remove(number)\n", + " \n", + "set1" + ] + }, + { + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb new file mode 100644 index 0000000..abdce71 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,481 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 1: Tuples\n", + "\n", + "#### Do you know you can create tuples with only one element?\n", + "\n", + "**In the cell below, define a variable `tup` with a single element `\"I\"`.**\n", + "\n", + "*Hint: you need to add a comma (`,`) after the single element.*" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n", + "tup = (\"I\",)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Print the type of `tup`. \n", + "\n", + "Make sure its type is correct (i.e. *tuple* instead of *str*)." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(type(tup))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now try to append the following elements to `tup`. \n", + "\n", + "Are you able to do it? Explain.\n", + "\n", + "```\n", + "\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n", + "tup.add(\"r\",\"o\",\"n\",\"h\",\"a\",\"c\",\"k\")\n", + "# Your explanation here\n", + "# Unable to add elements to a tuple due to it's unmutable nature" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### How about re-assign a new value to an existing tuple?\n", + "\n", + "Re-assign the following elements to `tup`. Are you able to do it? Explain.\n", + "\n", + "```\n", + "\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class tuple in module builtins:\n", + "\n", + "class tuple(object)\n", + " | tuple(iterable=(), /)\n", + " | \n", + " | Built-in immutable sequence.\n", + " | \n", + " | If no argument is given, the constructor returns an empty tuple.\n", + " | If iterable is specified the tuple is initialized from iterable's items.\n", + " | \n", + " | If the argument is a tuple, the return value is the same object.\n", + " | \n", + " | Built-in subclasses:\n", + " | asyncgen_hooks\n", + " | UnraisableHookArgs\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(self, key, /)\n", + " | Return self[key].\n", + " | \n", + " | __getnewargs__(self, /)\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __hash__(self, /)\n", + " | Return hash(self).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self \n", + "('I', 'r', 'o', 'n') ('h', 'a', 'c', 'k')\n" + ] + } + ], + "source": [ + "# Your code here\n", + "# Con slicing\n", + "tup = (\"I\",\"r\",\"o\",\"n\",\"h\",\"a\",\"c\",\"k\")\n", + "tup1 = tuple(tup[0:4])\n", + "tup2 = tuple(tup[-4:])\n", + "\n", + "print(type(tup1),type(tup2))\n", + "print(tup1,tup2)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "# Con ciclo for\n", + "tup1=[]\n", + "tup2=[]\n", + "for i in tup:\n", + " if tup.index(i) < 4:\n", + " tup1.append(i)\n", + " else:\n", + " tup2.append(i)\n", + "\n", + "tup1 = tuple(tup1)\n", + "tup2 = tuple(tup2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(('I', 'r', 'o', 'n'), ('h', 'a', 'c', 'k'))" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup1, tup2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `tup1` and `tup2` into `tup3` using the `+` operator.\n", + "\n", + "Then print `tup3` and check if `tup3` equals to `tup`." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], + "source": [ + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "print(tup3)\n", + "# Cannot add tuples by using the + operator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Count the number of elements in `tup1` and `tup2`. Then add the two counts together and check if the sum is the same as the number of elements in `tup3`" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "8\n" + ] + } + ], + "source": [ + "# Your code here\n", + "count1 = len(tup1)\n", + "count2 = len(tup2)\n", + "print(count1 + count2)\n", + "print(len(tup3))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the index number of `\"h\"` in `tup3`?" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "tup3.index(\"h\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", + "\n", + "```\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "```\n", + "\n", + "For each letter you check, print `True` if it is present in `tup3` otherwise print `False`.\n", + "\n", + "*Hint: you only need to loop `letters`. You don't need to loop `tup3` because there is a Python operator `in` you can use. See [reference](https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element-in-python).*" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a : True\n", + "Letter b : False\n", + "Letter c : True\n", + "Letter d : False\n", + "Letter e : False\n" + ] + } + ], + "source": [ + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(\"Letter\", letter, \": True\")\n", + " else:\n", + " print(\"Letter\", letter, \": False\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### How many times does each letter in `letters` appear in `tup3`?\n", + "\n", + "Print out the number of occurrence of each letter." + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a appears 1 time(s) in tup3\n", + "Letter b appears 0 time(s) in tup3\n", + "Letter c appears 1 time(s) in tup3\n", + "Letter d appears 0 time(s) in tup3\n", + "Letter e appears 0 time(s) in tup3\n" + ] + } + ], + "source": [ + "# Your code here\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(\"Letter\", letter, \"appears\", tup3.count(letter), \"time(s) in tup3\")\n", + " else:\n", + " print(\"Letter\", letter, \"appears\", tup3.count(letter), \"time(s) in tup3\")" + ] + }, + { + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb new file mode 100644 index 0000000..45e5df4 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -0,0 +1,1346 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2: Sets\n", + "\n", + "There are a lot to learn about Python Sets and the information presented in the lesson is limited due to its length. To learn Python Sets in depth you are strongly encouraged to review the W3Schools tutorial on [Python Sets Examples and Methods](https://www.w3schools.com/python/python_sets.asp) before you work on this lab. Some difficult questions in this lab have their solutions in the W3Schools tutorial.\n", + "\n", + "#### First, import the Python `random` libary" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In the cell below, create a list named `sample_list_1` with 80 random values. \n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* Each value in the list is unique.\n", + "\n", + "Print `sample_list_1` to review its values\n", + "\n", + "*Hint: use `random.sample` ([reference](https://docs.python.org/3/library/random.html#random.sample)).*" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[56,\n", + " 67,\n", + " 71,\n", + " 83,\n", + " 7,\n", + " 59,\n", + " 39,\n", + " 51,\n", + " 99,\n", + " 3,\n", + " 13,\n", + " 57,\n", + " 35,\n", + " 42,\n", + " 43,\n", + " 38,\n", + " 63,\n", + " 6,\n", + " 44,\n", + " 48,\n", + " 37,\n", + " 74,\n", + " 27,\n", + " 46,\n", + " 76,\n", + " 5,\n", + " 85,\n", + " 40,\n", + " 82,\n", + " 80,\n", + " 9,\n", + " 4,\n", + " 66,\n", + " 86,\n", + " 0,\n", + " 77,\n", + " 84,\n", + " 58,\n", + " 8,\n", + " 90,\n", + " 29,\n", + " 2,\n", + " 34,\n", + " 33,\n", + " 52,\n", + " 15,\n", + " 89,\n", + " 92,\n", + " 79,\n", + " 73,\n", + " 21,\n", + " 69,\n", + " 95,\n", + " 28,\n", + " 53,\n", + " 49,\n", + " 65,\n", + " 45,\n", + " 75,\n", + " 68,\n", + " 64,\n", + " 47,\n", + " 72,\n", + " 19,\n", + " 17,\n", + " 94,\n", + " 96,\n", + " 10,\n", + " 31,\n", + " 23,\n", + " 97,\n", + " 14,\n", + " 81,\n", + " 50,\n", + " 62,\n", + " 93,\n", + " 60,\n", + " 78,\n", + " 22,\n", + " 41]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = list(random.sample(range(100),80))\n", + "sample_list_1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "56 1\n", + "67 1\n", + "71 1\n", + "83 1\n", + "7 1\n", + "59 1\n", + "39 1\n", + "51 1\n", + "99 1\n", + "3 1\n", + "13 1\n", + "57 1\n", + "35 1\n", + "42 1\n", + "43 1\n", + "38 1\n", + "63 1\n", + "6 1\n", + "44 1\n", + "48 1\n", + "37 1\n", + "74 1\n", + "27 1\n", + "46 1\n", + "76 1\n", + "5 1\n", + "85 1\n", + "40 1\n", + "82 1\n", + "80 1\n", + "9 1\n", + "4 1\n", + "66 1\n", + "86 1\n", + "0 1\n", + "77 1\n", + "84 1\n", + "58 1\n", + "8 1\n", + "90 1\n", + "29 1\n", + "2 1\n", + "34 1\n", + "33 1\n", + "52 1\n", + "15 1\n", + "89 1\n", + "92 1\n", + "79 1\n", + "73 1\n", + "21 1\n", + "69 1\n", + "95 1\n", + "28 1\n", + "53 1\n", + "49 1\n", + "65 1\n", + "45 1\n", + "75 1\n", + "68 1\n", + "64 1\n", + "47 1\n", + "72 1\n", + "19 1\n", + "17 1\n", + "94 1\n", + "96 1\n", + "10 1\n", + "31 1\n", + "23 1\n", + "97 1\n", + "14 1\n", + "81 1\n", + "50 1\n", + "62 1\n", + "93 1\n", + "60 1\n", + "78 1\n", + "22 1\n", + "41 1\n" + ] + } + ], + "source": [ + "# Count the number of times a number appears in sample_list\n", + "\n", + "for number in sample_list_1:\n", + " count = sample_list_1.count(number)\n", + " print(number, count)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create another list named `sample_list_2` with 80 random values.\n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* The values in the list don't have to be unique.\n", + "\n", + "*Hint: Use a FOR loop.*" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5,\n", + " 71,\n", + " 62,\n", + " 77,\n", + " 86,\n", + " 37,\n", + " 21,\n", + " 0,\n", + " 23,\n", + " 76,\n", + " 28,\n", + " 9,\n", + " 30,\n", + " 51,\n", + " 9,\n", + " 98,\n", + " 84,\n", + " 44,\n", + " 18,\n", + " 98,\n", + " 100,\n", + " 17,\n", + " 42,\n", + " 98,\n", + " 62,\n", + " 7,\n", + " 99,\n", + " 40,\n", + " 96,\n", + " 56,\n", + " 87,\n", + " 4,\n", + " 60,\n", + " 39,\n", + " 6,\n", + " 51,\n", + " 3,\n", + " 46,\n", + " 3,\n", + " 62,\n", + " 40,\n", + " 0,\n", + " 27,\n", + " 9,\n", + " 10,\n", + " 28,\n", + " 73,\n", + " 92,\n", + " 50,\n", + " 15,\n", + " 51,\n", + " 21,\n", + " 4,\n", + " 62,\n", + " 81,\n", + " 62,\n", + " 45,\n", + " 37,\n", + " 13,\n", + " 13,\n", + " 72,\n", + " 76,\n", + " 73,\n", + " 41,\n", + " 99,\n", + " 1,\n", + " 21,\n", + " 53,\n", + " 13,\n", + " 83,\n", + " 82,\n", + " 57,\n", + " 12,\n", + " 65,\n", + " 48,\n", + " 40,\n", + " 83,\n", + " 89,\n", + " 33,\n", + " 97]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80): \n", + " sample_list_2.append(random.randint(0,100))\n", + " \n", + "sample_list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n", + "71 1\n", + "62 5\n", + "77 1\n", + "86 1\n", + "37 2\n", + "21 3\n", + "0 2\n", + "23 1\n", + "76 2\n", + "28 2\n", + "9 3\n", + "30 1\n", + "51 3\n", + "9 3\n", + "98 3\n", + "84 1\n", + "44 1\n", + "18 1\n", + "98 3\n", + "100 1\n", + "17 1\n", + "42 1\n", + "98 3\n", + "62 5\n", + "7 1\n", + "99 2\n", + "40 3\n", + "96 1\n", + "56 1\n", + "87 1\n", + "4 2\n", + "60 1\n", + "39 1\n", + "6 1\n", + "51 3\n", + "3 2\n", + "46 1\n", + "3 2\n", + "62 5\n", + "40 3\n", + "0 2\n", + "27 1\n", + "9 3\n", + "10 1\n", + "28 2\n", + "73 2\n", + "92 1\n", + "50 1\n", + "15 1\n", + "51 3\n", + "21 3\n", + "4 2\n", + "62 5\n", + "81 1\n", + "62 5\n", + "45 1\n", + "37 2\n", + "13 3\n", + "13 3\n", + "72 1\n", + "76 2\n", + "73 2\n", + "41 1\n", + "99 2\n", + "1 1\n", + "21 3\n", + "53 1\n", + "13 3\n", + "83 2\n", + "82 1\n", + "57 1\n", + "12 1\n", + "65 1\n", + "48 1\n", + "40 3\n", + "83 2\n", + "89 1\n", + "33 1\n", + "97 1\n", + "80\n" + ] + } + ], + "source": [ + "for number in sample_list_2:\n", + " count = sample_list_2.count(number)\n", + " print(number, count)\n", + "print(len(sample_list_2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(sample_list_2))\n", + "print(len(set2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 8,\n", + " 14,\n", + " 19,\n", + " 22,\n", + " 29,\n", + " 31,\n", + " 34,\n", + " 35,\n", + " 38,\n", + " 43,\n", + " 47,\n", + " 49,\n", + " 52,\n", + " 58,\n", + " 59,\n", + " 63,\n", + " 64,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 74,\n", + " 75,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 85,\n", + " 90,\n", + " 93,\n", + " 94,\n", + " 95}" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set3 = set1.difference(set2)\n", + "set3" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 12, 18, 30, 87, 98, 100}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 21,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 37,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 48,\n", + " 50,\n", + " 51,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 60,\n", + " 62,\n", + " 65,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 76,\n", + " 77,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 86,\n", + " 89,\n", + " 92,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "set5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the relationship among the following values:\n", + "\n", + "* len(set1)\n", + "* len(set2)\n", + "* len(set3)\n", + "* len(set4)\n", + "* len(set5)\n", + "\n", + "Use a math formular to represent that relationship. Test your formular with Python code." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n", + "32\n", + "7\n", + "48\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(len(set1))\n", + "print(len(set2))\n", + "print(len(set3))\n", + "print(len(set4))\n", + "print(len(set5))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(set4) < len(set3) < len(set5) < len(set2) < len(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set6 = set()\n", + "type(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class set in module builtins:\n", + "\n", + "class set(object)\n", + " | set() -> new empty set object\n", + " | set(iterable) -> new set object\n", + " | \n", + " | Build an unordered collection of unique elements.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __and__(self, value, /)\n", + " | Return self&value.\n", + " | \n", + " | __contains__(...)\n", + " | x.__contains__(y) <==> y in x.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iand__(self, value, /)\n", + " | Return self&=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __ior__(self, value, /)\n", + " | Return self|=value.\n", + " | \n", + " | __isub__(self, value, /)\n", + " | Return self-=value.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __ixor__(self, value, /)\n", + " | Return self^=value.\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self size of S in memory, in bytes\n", + " | \n", + " | __sub__(self, value, /)\n", + " | Return self-value.\n", + " | \n", + " | __xor__(self, value, /)\n", + " | Return self^value.\n", + " | \n", + " | add(...)\n", + " | Add an element to a set.\n", + " | \n", + " | This has no effect if the element is already present.\n", + " | \n", + " | clear(...)\n", + " | Remove all elements from this set.\n", + " | \n", + " | copy(...)\n", + " | Return a shallow copy of a set.\n", + " | \n", + " | difference(...)\n", + " | Return the difference of two or more sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in this set but not the others.)\n", + " | \n", + " | difference_update(...)\n", + " | Remove all elements of another set from this set.\n", + " | \n", + " | discard(...)\n", + " | Remove an element from a set if it is a member.\n", + " | \n", + " | If the element is not a member, do nothing.\n", + " | \n", + " | intersection(...)\n", + " | Return the intersection of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in both sets.)\n", + " | \n", + " | intersection_update(...)\n", + " | Update a set with the intersection of itself and another.\n", + " | \n", + " | isdisjoint(...)\n", + " | Return True if two sets have a null intersection.\n", + " | \n", + " | issubset(...)\n", + " | Report whether another set contains this set.\n", + " | \n", + " | issuperset(...)\n", + " | Report whether this set contains another set.\n", + " | \n", + " | pop(...)\n", + " | Remove and return an arbitrary set element.\n", + " | Raises KeyError if the set is empty.\n", + " | \n", + " | remove(...)\n", + " | Remove an element from a set; it must be a member.\n", + " | \n", + " | If the element is not a member, raise a KeyError.\n", + " | \n", + " | symmetric_difference(...)\n", + " | Return the symmetric difference of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in exactly one of the sets.)\n", + " | \n", + " | symmetric_difference_update(...)\n", + " | Update a set with the symmetric difference of itself and another.\n", + " | \n", + " | union(...)\n", + " | Return the union of sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in either set.)\n", + " | \n", + " | update(...)\n", + " | Update a set with the union of itself and others.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | __class_getitem__(...) from builtins.type\n", + " | See PEP 585\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "help(set)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 17, 19, 21, 22, 23, 27, 28, 29, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 89, 90, 92, 93, 94, 95, 96, 97, 99}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set5)\n", + "set6.update(set3)\n", + "print(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1 == set6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set2.issubset(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.issubset(set1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "\n", + "#### Check if the aggregated values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set3.union(set4,set5))\n", + "print(set1.union(set2))" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.union(set4,set5) == set1.union(set2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the `pop` method, remove the first element from `set1`." + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 19,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 31,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 51,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 59,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 89,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "set1.pop()\n", + "set1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\n", + "```\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 50,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 80,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97}" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for number in list_to_remove:\n", + " if number in set1:\n", + " set1.remove(number)\n", + " \n", + "set1" + ] + }, + { + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb new file mode 100644 index 0000000..0e8c03c --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb @@ -0,0 +1,392 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 3: Dictionaries\n", + "\n", + "In this challenge you will practice how to manipulate Python dictionaries. Before starting on this challenge, you are encouraged to review W3School's [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp).\n", + "\n", + "First thing you will practice is how to sort the keys in a dictionary. Unlike the list object, Python dictionary does not have a built-in *sort* method. You'll need to use FOR loops to to sort dictionaries either by key or by value.\n", + "\n", + "The dictionary below is a summary of the word frequency of Ed Sheeran's song *Shape of You*. Each key is a word in the lyrics and the value is the number of times that word appears in the lyrics." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort the keys of `word_freq` ascendingly.\n", + "\n", + "Please create a new dictionary called `word_freq2` based on `word_freq` with the keys sorted ascedingly.\n", + "\n", + "There are several ways to achieve that goal but many of the ways are beyond what we have covered so far in the course. There is one way that we'll describe employing what you have learned. Please feel free to use this way or any other way you want.\n", + "\n", + "1. First extract the keys of `word_freq` and convert it to a list called `keys`.\n", + "\n", + "1. Sort the `keys` list.\n", + "\n", + "1. Create an empty dictionary `word_freq2`.\n", + "\n", + "1. Use a FOR loop to iterate each value in `keys`. For each key iterated, find the corresponding value in `word_freq` and insert the key-value pair to `word_freq2`.\n", + "\n", + "Print out `word_freq2` to examine its keys and values. Your output should be:\n", + "\n", + "```python\n", + "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a',\n", + " 'about',\n", + " 'all',\n", + " 'although',\n", + " 'and',\n", + " 'are',\n", + " 'at',\n", + " 'baby',\n", + " 'backseat',\n", + " 'bag',\n", + " 'bar',\n", + " 'be',\n", + " 'bedsheets',\n", + " 'begin',\n", + " 'best',\n", + " 'body',\n", + " 'boy',\n", + " 'brand',\n", + " 'can',\n", + " 'chance',\n", + " 'club',\n", + " 'come',\n", + " 'conversation',\n", + " 'crazy',\n", + " 'dance',\n", + " 'date',\n", + " 'day',\n", + " 'discovering',\n", + " 'do',\n", + " 'doing',\n", + " \"don't\",\n", + " 'drinking',\n", + " 'driver',\n", + " 'eat',\n", + " 'every',\n", + " 'falling',\n", + " 'family',\n", + " 'fast',\n", + " 'fill',\n", + " 'find',\n", + " 'first',\n", + " 'follow',\n", + " 'for',\n", + " 'friends',\n", + " 'get',\n", + " 'girl',\n", + " 'give',\n", + " 'go',\n", + " 'going',\n", + " 'grab',\n", + " 'hand',\n", + " 'handmade',\n", + " 'heart',\n", + " 'hours',\n", + " 'how',\n", + " 'i',\n", + " \"i'll\",\n", + " \"i'm\",\n", + " 'in',\n", + " 'is',\n", + " \"isn't\",\n", + " 'it',\n", + " 'jukebox',\n", + " 'just',\n", + " 'kiss',\n", + " 'know',\n", + " 'last',\n", + " 'lead',\n", + " 'leave',\n", + " 'let',\n", + " \"let's\",\n", + " 'like',\n", + " 'love',\n", + " 'lover',\n", + " 'magnet',\n", + " 'make',\n", + " 'man',\n", + " 'may',\n", + " 'me',\n", + " 'mind',\n", + " 'much',\n", + " 'my',\n", + " 'new',\n", + " 'night',\n", + " 'not',\n", + " 'now',\n", + " 'of',\n", + " 'okay',\n", + " 'on',\n", + " 'one',\n", + " 'our',\n", + " 'out',\n", + " 'over',\n", + " 'place',\n", + " 'plate',\n", + " 'play',\n", + " 'pull',\n", + " 'push',\n", + " 'put',\n", + " 'radio',\n", + " 'room',\n", + " 'say',\n", + " 'shape',\n", + " 'shots',\n", + " 'singing',\n", + " 'slow',\n", + " 'smell',\n", + " 'so',\n", + " 'somebody',\n", + " 'something',\n", + " 'sour',\n", + " 'start',\n", + " 'stop',\n", + " 'story',\n", + " 'sweet',\n", + " 'table',\n", + " 'take',\n", + " 'talk',\n", + " 'taxi',\n", + " 'tell',\n", + " 'that',\n", + " 'the',\n", + " 'then',\n", + " 'thrifty',\n", + " 'to',\n", + " 'too',\n", + " 'trust',\n", + " 'up',\n", + " 'van',\n", + " 'waist',\n", + " 'want',\n", + " 'was',\n", + " 'we',\n", + " \"we're\",\n", + " 'week',\n", + " 'were',\n", + " 'where',\n", + " 'with',\n", + " 'you',\n", + " 'your']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "keys = list(word_freq.keys())\n", + "keys.sort()\n", + "keys" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "word_freq2 = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "def dict_iteration(key):\n", + " value = keys[key]\n", + " return print(keys[key],value)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'a' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [36]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m dict_iteration(\u001b[43ma\u001b[49m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" + ] + } + ], + "source": [ + "dict_iteration(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort the values of `word_freq` ascendingly.\n", + "\n", + "Sorting the values of a dictionary is more tricky than sorting the keys because a dictionary's values are not unique. Therefore you cannot use the same way you sorted dict keys to sort dict values.\n", + "\n", + "The way to sort a dict by value is to utilize the `sorted` and `operator.itemgetter` functions. The following code snippet is provided to you to try. It will give you a list of tuples in which each tuple contains the key and value of a dict item. And the list is sorted based on the dict value ([reference](http://thomas-cokelaer.info/blog/2017/12/how-to-sort-a-dictionary-by-values-in-python/)\n", + ").\n", + "\n", + "```python\n", + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)\n", + "```\n", + "\n", + "Therefore, the steps to sort `word_freq` by value are:\n", + "\n", + "* Using `sorted` and `operator.itemgetter`, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "\n", + "* Create an empty dictionary named `word_freq2`.\n", + "\n", + "* Iterate the list of tuples. Insert each key-value pair into `word_freq2` as an object.\n", + "\n", + "Print `word_freq2` to confirm your dictionary has its values sorted. Your output should be:\n", + "\n", + "```python\n", + "{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `word_freq` into Pandas dataframes\n", + "\n", + "In your future work, you may need to convert Python dictionaries to Pandas dataframes. So let's practice this by converting `word_freq`.\n", + "\n", + "**First, import the `pandas` library.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Then, use the `pd.DataFrame()` constructor to convert `word_freq` into a dataframe.**\n", + "\n", + "Here's a [reference](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) to show you how to accomplish this. Also name the two columns of the dataframe as `word` and `freq`.\n", + "\n", + "Assign the converted value to a variable called `df`. The first few rows of `df` should look like this:\n", + "\n", + "![df](df.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With the Pandas DataFrame, you can sort the values easily with the built-in method `sort_values` ([reference](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html)).\n", + "\n", + "#### Sort `df` ascendingly based on column `word`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort `df` ascendingly based on column `freq`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + } + ], + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.~challenge-1.ipynb b/your-code/.~challenge-1.ipynb new file mode 100644 index 0000000..2e59d77 --- /dev/null +++ b/your-code/.~challenge-1.ipynb @@ -0,0 +1,227 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 1: Tuples\n", + "\n", + "#### Do you know you can create tuples with only one element?\n", + "\n", + "**In the cell below, define a variable `tup` with a single element `\"I\"`.**\n", + "\n", + "*Hint: you need to add a comma (`,`) after the single element.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Print the type of `tup`. \n", + "\n", + "Make sure its type is correct (i.e. *tuple* instead of *str*)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now try to append the following elements to `tup`. \n", + "\n", + "Are you able to do it? Explain.\n", + "\n", + "```\n", + "\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k',\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n", + "\n", + "# Your explanation here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### How about re-assign a new value to an existing tuple?\n", + "\n", + "Re-assign the following elements to `tup`. Are you able to do it? Explain.\n", + "\n", + "```\n", + "\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\"\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n", + "\n", + "# Your explanation here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Split `tup` into `tup1` and `tup2` with 4 elements in each. \n", + "\n", + "`tup1` should be `(\"I\", \"r\", \"o\", \"n\")` and `tup2` should be `(\"h\", \"a\", \"c\", \"k\")`.\n", + "\n", + "*Hint: use positive index numbers for `tup1` assignment and use negative index numbers for `tup2` assignment. Positive index numbers count from the beginning whereas negative index numbers count from the end of the sequence.*\n", + "\n", + "Also print `tup1` and `tup2`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `tup1` and `tup2` into `tup3` using the `+` operator.\n", + "\n", + "Then print `tup3` and check if `tup3` equals to `tup`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Count the number of elements in `tup1` and `tup2`. Then add the two counts together and check if the sum is the same as the number of elements in `tup3`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the index number of `\"h\"` in `tup3`?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now, use a FOR loop to check whether each letter in the following list is present in `tup3`:\n", + "\n", + "```\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "```\n", + "\n", + "For each letter you check, print `True` if it is present in `tup3` otherwise print `False`.\n", + "\n", + "*Hint: you only need to loop `letters`. You don't need to loop `tup3` because there is a Python operator `in` you can use. See [reference](https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element-in-python).*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### How many times does each letter in `letters` appear in `tup3`?\n", + "\n", + "Print out the number of occurrence of each letter." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.~challenge-2.ipynb b/your-code/.~challenge-2.ipynb new file mode 100644 index 0000000..41d6911 --- /dev/null +++ b/your-code/.~challenge-2.ipynb @@ -0,0 +1,315 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2: Sets\n", + "\n", + "There are a lot to learn about Python Sets and the information presented in the lesson is limited due to its length. To learn Python Sets in depth you are strongly encouraged to review the W3Schools tutorial on [Python Sets Examples and Methods](https://www.w3schools.com/python/python_sets.asp) before you work on this lab. Some difficult questions in this lab have their solutions in the W3Schools tutorial.\n", + "\n", + "#### First, import the Python `random` libary" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In the cell below, create a list named `sample_list_1` with 80 random values. \n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* Each value in the list is unique.\n", + "\n", + "Print `sample_list_1` to review its values\n", + "\n", + "*Hint: use `random.sample` ([reference](https://docs.python.org/3/library/random.html#random.sample)).*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_1` to a set called `set1`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create another list named `sample_list_2` with 80 random values.\n", + "\n", + "Requirements:\n", + "\n", + "* Each value is an integer falling between 0 and 100.\n", + "* The values in the list don't have to be unique.\n", + "\n", + "*Hint: Use a FOR loop.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `sample_list_2` to a set called `set2`. Print the length of the set. Is its length still 80?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set1` but not in `set2`. Assign the elements to a new set named `set3`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Identify the elements present in `set2` but not in `set1`. Assign the elements to a new set named `set4`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now Identify the elements shared between `set1` and `set2`. Assign the elements to a new set named `set5`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### What is the relationship among the following values:\n", + "\n", + "* len(set1)\n", + "* len(set2)\n", + "* len(set3)\n", + "* len(set4)\n", + "* len(set5)\n", + "\n", + "Use a math formular to represent that relationship. Test your formular with Python code." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` contains `set2` using the Python Set `issubset` method. Then check if `set1` contains `set3`.*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the Python Set `union` method, aggregate `set3`, `set4`, and `set5`. Then aggregate `set1` and `set2`. \n", + "\n", + "#### Check if the aggregated values are equal." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the `pop` method, remove the first element from `set1`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Remove every element in the following list from `set1` if they are present in the set. Print the remaining elements.\n", + "\n", + "```\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.~challenge-3.ipynb b/your-code/.~challenge-3.ipynb new file mode 100644 index 0000000..7ab8ea5 --- /dev/null +++ b/your-code/.~challenge-3.ipynb @@ -0,0 +1,198 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 3: Dictionaries\n", + "\n", + "In this challenge you will practice how to manipulate Python dictionaries. Before starting on this challenge, you are encouraged to review W3School's [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp).\n", + "\n", + "First thing you will practice is how to sort the keys in a dictionary. Unlike the list object, Python dictionary does not have a built-in *sort* method. You'll need to use FOR loops to to sort dictionaries either by key or by value.\n", + "\n", + "The dictionary below is a summary of the word frequency of Ed Sheeran's song *Shape of You*. Each key is a word in the lyrics and the value is the number of times that word appears in the lyrics." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort the keys of `word_freq` ascendingly.\n", + "\n", + "Please create a new dictionary called `word_freq2` based on `word_freq` with the keys sorted ascedingly.\n", + "\n", + "There are several ways to achieve that goal but many of the ways are beyond what we have covered so far in the course. There is one way that we'll describe employing what you have learned. Please feel free to use this way or any other way you want.\n", + "\n", + "1. First extract the keys of `word_freq` and convert it to a list called `keys`.\n", + "\n", + "1. Sort the `keys` list.\n", + "\n", + "1. Create an empty dictionary `word_freq2`.\n", + "\n", + "1. Use a FOR loop to iterate each value in `keys`. For each key iterated, find the corresponding value in `word_freq` and insert the key-value pair to `word_freq2`.\n", + "\n", + "Print out `word_freq2` to examine its keys and values. Your output should be:\n", + "\n", + "```python\n", + "{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort the values of `word_freq` ascendingly.\n", + "\n", + "Sorting the values of a dictionary is more tricky than sorting the keys because a dictionary's values are not unique. Therefore you cannot use the same way you sorted dict keys to sort dict values.\n", + "\n", + "The way to sort a dict by value is to utilize the `sorted` and `operator.itemgetter` functions. The following code snippet is provided to you to try. It will give you a list of tuples in which each tuple contains the key and value of a dict item. And the list is sorted based on the dict value ([reference](http://thomas-cokelaer.info/blog/2017/12/how-to-sort-a-dictionary-by-values-in-python/)\n", + ").\n", + "\n", + "```python\n", + "import operator\n", + "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", + "print(sorted_tups)\n", + "```\n", + "\n", + "Therefore, the steps to sort `word_freq` by value are:\n", + "\n", + "* Using `sorted` and `operator.itemgetter`, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n", + "\n", + "* Create an empty dictionary named `word_freq2`.\n", + "\n", + "* Iterate the list of tuples. Insert each key-value pair into `word_freq2` as an object.\n", + "\n", + "Print `word_freq2` to confirm your dictionary has its values sorted. Your output should be:\n", + "\n", + "```python\n", + "{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Convert `word_freq` into Pandas dataframes\n", + "\n", + "In your future work, you may need to convert Python dictionaries to Pandas dataframes. So let's practice this by converting `word_freq`.\n", + "\n", + "**First, import the `pandas` library.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Then, use the `pd.DataFrame()` constructor to convert `word_freq` into a dataframe.**\n", + "\n", + "Here's a [reference](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) to show you how to accomplish this. Also name the two columns of the dataframe as `word` and `freq`.\n", + "\n", + "Assign the converted value to a variable called `df`. The first few rows of `df` should look like this:\n", + "\n", + "![df](df.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With the Pandas DataFrame, you can sort the values easily with the built-in method `sort_values` ([reference](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html)).\n", + "\n", + "#### Sort `df` ascendingly based on column `word`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort `df` ascendingly based on column `freq`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..abdce71 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = (\"I\",)" ] }, { @@ -33,11 +34,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(type(tup))" ] }, { @@ -60,8 +70,9 @@ "outputs": [], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup.add(\"r\",\"o\",\"n\",\"h\",\"a\",\"c\",\"k\")\n", + "# Your explanation here\n", + "# Unable to add elements to a tuple due to it's unmutable nature" ] }, { @@ -77,6 +88,112 @@ "```" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class tuple in module builtins:\n", + "\n", + "class tuple(object)\n", + " | tuple(iterable=(), /)\n", + " | \n", + " | Built-in immutable sequence.\n", + " | \n", + " | If no argument is given, the constructor returns an empty tuple.\n", + " | If iterable is specified the tuple is initialized from iterable's items.\n", + " | \n", + " | If the argument is a tuple, the return value is the same object.\n", + " | \n", + " | Built-in subclasses:\n", + " | asyncgen_hooks\n", + " | UnraisableHookArgs\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(self, value, /)\n", + " | Return self+value.\n", + " | \n", + " | __contains__(self, key, /)\n", + " | Return key in self.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __getitem__(self, key, /)\n", + " | Return self[key].\n", + " | \n", + " | __getnewargs__(self, /)\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __hash__(self, /)\n", + " | Return hash(self).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self \n", + "('I', 'r', 'o', 'n') ('h', 'a', 'c', 'k')\n" + ] + } + ], + "source": [ + "# Your code here\n", + "# Con slicing\n", + "tup = (\"I\",\"r\",\"o\",\"n\",\"h\",\"a\",\"c\",\"k\")\n", + "tup1 = tuple(tup[0:4])\n", + "tup2 = tuple(tup[-4:])\n", + "\n", + "print(type(tup1),type(tup2))\n", + "print(tup1,tup2)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "# Con ciclo for\n", + "tup1=[]\n", + "tup2=[]\n", + "for i in tup:\n", + " if tup.index(i) < 4:\n", + " tup1.append(i)\n", + " else:\n", + " tup2.append(i)\n", + "\n", + "tup1 = tuple(tup1)\n", + "tup2 = tuple(tup2)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(('I', 'r', 'o', 'n'), ('h', 'a', 'c', 'k'))" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup1, tup2" ] }, { @@ -121,11 +296,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3 = tup1 + tup2\n", + "print(tup3)\n", + "# Cannot add tuples by using the + operator" ] }, { @@ -137,11 +323,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "8\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "count1 = len(tup1)\n", + "count2 = len(tup2)\n", + "print(count1 + count2)\n", + "print(len(tup3))" ] }, { @@ -153,11 +352,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup3.index(\"h\")" ] }, { @@ -177,11 +388,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a : True\n", + "Letter b : False\n", + "Letter c : True\n", + "Letter d : False\n", + "Letter e : False\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(\"Letter\", letter, \": True\")\n", + " else:\n", + " print(\"Letter\", letter, \": False\")" ] }, { @@ -195,17 +425,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Letter a appears 1 time(s) in tup3\n", + "Letter b appears 0 time(s) in tup3\n", + "Letter c appears 1 time(s) in tup3\n", + "Letter d appears 0 time(s) in tup3\n", + "Letter e appears 0 time(s) in tup3\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "for letter in letters:\n", + " if letter in tup3:\n", + " print(\"Letter\", letter, \"appears\", tup3.count(letter), \"time(s) in tup3\")\n", + " else:\n", + " print(\"Letter\", letter, \"appears\", tup3.count(letter), \"time(s) in tup3\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -219,7 +473,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 41d6911..45e5df4 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -38,11 +38,203 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[56,\n", + " 67,\n", + " 71,\n", + " 83,\n", + " 7,\n", + " 59,\n", + " 39,\n", + " 51,\n", + " 99,\n", + " 3,\n", + " 13,\n", + " 57,\n", + " 35,\n", + " 42,\n", + " 43,\n", + " 38,\n", + " 63,\n", + " 6,\n", + " 44,\n", + " 48,\n", + " 37,\n", + " 74,\n", + " 27,\n", + " 46,\n", + " 76,\n", + " 5,\n", + " 85,\n", + " 40,\n", + " 82,\n", + " 80,\n", + " 9,\n", + " 4,\n", + " 66,\n", + " 86,\n", + " 0,\n", + " 77,\n", + " 84,\n", + " 58,\n", + " 8,\n", + " 90,\n", + " 29,\n", + " 2,\n", + " 34,\n", + " 33,\n", + " 52,\n", + " 15,\n", + " 89,\n", + " 92,\n", + " 79,\n", + " 73,\n", + " 21,\n", + " 69,\n", + " 95,\n", + " 28,\n", + " 53,\n", + " 49,\n", + " 65,\n", + " 45,\n", + " 75,\n", + " 68,\n", + " 64,\n", + " 47,\n", + " 72,\n", + " 19,\n", + " 17,\n", + " 94,\n", + " 96,\n", + " 10,\n", + " 31,\n", + " 23,\n", + " 97,\n", + " 14,\n", + " 81,\n", + " 50,\n", + " 62,\n", + " 93,\n", + " 60,\n", + " 78,\n", + " 22,\n", + " 41]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "sample_list_1 = list(random.sample(range(100),80))\n", + "sample_list_1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "56 1\n", + "67 1\n", + "71 1\n", + "83 1\n", + "7 1\n", + "59 1\n", + "39 1\n", + "51 1\n", + "99 1\n", + "3 1\n", + "13 1\n", + "57 1\n", + "35 1\n", + "42 1\n", + "43 1\n", + "38 1\n", + "63 1\n", + "6 1\n", + "44 1\n", + "48 1\n", + "37 1\n", + "74 1\n", + "27 1\n", + "46 1\n", + "76 1\n", + "5 1\n", + "85 1\n", + "40 1\n", + "82 1\n", + "80 1\n", + "9 1\n", + "4 1\n", + "66 1\n", + "86 1\n", + "0 1\n", + "77 1\n", + "84 1\n", + "58 1\n", + "8 1\n", + "90 1\n", + "29 1\n", + "2 1\n", + "34 1\n", + "33 1\n", + "52 1\n", + "15 1\n", + "89 1\n", + "92 1\n", + "79 1\n", + "73 1\n", + "21 1\n", + "69 1\n", + "95 1\n", + "28 1\n", + "53 1\n", + "49 1\n", + "65 1\n", + "45 1\n", + "75 1\n", + "68 1\n", + "64 1\n", + "47 1\n", + "72 1\n", + "19 1\n", + "17 1\n", + "94 1\n", + "96 1\n", + "10 1\n", + "31 1\n", + "23 1\n", + "97 1\n", + "14 1\n", + "81 1\n", + "50 1\n", + "62 1\n", + "93 1\n", + "60 1\n", + "78 1\n", + "22 1\n", + "41 1\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Count the number of times a number appears in sample_list\n", + "\n", + "for number in sample_list_1:\n", + " count = sample_list_1.count(number)\n", + " print(number, count)" ] }, { @@ -54,11 +246,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "80" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 = set(sample_list_1)\n", + "len(set1)" ] }, { @@ -77,11 +282,207 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[5,\n", + " 71,\n", + " 62,\n", + " 77,\n", + " 86,\n", + " 37,\n", + " 21,\n", + " 0,\n", + " 23,\n", + " 76,\n", + " 28,\n", + " 9,\n", + " 30,\n", + " 51,\n", + " 9,\n", + " 98,\n", + " 84,\n", + " 44,\n", + " 18,\n", + " 98,\n", + " 100,\n", + " 17,\n", + " 42,\n", + " 98,\n", + " 62,\n", + " 7,\n", + " 99,\n", + " 40,\n", + " 96,\n", + " 56,\n", + " 87,\n", + " 4,\n", + " 60,\n", + " 39,\n", + " 6,\n", + " 51,\n", + " 3,\n", + " 46,\n", + " 3,\n", + " 62,\n", + " 40,\n", + " 0,\n", + " 27,\n", + " 9,\n", + " 10,\n", + " 28,\n", + " 73,\n", + " 92,\n", + " 50,\n", + " 15,\n", + " 51,\n", + " 21,\n", + " 4,\n", + " 62,\n", + " 81,\n", + " 62,\n", + " 45,\n", + " 37,\n", + " 13,\n", + " 13,\n", + " 72,\n", + " 76,\n", + " 73,\n", + " 41,\n", + " 99,\n", + " 1,\n", + " 21,\n", + " 53,\n", + " 13,\n", + " 83,\n", + " 82,\n", + " 57,\n", + " 12,\n", + " 65,\n", + " 48,\n", + " 40,\n", + " 83,\n", + " 89,\n", + " 33,\n", + " 97]" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "sample_list_2 = []\n", + "\n", + "for i in range(80): \n", + " sample_list_2.append(random.randint(0,100))\n", + " \n", + "sample_list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 1\n", + "71 1\n", + "62 5\n", + "77 1\n", + "86 1\n", + "37 2\n", + "21 3\n", + "0 2\n", + "23 1\n", + "76 2\n", + "28 2\n", + "9 3\n", + "30 1\n", + "51 3\n", + "9 3\n", + "98 3\n", + "84 1\n", + "44 1\n", + "18 1\n", + "98 3\n", + "100 1\n", + "17 1\n", + "42 1\n", + "98 3\n", + "62 5\n", + "7 1\n", + "99 2\n", + "40 3\n", + "96 1\n", + "56 1\n", + "87 1\n", + "4 2\n", + "60 1\n", + "39 1\n", + "6 1\n", + "51 3\n", + "3 2\n", + "46 1\n", + "3 2\n", + "62 5\n", + "40 3\n", + "0 2\n", + "27 1\n", + "9 3\n", + "10 1\n", + "28 2\n", + "73 2\n", + "92 1\n", + "50 1\n", + "15 1\n", + "51 3\n", + "21 3\n", + "4 2\n", + "62 5\n", + "81 1\n", + "62 5\n", + "45 1\n", + "37 2\n", + "13 3\n", + "13 3\n", + "72 1\n", + "76 2\n", + "73 2\n", + "41 1\n", + "99 2\n", + "1 1\n", + "21 3\n", + "53 1\n", + "13 3\n", + "83 2\n", + "82 1\n", + "57 1\n", + "12 1\n", + "65 1\n", + "48 1\n", + "40 3\n", + "83 2\n", + "89 1\n", + "33 1\n", + "97 1\n", + "80\n" + ] + } + ], + "source": [ + "for number in sample_list_2:\n", + " count = sample_list_2.count(number)\n", + " print(number, count)\n", + "print(len(sample_list_2))" ] }, { @@ -93,11 +494,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2 = set(sample_list_2)\n", + "print(len(sample_list_2))\n", + "print(len(set2))" ] }, { @@ -109,11 +522,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 8,\n", + " 14,\n", + " 19,\n", + " 22,\n", + " 29,\n", + " 31,\n", + " 34,\n", + " 35,\n", + " 38,\n", + " 43,\n", + " 47,\n", + " 49,\n", + " 52,\n", + " 58,\n", + " 59,\n", + " 63,\n", + " 64,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 74,\n", + " 75,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 85,\n", + " 90,\n", + " 93,\n", + " 94,\n", + " 95}" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set3 = set1.difference(set2)\n", + "set3" ] }, { @@ -125,11 +582,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 12, 18, 30, 87, 98, 100}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set4 = set2.difference(set1)\n", + "set4" ] }, { @@ -141,11 +611,71 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{0,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 21,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 37,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 48,\n", + " 50,\n", + " 51,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 60,\n", + " 62,\n", + " 65,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 76,\n", + " 77,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 86,\n", + " 89,\n", + " 92,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set5 = set1.intersection(set2)\n", + "set5" ] }, { @@ -165,11 +695,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "80\n", + "55\n", + "32\n", + "7\n", + "48\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(len(set1))\n", + "print(len(set2))\n", + "print(len(set3))\n", + "print(len(set4))\n", + "print(len(set5))" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(set4) < len(set3) < len(set5) < len(set2) < len(set1)" ] }, { @@ -181,11 +748,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set()\n", + "type(set6)" ] }, { @@ -197,11 +777,209 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class set in module builtins:\n", + "\n", + "class set(object)\n", + " | set() -> new empty set object\n", + " | set(iterable) -> new set object\n", + " | \n", + " | Build an unordered collection of unique elements.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __and__(self, value, /)\n", + " | Return self&value.\n", + " | \n", + " | __contains__(...)\n", + " | x.__contains__(y) <==> y in x.\n", + " | \n", + " | __eq__(self, value, /)\n", + " | Return self==value.\n", + " | \n", + " | __ge__(self, value, /)\n", + " | Return self>=value.\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __gt__(self, value, /)\n", + " | Return self>value.\n", + " | \n", + " | __iand__(self, value, /)\n", + " | Return self&=value.\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | __ior__(self, value, /)\n", + " | Return self|=value.\n", + " | \n", + " | __isub__(self, value, /)\n", + " | Return self-=value.\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __ixor__(self, value, /)\n", + " | Return self^=value.\n", + " | \n", + " | __le__(self, value, /)\n", + " | Return self<=value.\n", + " | \n", + " | __len__(self, /)\n", + " | Return len(self).\n", + " | \n", + " | __lt__(self, value, /)\n", + " | Return self size of S in memory, in bytes\n", + " | \n", + " | __sub__(self, value, /)\n", + " | Return self-value.\n", + " | \n", + " | __xor__(self, value, /)\n", + " | Return self^value.\n", + " | \n", + " | add(...)\n", + " | Add an element to a set.\n", + " | \n", + " | This has no effect if the element is already present.\n", + " | \n", + " | clear(...)\n", + " | Remove all elements from this set.\n", + " | \n", + " | copy(...)\n", + " | Return a shallow copy of a set.\n", + " | \n", + " | difference(...)\n", + " | Return the difference of two or more sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in this set but not the others.)\n", + " | \n", + " | difference_update(...)\n", + " | Remove all elements of another set from this set.\n", + " | \n", + " | discard(...)\n", + " | Remove an element from a set if it is a member.\n", + " | \n", + " | If the element is not a member, do nothing.\n", + " | \n", + " | intersection(...)\n", + " | Return the intersection of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in both sets.)\n", + " | \n", + " | intersection_update(...)\n", + " | Update a set with the intersection of itself and another.\n", + " | \n", + " | isdisjoint(...)\n", + " | Return True if two sets have a null intersection.\n", + " | \n", + " | issubset(...)\n", + " | Report whether another set contains this set.\n", + " | \n", + " | issuperset(...)\n", + " | Report whether this set contains another set.\n", + " | \n", + " | pop(...)\n", + " | Remove and return an arbitrary set element.\n", + " | Raises KeyError if the set is empty.\n", + " | \n", + " | remove(...)\n", + " | Remove an element from a set; it must be a member.\n", + " | \n", + " | If the element is not a member, raise a KeyError.\n", + " | \n", + " | symmetric_difference(...)\n", + " | Return the symmetric difference of two sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in exactly one of the sets.)\n", + " | \n", + " | symmetric_difference_update(...)\n", + " | Update a set with the symmetric difference of itself and another.\n", + " | \n", + " | union(...)\n", + " | Return the union of sets as a new set.\n", + " | \n", + " | (i.e. all elements that are in either set.)\n", + " | \n", + " | update(...)\n", + " | Update a set with the union of itself and others.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | __class_getitem__(...) from builtins.type\n", + " | See PEP 585\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data and other attributes defined here:\n", + " | \n", + " | __hash__ = None\n", + "\n" + ] + } + ], + "source": [ + "help(set)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 17, 19, 21, 22, 23, 27, 28, 29, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 89, 90, 92, 93, 94, 95, 96, 97, 99}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6.update(set5)\n", + "set6.update(set3)\n", + "print(set6)" ] }, { @@ -213,11 +991,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1 == set6" ] }, { @@ -229,11 +1019,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2.issubset(set1)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set3.issubset(set1)" ] }, { @@ -247,11 +1069,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n", + "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21, 22, 23, 27, 28, 29, 30, 31, 33, 34, 35, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 56, 57, 58, 59, 60, 62, 63, 64, 65, 66, 67, 68, 69, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 92, 93, 94, 95, 96, 97, 98, 99, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(set3.union(set4,set5))\n", + "print(set1.union(set2))" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "set3.union(set4,set5) == set1.union(set2)" ] }, { @@ -263,11 +1116,102 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 19,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 31,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50,\n", + " 51,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 59,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 69,\n", + " 71,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 79,\n", + " 80,\n", + " 81,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 89,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97,\n", + " 99}" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set1.pop()\n", + "set1" ] }, { @@ -283,17 +1227,104 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 10,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 17,\n", + " 22,\n", + " 23,\n", + " 27,\n", + " 28,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 37,\n", + " 38,\n", + " 40,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 50,\n", + " 52,\n", + " 53,\n", + " 56,\n", + " 57,\n", + " 58,\n", + " 60,\n", + " 62,\n", + " 63,\n", + " 64,\n", + " 65,\n", + " 66,\n", + " 67,\n", + " 68,\n", + " 72,\n", + " 73,\n", + " 74,\n", + " 75,\n", + " 76,\n", + " 77,\n", + " 78,\n", + " 80,\n", + " 82,\n", + " 83,\n", + " 84,\n", + " 85,\n", + " 86,\n", + " 90,\n", + " 92,\n", + " 93,\n", + " 94,\n", + " 95,\n", + " 96,\n", + " 97}" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "list_to_remove = [1, 9, 11, 19, 21, 29, 31, 39, 41, 49, 51, 59, 61, 69, 71, 79, 81, 89, 91, 99]\n", + "\n", + "for number in list_to_remove:\n", + " if number in set1:\n", + " set1.remove(number)\n", + " \n", + "set1" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -307,7 +1338,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..0e8c03c 100644 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -49,11 +49,205 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a',\n", + " 'about',\n", + " 'all',\n", + " 'although',\n", + " 'and',\n", + " 'are',\n", + " 'at',\n", + " 'baby',\n", + " 'backseat',\n", + " 'bag',\n", + " 'bar',\n", + " 'be',\n", + " 'bedsheets',\n", + " 'begin',\n", + " 'best',\n", + " 'body',\n", + " 'boy',\n", + " 'brand',\n", + " 'can',\n", + " 'chance',\n", + " 'club',\n", + " 'come',\n", + " 'conversation',\n", + " 'crazy',\n", + " 'dance',\n", + " 'date',\n", + " 'day',\n", + " 'discovering',\n", + " 'do',\n", + " 'doing',\n", + " \"don't\",\n", + " 'drinking',\n", + " 'driver',\n", + " 'eat',\n", + " 'every',\n", + " 'falling',\n", + " 'family',\n", + " 'fast',\n", + " 'fill',\n", + " 'find',\n", + " 'first',\n", + " 'follow',\n", + " 'for',\n", + " 'friends',\n", + " 'get',\n", + " 'girl',\n", + " 'give',\n", + " 'go',\n", + " 'going',\n", + " 'grab',\n", + " 'hand',\n", + " 'handmade',\n", + " 'heart',\n", + " 'hours',\n", + " 'how',\n", + " 'i',\n", + " \"i'll\",\n", + " \"i'm\",\n", + " 'in',\n", + " 'is',\n", + " \"isn't\",\n", + " 'it',\n", + " 'jukebox',\n", + " 'just',\n", + " 'kiss',\n", + " 'know',\n", + " 'last',\n", + " 'lead',\n", + " 'leave',\n", + " 'let',\n", + " \"let's\",\n", + " 'like',\n", + " 'love',\n", + " 'lover',\n", + " 'magnet',\n", + " 'make',\n", + " 'man',\n", + " 'may',\n", + " 'me',\n", + " 'mind',\n", + " 'much',\n", + " 'my',\n", + " 'new',\n", + " 'night',\n", + " 'not',\n", + " 'now',\n", + " 'of',\n", + " 'okay',\n", + " 'on',\n", + " 'one',\n", + " 'our',\n", + " 'out',\n", + " 'over',\n", + " 'place',\n", + " 'plate',\n", + " 'play',\n", + " 'pull',\n", + " 'push',\n", + " 'put',\n", + " 'radio',\n", + " 'room',\n", + " 'say',\n", + " 'shape',\n", + " 'shots',\n", + " 'singing',\n", + " 'slow',\n", + " 'smell',\n", + " 'so',\n", + " 'somebody',\n", + " 'something',\n", + " 'sour',\n", + " 'start',\n", + " 'stop',\n", + " 'story',\n", + " 'sweet',\n", + " 'table',\n", + " 'take',\n", + " 'talk',\n", + " 'taxi',\n", + " 'tell',\n", + " 'that',\n", + " 'the',\n", + " 'then',\n", + " 'thrifty',\n", + " 'to',\n", + " 'too',\n", + " 'trust',\n", + " 'up',\n", + " 'van',\n", + " 'waist',\n", + " 'want',\n", + " 'was',\n", + " 'we',\n", + " \"we're\",\n", + " 'week',\n", + " 'were',\n", + " 'where',\n", + " 'with',\n", + " 'you',\n", + " 'your']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "keys = list(word_freq.keys())\n", + "keys.sort()\n", + "keys" + ] + }, + { + "cell_type": "code", + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "# Your code here\n" + "word_freq2 = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "def dict_iteration(key):\n", + " value = keys[key]\n", + " return print(keys[key],value)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'a' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [36]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m dict_iteration(\u001b[43ma\u001b[49m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" + ] + } + ], + "source": [ + "dict_iteration(a)" ] }, { @@ -176,7 +370,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -190,7 +384,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.3" } }, "nbformat": 4,