From 31a7f3f30d6afe2d8aad5814a764ae2f7ed59db5 Mon Sep 17 00:00:00 2001 From: Cristhian Ordonez Date: Sat, 4 Sep 2021 14:32:57 -0400 Subject: [PATCH] All challenges done --- .../Learning-checkpoint.ipynb | 556 ++++++++++++++++++ .../Untitled-checkpoint.ipynb | 6 + .../challenge-1-checkpoint.ipynb | 227 +++++++ .../challenge-2-checkpoint.ipynb | 501 ++++++++++++++++ .../challenge-3-checkpoint.ipynb | 497 ++++++++++++++++ your-code/Learning.ipynb | 2 +- your-code/Untitled.ipynb | 104 ++++ your-code/challenge-1.ipynb | 235 +++++++- your-code/challenge-2.ipynb | 288 +++++++-- your-code/challenge-3.ipynb | 395 +++++++++++-- 10 files changed, 2679 insertions(+), 132 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/Learning-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb create mode 100644 your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb create mode 100644 your-code/Untitled.ipynb diff --git a/your-code/.ipynb_checkpoints/Learning-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Learning-checkpoint.ipynb new file mode 100644 index 0000000..b1be774 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Learning-checkpoint.ipynb @@ -0,0 +1,556 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dicts, Sets, and Tuples\n", + "\n", + "\n", + "Lesson Goals\n", + "\n", + " Learn how to use tuples\n", + " Learn how to use dicts\n", + " Learn how to use sets\n", + "\n", + "Introduction\n", + "\n", + "In the previous lesson, we learned about lists. Lists are one of the most important data structures in Python. However, we cannot survive with lists alone. There are many use cases where other data structures are better suited. This lesson will introduce three other important data structures in Python.\n", + "Tuples\n", + "\n", + "Tuples are sequences just like list. However, the main difference between tuples and lists is that tuples are immutable. This means that the values inside of a tuple cannot be overwritten (or mutated) once the tuple is defined.\n", + "\n", + "We define tuples using parentheses and specify the sequence in our tuple as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "chocolates = ('dark', 'milk', 'semi sweet')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can retrieve any item in the tuple in the same way we would with a string. However, we cannot reassign new values to a tuple." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'dark'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chocolates[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mchocolates\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'caramel filled'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "chocolates[3] = 'caramel filled'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Similar to lists, we can find the length of a tuple using the len() function. We will also get an out of bounds error if we try to access a tuple position that is beyond the tuple's length. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(chocolates)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "IndexError", + "evalue": "tuple index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mchocolates\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: tuple index out of range" + ] + } + ], + "source": [ + "chocolates[10]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can iterate through tuples just like we iterate through lists. However, as previously mentioned, we cannot change the values of the tuple." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dark\n", + "milk\n", + "semi sweet\n" + ] + } + ], + "source": [ + "for chocolate in chocolates:\n", + " print(chocolate)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dicts\n", + "\n", + "Sometimes we don't just want to store data in a sequence. There are cases where we want to easily retrieve our data rather than iterate through an entire list. There are also cases where we need to label our data. For example, the phone numbers stored in our phone are labeled using the name of our contacts. In these cases, it is better to use a dict. Dicts are a sequence of key value pairs. We store the data behind the scenes in a hash map. This means that we use the key to generate a unique index (called a hash) and store the value in the location marked by that index. This makes retrieval very fast.\n", + "\n", + "We can manually create a dict by specifying all keys and values separated by a colon within curly braces." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "contacts = {'John': '312-555-1234', 'Paul': '312-555-3123', 'George': '312-555-3333', 'Ringo': '312-555-2222'}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can access the keys and the values separately in a dict using the keys() and values() methods. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['John', 'Paul', 'George', 'Ringo'])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "contacts.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values(['312-555-1234', '312-555-3123', '312-555-3333', '312-555-2222'])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "contacts.values()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can list both keys and values as tuples using the items() method.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_items([('John', '312-555-1234'), ('Paul', '312-555-3123'), ('George', '312-555-3333'), ('Ringo', '312-555-2222')])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "contacts.items()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We add new keys and values to a dict using the following syntax:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "contacts['Pete'] = '312-555-1111'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Dicts are not immutable. Therefore, we can change a value by reassigning a new value to a key." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "contacts['Paul'] = '312-555-4444'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We delete a record from a dict using the del command. " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "del contacts['Pete']" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can create an empty dict with only a pair of curly braces:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "empty_dict = {}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Iterating Through a Dict\n", + "\n", + "We can use keys to iterate through the keys, values() to iterate through the values and items() to iterate through both simultaneously. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John\n", + "Paul\n", + "George\n", + "Ringo\n", + "\n", + "312-555-1234\n", + "312-555-4444\n", + "312-555-3333\n", + "312-555-2222\n" + ] + } + ], + "source": [ + "for i in contacts.keys():\n", + " print(i)\n", + "\n", + "print ('')\n", + "\n", + "for i in contacts.values():\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John: 312-555-1234\n", + "Paul: 312-555-4444\n", + "George: 312-555-3333\n", + "Ringo: 312-555-2222\n" + ] + } + ], + "source": [ + "for k, v in contacts.items():\n", + " print(k+\": \"+v)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Dict Comprehensions\n", + "\n", + "Just like list comprehensions, we can also iterate through a dict to generate a new list or new dict. Below is an example of adding the country code to our contact dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "John: +1-312-555-1234\n", + "Paul: +1-312-555-4444\n", + "George: +1-312-555-3333\n", + "Ringo: +1-312-555-2222\n" + ] + } + ], + "source": [ + "international = {k: \"+1-\"+v for k, v in contacts.items()}\n", + "for k, v in international.items():\n", + " print(k+\": \"+v)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Sets\n", + "\n", + "Sets are unordered collections of unique elements. Similar to lists, they are also mutable. This means that we can insert and delete values to our set.\n", + "\n", + "We can define an empty set as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "letters = set()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We then add to the set using the add command. " + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a'}\n" + ] + } + ], + "source": [ + "letters.add('a')\n", + "\n", + "print(letters)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can delete from our set using the remove command. The remove command will throw an error if the element does not exist in the set. We can use the pop command if we would like to try removing the element but do nothing if it is not in the set. " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "letters.remove('a')\n", + "\n", + "print(letters)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Union, Intersection, and Difference\n", + "\n", + "The fact that sets contain unique values and are not ordered allows us to compare sets and find their union, intersection, and difference.\n", + "\n", + "We will look at examples of all three operations in the code block below." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Logan', 'Madison'}\n", + "{'Logan', 'Joanna', 'John', 'Madison', 'Alexander', 'Mary'}\n", + "{'John', 'Alexander'}\n" + ] + } + ], + "source": [ + "# We start off defining both sets. Note that we have to use both parentheses and brackets.\n", + "girl_names = set(['Mary', 'Madison', 'Logan', 'Joanna'])\n", + "boy_names = set(['John', 'Alexander', 'Logan', 'Madison'])\n", + "\n", + "# We find the unisex names by finding the intersection of boy and girl names\n", + "unisex_names = girl_names.intersection(boy_names)\n", + "print(unisex_names)\n", + "\n", + "\n", + "# We find the list of all names without duplicates by finding the union of both sets\n", + "all_names = boy_names.union(girl_names)\n", + "print(all_names)\n", + "\n", + "\n", + "# We find the difference between both sets by subtracting one set from the other\n", + "boy_only_names = boy_names - girl_names\n", + "print(boy_only_names)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..363fcab --- /dev/null +++ b/your-code/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} 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..a7cf73a --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.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.8.8" + } + }, + "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..27d5ee3 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -0,0 +1,501 @@ +{ + "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 89, 6, 52, 86, 16, 87, 54, 68, 75, 10, 88, 70, 88, 32, 33, 26, 69, 32, 7, 81, 87, 13, 100, 73, 10, 26, 74, 44, 90, 8, 55, 18, 38, 73, 87, 67, 35, 69, 34, 100, 65, 81, 84, 21, 7, 5, 54, 59, 66, 64, 56, 29, 31, 71, 97, 13, 25, 96, 93, 10, 76, 85, 23, 18, 48, 14, 46, 49, 51, 23, 51, 92, 18, 80, 85, 58, 39, 25, 83]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "import random\n", + "randomlist = []\n", + "for i in range(0,80):\n", + " n = random.randint(1,100)\n", + " randomlist.append(n)\n", + "print(randomlist)\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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n", + "59\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set1=set(randomlist)\n", + "print(set1)\n", + "print(len(set1))\n", + "\n", + "#The set doesnt have the same length because it doesnt allow duplicates values\n", + "\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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[37, 78, 53, 52, 44, 59, 37, 10, 40, 62, 30, 70, 61, 42, 63, 44, 50, 67, 81, 34, 62, 28, 27, 15, 60, 28, 89, 80, 96, 40, 5, 41, 95, 83, 69, 56, 14, 68, 28, 11, 73, 93, 36, 37, 49, 84, 5, 19, 85, 23, 50, 20, 93, 42, 5, 65, 70, 48, 14, 43, 68, 9, 19, 83, 100, 25, 33, 9, 50, 62, 87, 28, 10, 15, 14, 70, 53, 84, 42, 54]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "import random\n", + "sample_list_2 = []\n", + "for i in range(0,80):\n", + " n = random.randint(1,100)\n", + " sample_list_2.append(n)\n", + "print(sample_list_2)\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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 9, 10, 11, 14, 15, 19, 20, 23, 25, 27, 28, 30, 33, 34, 36, 37, 40, 41, 42, 43, 44, 48, 49, 50, 52, 53, 54, 56, 59, 60, 61, 62, 63, 65, 67, 68, 69, 70, 73, 78, 80, 81, 83, 84, 85, 87, 89, 93, 95, 96, 100}\n", + "52\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set2=set(sample_list_2)\n", + "print(set2)\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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set3 = []\n", + "for i in set1:\n", + " if i in set2:\n", + " set3.append(i)\n", + "print(set(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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set4 = []\n", + "for i in set2:\n", + " if i in set1:\n", + " set4.append(i)\n", + "print(set(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": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n", + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(type(set3),type(set4))\n", + "#set5 = list(set3).intersection(list(set4))\n", + "set5 = []\n", + "set5 = set(set3).intersection(set(set4))\n", + "print(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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "59\n", + "52\n", + "30\n", + "30\n", + "30\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": "markdown", + "metadata": {}, + "source": [ + "#### Create an empty set called `set6`." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6 = set([])\n", + "print(type(set6))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Add `set3` and `set5` to `set6` using the Python Set `update` method." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, '1', 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, '2', 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set6.update(set3,set5)\n", + "print(set6)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Check if `set1` and `set6` are equal." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False, those are NOT equal\n" + ] + } + ], + "source": [ + "# Your code here\n", + "if set(set1) == set(set6):\n", + " print('True, those are equal')\n", + "else:\n", + " print('False, those are NOT equal')" + ] + }, + { + "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": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "# Your code here\n", + "z = set1.issubset(set2)\n", + "print(z)" + ] + }, + { + "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": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "{5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 78, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 95, 96, 97, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "z_union = set(set3).union(set(set4)).union(set5)\n", + "z_union = z_union.union(set1).union(set2)\n", + "print(z_union)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Using the `pop` method, remove the first element from `set1`." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "{5, 6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n", + "{6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "#fruits = {\"apple\", \"banana\", \"cherry\"}\n", + "#fruits.pop()\n", + "#print(fruits)\n", + "\n", + "print(type(set1))\n", + "print(set1)\n", + "set1.pop()\n", + "print(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": 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.8.8" + } + }, + "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..808050d --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb @@ -0,0 +1,497 @@ +{ + "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 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": "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": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'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" + ] + } + ], + "source": [ + "# Your code here\n", + "import operator\n", + "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}\n", + "\n", + "# 1. First extract the keys of word_freq and convert it to a list called keys.\n", + "keys = list(word_freq.keys())\n", + "\n", + "# 2. Sort the keys list.\n", + "keys.sort()\n", + "\n", + "# 3. Create an empty dictionary word_freq2.\n", + "word_freq2 = {}\n", + "\n", + "# 4. Use a FOR loop to iterate each value in keys. For each key iterated, \n", + "#find the corresponding value in word_freq and insert the key-value pair to word_freq2.\n", + "\n", + "for i in keys:\n", + " #getting frequencies of the keys\n", + " #(word_freq.get(i))\n", + " word_freq2.update({i: word_freq.get(i)})\n", + "print(word_freq2)\n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\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": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WordFreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " Word Freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.DataFrame(word_freq.items(), columns=['Word', 'Freq'])\n", + "df\n", + "\n" + ] + }, + { + "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": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WordFreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " Word Freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here\n", + "df.sort_values(by=['Word'], ascending=True)\n", + "df" + ] + }, + { + "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": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Word Freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(df.sort_values(by=['Word'], ascending=True))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Sort `df` ascendingly based on column `freq`." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Word Freq\n", + "139 bar 1\n", + "114 let 1\n", + "116 van 1\n", + "60 out 1\n", + "57 it 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "print(df.sort_values(by=['Freq'], ascending=True))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/Learning.ipynb b/your-code/Learning.ipynb index b6764a1..b1be774 100755 --- a/your-code/Learning.ipynb +++ b/your-code/Learning.ipynb @@ -548,7 +548,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/Untitled.ipynb b/your-code/Untitled.ipynb new file mode 100644 index 0000000..0a0831b --- /dev/null +++ b/your-code/Untitled.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 12, + "id": "50fb1d24", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 5, 8, 13, 21, 34, 55]\n" + ] + } + ], + "source": [ + "#Serie Fibonacci\n", + "#Queremos los primeros diez numeros de fibonacci\n", + "a = 0\n", + "b = 1\n", + "lista = []\n", + "for i in range(1,10):\n", + " lista.append(a + b)\n", + " a,b = b, (a + b)\n", + "print(lista)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6f8fb585", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Benja', 25, 'Caracas')\n", + "('Mari', 28, 'Maracaibo')\n", + "('Nathy', 27, 'Caracas')\n", + "('Wil', 33, 'Caracas')\n" + ] + } + ], + "source": [ + "people = ['Benja', 'Mari', 'Nathy', 'Wil']\n", + "ages = [25, 28, 27, 33]\n", + "city = ['Caracas', 'Maracaibo','Caracas','Caracas']\n", + "\n", + "for data in zip(people, ages, city):\n", + " print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "99caa08b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n" + ] + } + ], + "source": [ + "super_duper = [i for i in range(100) if i % 2 == 0]\n", + "print(super_duper)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89503bb6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 2e59d77..7d994d1 100755 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,11 +15,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I',)\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "tup = ('I',)\n", + "print(tup)\n" ] }, { @@ -33,11 +43,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(type(tup))" ] }, { @@ -55,13 +74,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'tuple' object has no attribute 'append'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Your code here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"o\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"h\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"a\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"c\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"k\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" + ] + } + ], "source": [ "# Your code here\n", "\n", - "# Your explanation here\n" + "a.append(\"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "\n", + "# Your explanation here\n", + "#Tuples dont admit new items because they are inmmutable" ] }, { @@ -79,13 +114,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('I', 'r', 'o', 'n', 'h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ "# Your code here\n", - "\n", - "# Your explanation here\n" + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "print(tup)\n", + "# Your explanation here\n", + "#It was possible because Tuples allow re-assign values" ] }, { @@ -103,11 +148,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'r', 'n', 'I', 'o'}\n", + "{'h', 'k', 'c', 'a'}\n", + "\n", + "('I', 'r', 'o', 'n')\n", + "('h', 'a', 'c', 'k')\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "lst1 = []\n", + "lst2 = []\n", + "tup = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "for i in range (0,4): \n", + " lst1.append(tup[i])\n", + " \n", + "for j in range (-4,0): \n", + " lst2.append(tup[j])\n", + "\n", + "print(set(lst1))\n", + "print(set(lst2))\n", + "\n", + "#Other method \n", + "tup_other = tup[0:4]\n", + "print(type(tup_other))\n", + "\n", + "tup1 = tup[:4]\n", + "print(tup1)\n", + "tup2 = tup[-4:]\n", + "print(tup2)\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + " \n", + "\n" ] }, { @@ -121,11 +207,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "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" ] }, { @@ -137,11 +233,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "4\n", + "8\n", + "8\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(len(tup1))\n", + "print(len(tup2))\n", + "print(len(tup3))\n", + "print(len(tup1 + tup2))" ] }, { @@ -153,11 +264,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'tup3' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Your code here\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"h\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mNameError\u001b[0m: name 'tup3' is not defined" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(tup3.index(\"h\"))" ] }, { @@ -177,11 +301,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "true\n", + "false\n", + "true\n", + "false\n", + "false\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "for i in letters:\n", + " if i in tup3:\n", + " print(\"true\")\n", + " else:\n", + " print(\"false\")\n", + "\n", + " \n", + " \n" ] }, { @@ -195,12 +342,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The letter a has a number of occurrence of 1\n", + "The letter b has a number of occurrence of 0\n", + "The letter c has a number of occurrence of 1\n", + "The letter d has a number of occurrence of 0\n", + "The letter e has a number of occurrence of 0\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "letters = [\"a\", \"b\", \"c\", \"d\", \"e\"]\n", + "tup3 = (\"I\", \"r\", \"o\", \"n\", \"h\", \"a\", \"c\", \"k\")\n", + "\n", + "for i in letters:\n", + " print('The letter', i , 'has a number of occurrence of', tup3.count(i))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -219,7 +390,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 41d6911..27d5ee3 100755 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -38,11 +38,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 89, 6, 52, 86, 16, 87, 54, 68, 75, 10, 88, 70, 88, 32, 33, 26, 69, 32, 7, 81, 87, 13, 100, 73, 10, 26, 74, 44, 90, 8, 55, 18, 38, 73, 87, 67, 35, 69, 34, 100, 65, 81, 84, 21, 7, 5, 54, 59, 66, 64, 56, 29, 31, 71, 97, 13, 25, 96, 93, 10, 76, 85, 23, 18, 48, 14, 46, 49, 51, 23, 51, 92, 18, 80, 85, 58, 39, 25, 83]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "import random\n", + "randomlist = []\n", + "for i in range(0,80):\n", + " n = random.randint(1,100)\n", + " randomlist.append(n)\n", + "print(randomlist)\n" ] }, { @@ -54,11 +68,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n", + "59\n" + ] + } + ], + "source": [ + "# Your code here\n", + "set1=set(randomlist)\n", + "print(set1)\n", + "print(len(set1))\n", + "\n", + "#The set doesnt have the same length because it doesnt allow duplicates values\n", + "\n" ] }, { @@ -77,11 +106,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[37, 78, 53, 52, 44, 59, 37, 10, 40, 62, 30, 70, 61, 42, 63, 44, 50, 67, 81, 34, 62, 28, 27, 15, 60, 28, 89, 80, 96, 40, 5, 41, 95, 83, 69, 56, 14, 68, 28, 11, 73, 93, 36, 37, 49, 84, 5, 19, 85, 23, 50, 20, 93, 42, 5, 65, 70, 48, 14, 43, 68, 9, 19, 83, 100, 25, 33, 9, 50, 62, 87, 28, 10, 15, 14, 70, 53, 84, 42, 54]\n" + ] + } + ], + "source": [ + "# Your code here\n", + "\n", + "import random\n", + "sample_list_2 = []\n", + "for i in range(0,80):\n", + " n = random.randint(1,100)\n", + " sample_list_2.append(n)\n", + "print(sample_list_2)\n" ] }, { @@ -93,11 +137,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 9, 10, 11, 14, 15, 19, 20, 23, 25, 27, 28, 30, 33, 34, 36, 37, 40, 41, 42, 43, 44, 48, 49, 50, 52, 53, 54, 56, 59, 60, 61, 62, 63, 65, 67, 68, 69, 70, 73, 78, 80, 81, 83, 84, 85, 87, 89, 93, 95, 96, 100}\n", + "52\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set2=set(sample_list_2)\n", + "print(set2)\n", + "print(len(set2))" ] }, { @@ -109,11 +165,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set3 = []\n", + "for i in set1:\n", + " if i in set2:\n", + " set3.append(i)\n", + "print(set(set3))" ] }, { @@ -125,11 +194,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set4 = []\n", + "for i in set2:\n", + " if i in set1:\n", + " set4.append(i)\n", + "print(set(set4))" ] }, { @@ -141,11 +223,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " \n", + "{5, 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(type(set3),type(set4))\n", + "#set5 = list(set3).intersection(list(set4))\n", + "set5 = []\n", + "set5 = set(set3).intersection(set(set4))\n", + "print(set5)" ] }, { @@ -165,11 +261,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "59\n", + "52\n", + "30\n", + "30\n", + "30\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))" ] }, { @@ -181,11 +294,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6 = set([])\n", + "print(type(set6))" ] }, { @@ -197,11 +320,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{5, '1', 10, 14, 23, 25, 33, 34, 44, 48, 49, 52, 54, 56, 59, 65, 67, 68, 69, 70, 73, '2', 80, 81, 83, 84, 85, 87, 89, 93, 96, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "set6.update(set3,set5)\n", + "print(set6)" ] }, { @@ -213,11 +346,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False, those are NOT equal\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "if set(set1) == set(set6):\n", + " print('True, those are equal')\n", + "else:\n", + " print('False, those are NOT equal')" ] }, { @@ -229,11 +374,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "z = set1.issubset(set2)\n", + "print(z)" ] }, { @@ -247,11 +402,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "{5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 78, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 95, 96, 97, 100}\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "z_union = set(set3).union(set(set4)).union(set5)\n", + "z_union = z_union.union(set1).union(set2)\n", + "print(z_union)" ] }, { @@ -263,11 +431,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "{5, 6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n", + "{6, 7, 8, 10, 13, 14, 16, 18, 21, 23, 25, 26, 29, 31, 32, 33, 34, 35, 38, 39, 44, 46, 48, 49, 51, 52, 54, 55, 56, 58, 59, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 92, 93, 96, 97, 100}\n" + ] + } + ], + "source": [ + "# Your code here\n", + "#fruits = {\"apple\", \"banana\", \"cherry\"}\n", + "#fruits.pop()\n", + "#print(fruits)\n", + "\n", + "print(type(set1))\n", + "print(set1)\n", + "set1.pop()\n", + "print(set1)" ] }, { @@ -307,7 +493,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/challenge-3.ipynb b/your-code/challenge-3.ipynb index 7ab8ea5..808050d 100755 --- a/your-code/challenge-3.ipynb +++ b/your-code/challenge-3.ipynb @@ -22,6 +22,38 @@ "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 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": "markdown", "metadata": {}, @@ -49,52 +81,47 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here\n" - ] - }, - { - "cell_type": "markdown", + "execution_count": 19, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'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" + ] + } + ], "source": [ - "#### Sort the values of `word_freq` ascendingly.\n", + "# Your code here\n", + "import operator\n", + "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}\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", + "# 1. First extract the keys of word_freq and convert it to a list called keys.\n", + "keys = list(word_freq.keys())\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", + "# 2. Sort the keys list.\n", + "keys.sort()\n", "\n", - "```python\n", - "import operator\n", - "sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n", - "print(sorted_tups)\n", - "```\n", + "# 3. Create an empty dictionary word_freq2.\n", + "word_freq2 = {}\n", "\n", - "Therefore, the steps to sort `word_freq` by value are:\n", + "# 4. Use a FOR loop to iterate each value in keys. For each key iterated, \n", + "#find the corresponding value in word_freq and insert the key-value pair to word_freq2.\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", + "for i in keys:\n", + " #getting frequencies of the keys\n", + " #(word_freq.get(i))\n", + " word_freq2.update({i: word_freq.get(i)})\n", + "print(word_freq2)\n", + " \n", + " \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" + "\n", + "\n" ] }, { @@ -110,11 +137,122 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WordFreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " Word Freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "import pandas as pd" + "import pandas as pd\n", + "df = pd.DataFrame(word_freq.items(), columns=['Word', 'Freq'])\n", + "df\n", + "\n" ] }, { @@ -132,11 +270,121 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
WordFreq
0love25
1conversation1
2every6
3we're1
4plate1
.........
135bedsheets3
136fill2
137hours2
138stop1
139bar1
\n", + "

140 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " Word Freq\n", + "0 love 25\n", + "1 conversation 1\n", + "2 every 6\n", + "3 we're 1\n", + "4 plate 1\n", + ".. ... ...\n", + "135 bedsheets 3\n", + "136 fill 2\n", + "137 hours 2\n", + "138 stop 1\n", + "139 bar 1\n", + "\n", + "[140 rows x 2 columns]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "df.sort_values(by=['Word'], ascending=True)\n", + "df" ] }, { @@ -150,11 +398,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Word Freq\n", + "120 a 8\n", + "109 about 1\n", + "43 all 1\n", + "96 although 3\n", + "72 and 23\n", + ".. ... ...\n", + "128 were 3\n", + "41 where 1\n", + "54 with 22\n", + "15 you 16\n", + "97 your 21\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(df.sort_values(by=['Word'], ascending=True))\n" ] }, { @@ -166,12 +436,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Word Freq\n", + "139 bar 1\n", + "114 let 1\n", + "116 van 1\n", + "60 out 1\n", + "57 it 1\n", + ".. ... ...\n", + "0 love 25\n", + "65 in 27\n", + "121 my 33\n", + "56 come 37\n", + "126 on 40\n", + "\n", + "[140 rows x 2 columns]\n" + ] + } + ], "source": [ - "# Your code here\n" + "# Your code here\n", + "print(df.sort_values(by=['Freq'], ascending=True))\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -190,7 +489,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4,