From 2f4018601cc062af5138900650d6579a6d7b442e Mon Sep 17 00:00:00 2001 From: Nathylyn <88753323+Nathylyn@users.noreply.github.com> Date: Sat, 28 Aug 2021 14:36:31 -0400 Subject: [PATCH 1/2] Delete main.ipynb --- your-code/main.ipynb | 404 ------------------------------------------- 1 file changed, 404 deletions(-) delete mode 100755 your-code/main.ipynb diff --git a/your-code/main.ipynb b/your-code/main.ipynb deleted file mode 100755 index d68aeb6..0000000 --- a/your-code/main.ipynb +++ /dev/null @@ -1,404 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Before your start:\n", - "- Read the README.md file\n", - "- Comment as much as you can and use the resources in the README.md file\n", - "- Happy learning!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Challenge 1 - Functions. \n", - "\n", - "A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Create a function to find the summation of the number of every number from 1 to num." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "num=76" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation(num):\n", - " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Challenge 2 - Positional arguments, Args and Kwargs.\n", - "\n", - "The syntax *args and **kwargs in function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form is used to pass a keyworded, variable-length argument list. Create a function to find the summation of all elements of the array." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "array=[3, 8, 16]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation2(*args):\n", - " # This function returns the sum of *args\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, do the same thing with positional arguments." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation3(a, b, c):\n", - " # This function returns a+b+c\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation3" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Given the next dictionary of M&Ms, create a function to sum all non red M&Ms." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation4(**kwargs):\n", - " # This function returns the sum of all M&Ms, except the red ones\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation4" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Below, create a function to sum all following elements." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "a=4\n", - "b=6\n", - "c=15\n", - "d=[48, 465, 23, 96]\n", - "e={'A':16, 'B':32, 'C':64}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation5('define the function input'):\n", - " # This function returns the sum of a,b,c,d and e\n", - " # You need to define the function input\n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Challenge 3 - Iterators, Generators and `yield`. \n", - "\n", - "In iterator in Python is an object that represents a stream of data. However, iterators contain a countable number of values. We traverse through the iterator and return one value at a time. All iterators support a `next` function that allows us to traverse through the iterator. We can create an iterator using the `iter` function that comes with the base package of Python. Below is an example of an iterator." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# We first define our iterator:\n", - "\n", - "iterator = iter([1,2,3])\n", - "\n", - "# We can now iterate through the object using the next function\n", - "\n", - "print(next(iterator))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# We continue to iterate through the iterator.\n", - "\n", - "print(next(iterator))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(next(iterator))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# After we have iterated through all elements, we will get a StopIteration Error\n", - "\n", - "print(next(iterator))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# We can also iterate through an iterator using a for loop like this:\n", - "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", - "# This is why we are redefining the iterator below\n", - "\n", - "iterator = iter([1,2,3])\n", - "\n", - "for i in iterator:\n", - " print(i)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the cell below, write a function that takes an iterator and returns the first element in the iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "iterator=iter([1,2,3])\n", - "\n", - "def divisible2(iterator):\n", - " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", - " # Input: Iterable\n", - " # Output: Integer\n", - " \n", - " # Sample Input: iter([1,2,3])\n", - " # Sample Output: 2\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print divisible2" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Generators\n", - "\n", - "It is quite difficult to create your own iterator since you would have to implement a `next` function. Generators are functions that enable us to create iterators. The difference between a function and a generator is that instead of using `return`, we use `yield`. For example, below we have a function that returns an iterator containing the numbers 0 through n:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def firstn(n):\n", - " number = 0\n", - " while number < n:\n", - " yield number\n", - " number = number + 1" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If we pass 5 to the function, we will see that we have a iterator containing the numbers 0 through 4." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "iterator = firstn(5)\n", - "\n", - "for i in iterator:\n", - " print(i)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "In the cell below, create a generator that takes a number and returns an iterator containing all even numbers between 0 and the number you passed to the generator." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def even_iterator(n):\n", - " # This function produces an iterator containing all even numbers between 0 and n\n", - " # Input: integer\n", - " # Output: iterator\n", - " \n", - " # Sample Input: 5\n", - " # Sample Output: iter([0, 2, 4])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" - ] - }, - { - "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.7.4" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 8a8ff7532d08d56ff074f68b17728187e09df248 Mon Sep 17 00:00:00 2001 From: Nathylyn <88753323+Nathylyn@users.noreply.github.com> Date: Sat, 28 Aug 2021 14:37:17 -0400 Subject: [PATCH 2/2] Add files via upload --- ...ctions_generators [Nathylyn Mendoza].ipynb | 567 ++++++++++++++++++ 1 file changed, 567 insertions(+) create mode 100644 your-code/lab_functions_generators [Nathylyn Mendoza].ipynb diff --git a/your-code/lab_functions_generators [Nathylyn Mendoza].ipynb b/your-code/lab_functions_generators [Nathylyn Mendoza].ipynb new file mode 100644 index 0000000..34d1d45 --- /dev/null +++ b/your-code/lab_functions_generators [Nathylyn Mendoza].ipynb @@ -0,0 +1,567 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Functions. \n", + "\n", + "A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Create a function to find the summation of the number of every number from 1 to num." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "num=76" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def summation(num):\n", + " # This function returns 1+2+3+....+num\n", + " return num*(num+1)/2\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2926.0" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "summation(num)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Positional arguments, Args and Kwargs.\n", + "\n", + "The syntax *args and **kwargs in function definitions is used to pass a variable number of arguments to a function. The single asterisk form (*args) is used to pass a non-keyworded, variable-length argument list, and the double asterisk form is used to pass a keyworded, variable-length argument list. Create a function to find the summation of all elements of the array." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "array=[3, 8, 16]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "#*args, se usara cuando no se este seguro de cuantos argumentos se tengan que pasar a la función\n", + "#te permite pasar una cantidad arbitraria de argumentos\n", + "def summation2(*args):\n", + " # This function returns the sum of *args\n", + " \n", + " # Your code here:\n", + " return sum(*args)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], + "source": [ + "# print summation2\n", + "print (summation2(array))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, do the same thing with positional arguments." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def summation3(a, b, c):\n", + " # This function returns a+b+c\n", + " \n", + " # Your code here:\n", + " return a+b+c" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], + "source": [ + "# print summation3\n", + "print (summation3(*array))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Given the next dictionary of M&Ms, create a function to sum all non red M&Ms." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def summation4(**kwargs):\n", + " # This function returns the sum of all M&Ms, except the red ones\n", + " \n", + " # Your code here:\n", + " return sum(kwargs.values())-kwargs['red']" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], + "source": [ + "# print summation4\n", + "print (summation4(**dictionary))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Below, create a function to sum all following elements." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "a=4\n", + "b=6\n", + "c=15\n", + "d=[48, 465, 23, 96]\n", + "e={'A':16, 'B':32, 'C':64}" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "def summation5(a,b,c,*e,**d):\n", + " # This function returns the sum of a,b,c,d and e\n", + " # You need to define the function input\n", + " # Your code here:\n", + " return a+b+c+sum(e)+sum(d.values())" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "769\n" + ] + } + ], + "source": [ + "# print summation5\n", + "print (summation5(a,b,c,*d,**e))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - Iterators, Generators and `yield`. \n", + "\n", + "In iterator in Python is an object that represents a stream of data. However, iterators contain a countable number of values. We traverse through the iterator and return one value at a time. All iterators support a `next` function that allows us to traverse through the iterator. We can create an iterator using the `iter` function that comes with the base package of Python. Below is an example of an iterator." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "# We first define our iterator:\n", + "\n", + "iterator = iter([1,2,3])\n", + "\n", + "# We can now iterate through the object using the next function\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "# We continue to iterate through the iterator.\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\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# After we have iterated through all elements, we will get a StopIteration Error\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\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;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "# After we have iterated through all elements, we will get a StopIteration Error\n", + "\n", + "print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n" + ] + } + ], + "source": [ + "# We can also iterate through an iterator using a for loop like this:\n", + "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", + "# This is why we are redefining the iterator below\n", + "\n", + "iterator = iter([1,2,3])\n", + "\n", + "for i in iterator:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, write a function that takes an iterator and returns the first element in the iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "iterator=iter([1,2,3])\n", + "\n", + "def divisible2(iterator):\n", + " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " # Input: Iterable\n", + " # Output: Integer\n", + " \n", + " # Sample Input: iter([1,2,3])\n", + " # Sample Output: 2\n", + " \n", + " # Your code here:\n", + " a=0\n", + " for i in iterator:\n", + " if i%2==0:\n", + " return i\n", + " else:\n", + " continue\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# print divisible2\n", + "divisible2(iterator)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Generators\n", + "\n", + "It is quite difficult to create your own iterator since you would have to implement a `next` function. Generators are functions that enable us to create iterators. The difference between a function and a generator is that instead of using `return`, we use `yield`. For example, below we have a function that returns an iterator containing the numbers 0 through n:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "def firstn(n):\n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we pass 5 to the function, we will see that we have a iterator containing the numbers 0 through 4." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "iterator = firstn(5)\n", + "\n", + "for i in iterator:\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, create a generator that takes a number and returns an iterator containing all even numbers between 0 and the number you passed to the generator." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def even_iterator(n):\n", + " # This function produces an iterator containing all even numbers between 0 and n\n", + " # Input: integer\n", + " # Output: iterator\n", + " number = 0\n", + " while number <= n:\n", + " yield number\n", + " number += 2 \n", + " \n", + " # Sample Input: 5\n", + " # Sample Output: iter([0, 2, 4])\n", + " \n", + " def even_iterator(n):\n", + " number = 0\n", + " while number <= n:\n", + " if number%2==0:\n", + " yield number\n", + " number += 1\n", + " else:\n", + " number += 1\n", + " continue \n", + " \n", + " def even_iterator(n):\n", + " for i in range(n+1):\n", + " if i%2==0:\n", + " yield(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "# print even_iterator\n", + "for i in even_iterator(5):\n", + " print(i)" + ] + }, + { + "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 +}