From 59c261290e20ec8d21ea2683504267b381520f4b Mon Sep 17 00:00:00 2001 From: Max Guzman Date: Sat, 30 Oct 2021 15:52:01 -0500 Subject: [PATCH 1/3] =?UTF-8?q?Solving=20lab-functional-programing[Max=20G?= =?UTF-8?q?uzm=C3=A1n]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 788 ++++++++++++++++-- your-code/main.ipynb | 788 ++++++++++++++++-- 2 files changed, 1482 insertions(+), 94 deletions(-) diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 0239d17..0889f07 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ "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[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[0m\n\u001b[0m", + "\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: " ] } @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -146,9 +146,20 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", @@ -159,7 +170,15 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " for i in iterator:\n", + " if i % 2 == 0:\n", + " d = i\n", + " break\n", + " else:\n", + " d = 0\n", + " return d\n", + "\n", + "divisible2(iter([1,5,3,9]))" ] }, { @@ -173,9 +192,20 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def firstn(n):\n", " number = 0\n", @@ -193,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -224,9 +254,43 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n", + "20\n", + "22\n", + "24\n", + "26\n", + "28\n", + "30\n", + "32\n", + "34\n", + "36\n", + "38\n", + "40\n", + "42\n", + "44\n", + "46\n", + "48\n" + ] + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -237,7 +301,20 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " \n", + " number = 0\n", + " while number < n:\n", + " if number % 2 == 0: # Is it even\n", + " yield number\n", + " number = number + 1\n", + " else:\n", + " number = number + 1\n", + " \n", + " \n", + "iterator = even_iterator(50)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -270,12 +347,99 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthiris_type
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width iris_type\n", + "0 5.1 3.5 1.4 0.2 Iris-setosa\n", + "1 4.9 3.0 1.4 0.2 Iris-setosa\n", + "2 4.7 3.2 1.3 0.2 Iris-setosa\n", + "3 4.6 3.1 1.5 0.2 Iris-setosa\n", + "4 5.0 3.6 1.4 0.2 Iris-setosa" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris.head()\n" ] }, { @@ -287,12 +451,29 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 5.843333\n", + "sepal_width 3.054000\n", + "petal_length 3.758667\n", + "petal_width 1.198667\n", + "dtype: float64" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# iris['sepal_length'].mean()\n", + "\n", + "iris.mean()" ] }, { @@ -304,12 +485,28 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.828066\n", + "sepal_width 0.433594\n", + "petal_length 1.764420\n", + "petal_width 0.763161\n", + "dtype: float64" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# np.std(iris)\n", + "iris.std()\n" ] }, { @@ -321,12 +518,42 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sepal_length float64\n", + "sepal_width float64\n", + "petal_length float64\n", + "petal_width float64\n", + "dtype: object\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# for col in iris.columns:\n", + "# # print(iris[col].dtypes)\n", + "# if iris[col].dtypes == 'float64':\n", + "# # print(iris[col].dtypes)\n", + "\n", + "# # iris.columns[0]\n", + "\n", + "iris_numeric = iris.select_dtypes(include='float64')\n", + "\n", + "print(iris_numeric.dtypes)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "# iris.select_dtypes(include='float64')" ] }, { @@ -338,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -351,7 +578,8 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " pulg = x * 0.393701 \n", + " return pulg" ] }, { @@ -363,12 +591,290 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 56, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 5.1 3.5 1.4 0.2\n", + "1 4.9 3.0 1.4 0.2\n", + "2 4.7 3.2 1.3 0.2\n", + "3 4.6 3.1 1.5 0.2\n", + "4 5.0 3.6 1.4 0.2\n", + ".. ... ... ... ...\n", + "145 6.7 3.0 5.2 2.3\n", + "146 6.3 2.5 5.0 1.9\n", + "147 6.5 3.0 5.2 2.0\n", + "148 6.2 3.4 5.4 2.3\n", + "149 5.9 3.0 5.1 1.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
02.0078751.3779540.5511810.078740
11.9291351.1811030.5511810.078740
21.8503951.2598430.5118110.078740
31.8110251.2204730.5905520.078740
41.9685051.4173240.5511810.078740
...............
1452.6377971.1811032.0472450.905512
1462.4803160.9842531.9685050.748032
1472.5590571.1811032.0472450.787402
1482.4409461.3385832.1259850.905512
1492.3228361.1811032.0078750.708662
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.078740\n", + "1 1.929135 1.181103 0.551181 0.078740\n", + "2 1.850395 1.259843 0.511811 0.078740\n", + "3 1.811025 1.220473 0.590552 0.078740\n", + "4 1.968505 1.417324 0.551181 0.078740\n", + ".. ... ... ... ...\n", + "145 2.637797 1.181103 2.047245 0.905512\n", + "146 2.480316 0.984253 1.968505 0.748032\n", + "147 2.559057 1.181103 2.047245 0.787402\n", + "148 2.440946 1.338583 2.125985 0.905512\n", + "149 2.322836 1.181103 2.007875 0.708662\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch" ] }, { @@ -380,9 +886,143 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], + "execution_count": 60, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
07.15.53.42.2
16.95.03.42.2
26.75.23.32.2
36.65.13.52.2
47.05.63.42.2
...............
1458.75.07.24.3
1468.34.57.03.9
1478.55.07.24.0
1488.25.47.44.3
1497.95.07.13.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 7.1 5.5 3.4 2.2\n", + "1 6.9 5.0 3.4 2.2\n", + "2 6.7 5.2 3.3 2.2\n", + "3 6.6 5.1 3.5 2.2\n", + "4 7.0 5.6 3.4 2.2\n", + ".. ... ... ... ...\n", + "145 8.7 5.0 7.2 4.3\n", + "146 8.3 4.5 7.0 3.9\n", + "147 8.5 5.0 7.2 4.0\n", + "148 8.2 5.4 7.4 4.3\n", + "149 7.9 5.0 7.1 3.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", "\n", @@ -393,7 +1033,11 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " y = x + 2\n", + " return y\n", + "\n", + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant" ] }, { @@ -407,12 +1051,36 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[7.9, 4.4, 6.9, 2.5]" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "def get_the_max(df):\n", + " # This function returns the maximum value of a dataset\n", + " # input: DataFrame\n", + " # output: List of maximum value of each column\n", + " \n", + " cols = df.columns\n", + " out = []\n", + " for col in cols:\n", + " out.append(max(df[col]))\n", + " \n", + " return out\n", + "\n", + "get_the_max(iris_numeric)\n", + "# iris.columns" ] }, { @@ -424,14 +1092,40 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 94, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The data frame has 150 rows, and 5 columns\n", + "The data frame has 150 rows, and 5 columns\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# cols = iris.columns\n", + "\n", + "total_length = iris.shape[0]\n", + "total_with = iris.shape[1]\n", + "string1 = 'The data frame has'\n", + "string2 = 'rows, and'\n", + "string3 = 'columns'\n", + "\n", + "print(f'{string1} {total_length} {string2} {total_with} {string3}')\n", + "\n", + "# len(iris[cols[0]])" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -456,7 +1150,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0239d17..0889f07 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -54,7 +54,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -100,7 +100,7 @@ "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[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[0m\n\u001b[0m", + "\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: " ] } @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -146,9 +146,20 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisible2(iterator):\n", " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", @@ -159,7 +170,15 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", - " " + " for i in iterator:\n", + " if i % 2 == 0:\n", + " d = i\n", + " break\n", + " else:\n", + " d = 0\n", + " return d\n", + "\n", + "divisible2(iter([1,5,3,9]))" ] }, { @@ -173,9 +192,20 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def firstn(n):\n", " number = 0\n", @@ -193,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -224,9 +254,43 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n", + "20\n", + "22\n", + "24\n", + "26\n", + "28\n", + "30\n", + "32\n", + "34\n", + "36\n", + "38\n", + "40\n", + "42\n", + "44\n", + "46\n", + "48\n" + ] + } + ], "source": [ "def even_iterator(n):\n", " # This function produces an iterator containing all even numbers between 0 and n\n", @@ -237,7 +301,20 @@ " # Sample Output: iter([0, 2, 4])\n", " \n", " # Your code here:\n", - " " + " \n", + " number = 0\n", + " while number < n:\n", + " if number % 2 == 0: # Is it even\n", + " yield number\n", + " number = number + 1\n", + " else:\n", + " number = number + 1\n", + " \n", + " \n", + "iterator = even_iterator(50)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -253,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -270,12 +347,99 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 11, "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", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthiris_type
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width iris_type\n", + "0 5.1 3.5 1.4 0.2 Iris-setosa\n", + "1 4.9 3.0 1.4 0.2 Iris-setosa\n", + "2 4.7 3.2 1.3 0.2 Iris-setosa\n", + "3 4.6 3.1 1.5 0.2 Iris-setosa\n", + "4 5.0 3.6 1.4 0.2 Iris-setosa" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris.head()\n" ] }, { @@ -287,12 +451,29 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 5.843333\n", + "sepal_width 3.054000\n", + "petal_length 3.758667\n", + "petal_width 1.198667\n", + "dtype: float64" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# iris['sepal_length'].mean()\n", + "\n", + "iris.mean()" ] }, { @@ -304,12 +485,28 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "sepal_length 0.828066\n", + "sepal_width 0.433594\n", + "petal_length 1.764420\n", + "petal_width 0.763161\n", + "dtype: float64" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "# np.std(iris)\n", + "iris.std()\n" ] }, { @@ -321,12 +518,42 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sepal_length float64\n", + "sepal_width float64\n", + "petal_length float64\n", + "petal_width float64\n", + "dtype: object\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# for col in iris.columns:\n", + "# # print(iris[col].dtypes)\n", + "# if iris[col].dtypes == 'float64':\n", + "# # print(iris[col].dtypes)\n", + "\n", + "# # iris.columns[0]\n", + "\n", + "iris_numeric = iris.select_dtypes(include='float64')\n", + "\n", + "print(iris_numeric.dtypes)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "# iris.select_dtypes(include='float64')" ] }, { @@ -338,7 +565,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ @@ -351,7 +578,8 @@ " # Sample Output: 0.393701\n", " \n", " # Your code here:\n", - " " + " pulg = x * 0.393701 \n", + " return pulg" ] }, { @@ -363,12 +591,290 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], + "execution_count": 56, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
...............
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 5.1 3.5 1.4 0.2\n", + "1 4.9 3.0 1.4 0.2\n", + "2 4.7 3.2 1.3 0.2\n", + "3 4.6 3.1 1.5 0.2\n", + "4 5.0 3.6 1.4 0.2\n", + ".. ... ... ... ...\n", + "145 6.7 3.0 5.2 2.3\n", + "146 6.3 2.5 5.0 1.9\n", + "147 6.5 3.0 5.2 2.0\n", + "148 6.2 3.4 5.4 2.3\n", + "149 5.9 3.0 5.1 1.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
02.0078751.3779540.5511810.078740
11.9291351.1811030.5511810.078740
21.8503951.2598430.5118110.078740
31.8110251.2204730.5905520.078740
41.9685051.4173240.5511810.078740
...............
1452.6377971.1811032.0472450.905512
1462.4803160.9842531.9685050.748032
1472.5590571.1811032.0472450.787402
1482.4409461.3385832.1259850.905512
1492.3228361.1811032.0078750.708662
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 2.007875 1.377954 0.551181 0.078740\n", + "1 1.929135 1.181103 0.551181 0.078740\n", + "2 1.850395 1.259843 0.511811 0.078740\n", + "3 1.811025 1.220473 0.590552 0.078740\n", + "4 1.968505 1.417324 0.551181 0.078740\n", + ".. ... ... ... ...\n", + "145 2.637797 1.181103 2.047245 0.905512\n", + "146 2.480316 0.984253 1.968505 0.748032\n", + "147 2.559057 1.181103 2.047245 0.787402\n", + "148 2.440946 1.338583 2.125985 0.905512\n", + "149 2.322836 1.181103 2.007875 0.708662\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "iris_inch = cm_to_in(iris_numeric)\n", + "iris_inch" ] }, { @@ -380,9 +886,143 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], + "execution_count": 60, + "metadata": { + "collapsed": true + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_width
07.15.53.42.2
16.95.03.42.2
26.75.23.32.2
36.65.13.52.2
47.05.63.42.2
...............
1458.75.07.24.3
1468.34.57.03.9
1478.55.07.24.0
1488.25.47.44.3
1497.95.07.13.8
\n", + "

150 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width\n", + "0 7.1 5.5 3.4 2.2\n", + "1 6.9 5.0 3.4 2.2\n", + "2 6.7 5.2 3.3 2.2\n", + "3 6.6 5.1 3.5 2.2\n", + "4 7.0 5.6 3.4 2.2\n", + ".. ... ... ... ...\n", + "145 8.7 5.0 7.2 4.3\n", + "146 8.3 4.5 7.0 3.9\n", + "147 8.5 5.0 7.2 4.0\n", + "148 8.2 5.4 7.4 4.3\n", + "149 7.9 5.0 7.1 3.8\n", + "\n", + "[150 rows x 4 columns]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define constant below:\n", "\n", @@ -393,7 +1033,11 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " " + " y = x + 2\n", + " return y\n", + "\n", + "iris_constant = add_constant(iris_numeric)\n", + "iris_constant" ] }, { @@ -407,12 +1051,36 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[7.9, 4.4, 6.9, 2.5]" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "def get_the_max(df):\n", + " # This function returns the maximum value of a dataset\n", + " # input: DataFrame\n", + " # output: List of maximum value of each column\n", + " \n", + " cols = df.columns\n", + " out = []\n", + " for col in cols:\n", + " out.append(max(df[col]))\n", + " \n", + " return out\n", + "\n", + "get_the_max(iris_numeric)\n", + "# iris.columns" ] }, { @@ -424,14 +1092,40 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 94, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The data frame has 150 rows, and 5 columns\n", + "The data frame has 150 rows, and 5 columns\n" + ] + } + ], "source": [ "# Your code here:\n", - "\n" + "# cols = iris.columns\n", + "\n", + "total_length = iris.shape[0]\n", + "total_with = iris.shape[1]\n", + "string1 = 'The data frame has'\n", + "string2 = 'rows, and'\n", + "string3 = 'columns'\n", + "\n", + "print(f'{string1} {total_length} {string2} {total_with} {string3}')\n", + "\n", + "# len(iris[cols[0]])" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -456,7 +1150,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, From 9111224dded466db3aee7637b259dbaf60cecb11 Mon Sep 17 00:00:00 2001 From: Max Guzman Date: Mon, 1 Nov 2021 12:59:50 -0600 Subject: [PATCH 2/3] =?UTF-8?q?solving=20main=20[Max=20Guzm=C3=A1n]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.ipynb_checkpoints/Q3-checkpoint.ipynb | 207 ++++++++++++++++++ .../.ipynb_checkpoints/main-checkpoint.ipynb | 149 ++++++++++--- your-code/Q3.ipynb | 2 +- your-code/main.ipynb | 149 ++++++++++--- 4 files changed, 442 insertions(+), 65 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb new file mode 100644 index 0000000..f1faea4 --- /dev/null +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -0,0 +1,207 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Lambda** is a special Python function type that is **anonymous**. By *anonymous* it means a lambda function does not have name. Lambda functions are embedded inside codes so that you don't call them like calling regular Python functions.\n", + "\n", + "**`Map`** applies a function to all the items in an input list. The function that is applied can be a standard or a lambda function.\n", + "\n", + "For instance, below is an example of multiplying number tuples in a list:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "\n", + "def multiply(num_tuple):\n", + " return num_tuple[0]*num_tuple[1]\n", + "list(map(multiply, items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "...is the same as:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 12, 30]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = [(1,2), (3,4), (5,6)]\n", + "list(map(lambda item: item[0]*item[1], items))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Why do we sometimes use `lambda` and `map`? Because, as you see in the example above, they make your code really concise by combining 3 lines of code to 1 line.\n", + "\n", + "Besides `map`, there is also **`filter`** that selectively returns elements in an array according to whether you return `True`. There is also **`reduce`** that performs computation on a list of items then returns result.\n", + "\n", + "Here is a [good tutorial](http://book.pythontips.com/en/latest/map_filter.html) about `map`, `filter`, and `reduce`. Read it if you are not familiar with how they are used. Then proceed to the next cell." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the next cell, use `filter` and `lambda` to return a list that contains positive numbers only. The output should be:\n", + "\n", + "```[1, 4, 5]```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "numbers = [1, 4, -1, -100, 0, 5, -99]\n", + "\n", + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, use `reduce` and `lambda` to return a string that only contains English terms. The English terms are separated with a whitespace ` `.\n", + "\n", + "Hints: \n", + "\n", + "* If your Jupyter Notebook cannot import `langdetect`, you need to install it with `pip install langdetect`. If Jupyter Notebook still cannot find the library, try install with `python3 -m pip install langdetect`. This is because you need to install `langdetect` in the same Python run environment where Jupyter Notebook is running.\n", + "\n", + "* If a word is English, `langdetect.detect(word)=='en'` will return `True`.\n", + "\n", + "Your output should read:\n", + "\n", + "```'good morning everyone'```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import langdetect\n", + "from functools import reduce\n", + "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", + "\n", + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Question" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, if you still have time, convert your response in Q2 by using `lambda`, `map`, `filter`, or `reduce` where applicable. Enter your function in the cell below.\n", + "\n", + "As you write Python functions, generally you don't want to make your function too long. Long functions are difficult to read and difficult to debug. If a function is getting too long, consider breaking it into sever shorter functions where the main function calls other functions. If anything goes wrong, you can output debug messages in each of the functions to check where exactly the error is." + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [], + "source": [ + "# Enter your code below" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function below with the Bag of Words lab docs (it's easier for you to debug your code). Your output should be:\n", + "\n", + "```{'bag_of_words': ['ironhack', 'cool', 'love', 'student'], 'term_freq': [[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]}```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_extraction import stop_words\n", + "bow = get_bow_from_docs([\n", + " '../../lab-bag-of-words/your-code/doc1.txt', \n", + " '../../lab-bag-of-words/your-code/doc2.txt', \n", + " '../../lab-bag-of-words/your-code/doc3.txt'],\n", + " stop_words.ENGLISH_STOP_WORDS\n", + ")\n", + "\n", + "print(bow)" + ] + }, + { + "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/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 0889f07..6925491 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -146,16 +146,16 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "6" ] }, - "execution_count": 17, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -170,15 +170,18 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", + " \n", " for i in iterator:\n", " if i % 2 == 0:\n", " d = i\n", " break\n", " else:\n", " d = 0\n", + " \n", + "\n", " return d\n", "\n", - "divisible2(iter([1,5,3,9]))" + "divisible2(iter([1,2,4,6]))" ] }, { @@ -208,10 +211,10 @@ ], "source": [ "def firstn(n):\n", - " number = 0\n", - " while number < n:\n", - " yield number\n", - " number = number + 1" + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 1" ] }, { @@ -304,7 +307,7 @@ " \n", " number = 0\n", " while number < n:\n", - " if number % 2 == 0: # Is it even\n", + " if number % 2 == 0: # Is it even?\n", " yield number\n", " number = number + 1\n", " else:\n", @@ -330,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -347,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -432,14 +435,15 @@ "4 5.0 3.6 1.4 0.2 Iris-setosa" ] }, - "execution_count": 11, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code here:\n", - "iris.head()\n" + "iris.head()\n", + "# iris" ] }, { @@ -518,7 +522,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -544,7 +548,8 @@ "\n", "iris_numeric = iris.select_dtypes(include='float64')\n", "\n", - "print(iris_numeric.dtypes)" + "print(iris_numeric.dtypes)\n", + "# print(iris.dtypes)" ] }, { @@ -565,7 +570,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -579,6 +584,7 @@ " \n", " # Your code here:\n", " pulg = x * 0.393701 \n", + " \n", " return pulg" ] }, @@ -591,7 +597,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 17, "metadata": { "collapsed": true }, @@ -723,7 +729,7 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 56, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -734,7 +740,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 20, "metadata": { "collapsed": true }, @@ -866,7 +872,7 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 57, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -886,7 +892,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 21, "metadata": { "collapsed": true }, @@ -1018,14 +1024,14 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 60, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define constant below:\n", - "\n", + "error = 2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", @@ -1033,7 +1039,7 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " y = x + 2\n", + " y = x + error\n", " return y\n", "\n", "iris_constant = add_constant(iris_numeric)\n", @@ -1051,7 +1057,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -1060,7 +1066,7 @@ "[7.9, 4.4, 6.9, 2.5]" ] }, - "execution_count": 74, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -1073,16 +1079,96 @@ " # output: List of maximum value of each column\n", " \n", " cols = df.columns\n", - " out = []\n", + " out1 = []\n", " for col in cols:\n", - " out.append(max(df[col]))\n", + " out1.append(max(df[col]))\n", " \n", - " return out\n", + " return out1\n", "\n", "get_the_max(iris_numeric)\n", "# iris.columns" ] }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5.0, 5.0, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5.0, 5.5, 4.9, 4.4, 5.1, 5.0, 4.5, 4.4, 5.0, 5.1, 4.8, 5.1, 4.6, 5.3, 5.0, 7.0, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5.0, 5.9, 6.0, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6.0, 5.7, 5.5, 5.5, 5.8, 6.0, 5.4, 6.0, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5.0, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6.0, 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2, 7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6.0, 6.9, 6.7, 6.9, 5.8, 6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9]\n" + ] + } + ], + "source": [ + "# maximum value for each ROW \n", + "def get_the_max(df):\n", + " out2 = []\n", + " for i in range(len(df)):\n", + " out2.append(max(df.iloc[i]))\n", + " return out2\n", + "\n", + "print(get_the_max(iris_numeric)) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "a = get_the_max(iris_numeric)\n", + "b = []\n", + "for item in iris_numeric['sepal_length']:\n", + " b.append(item)\n", + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.1" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric['sepal_length'][0]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1092,14 +1178,13 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The data frame has 150 rows, and 5 columns\n", "The data frame has 150 rows, and 5 columns\n" ] } diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index 75055ac..f1faea4 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -199,7 +199,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0889f07..6925491 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -146,16 +146,16 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "6" ] }, - "execution_count": 17, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -170,15 +170,18 @@ " # Sample Output: 2\n", " \n", " # Your code here:\n", + " \n", " for i in iterator:\n", " if i % 2 == 0:\n", " d = i\n", " break\n", " else:\n", " d = 0\n", + " \n", + "\n", " return d\n", "\n", - "divisible2(iter([1,5,3,9]))" + "divisible2(iter([1,2,4,6]))" ] }, { @@ -208,10 +211,10 @@ ], "source": [ "def firstn(n):\n", - " number = 0\n", - " while number < n:\n", - " yield number\n", - " number = number + 1" + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 1" ] }, { @@ -304,7 +307,7 @@ " \n", " number = 0\n", " while number < n:\n", - " if number % 2 == 0: # Is it even\n", + " if number % 2 == 0: # Is it even?\n", " yield number\n", " number = number + 1\n", " else:\n", @@ -330,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -347,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -432,14 +435,15 @@ "4 5.0 3.6 1.4 0.2 Iris-setosa" ] }, - "execution_count": 11, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code here:\n", - "iris.head()\n" + "iris.head()\n", + "# iris" ] }, { @@ -518,7 +522,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -544,7 +548,8 @@ "\n", "iris_numeric = iris.select_dtypes(include='float64')\n", "\n", - "print(iris_numeric.dtypes)" + "print(iris_numeric.dtypes)\n", + "# print(iris.dtypes)" ] }, { @@ -565,7 +570,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -579,6 +584,7 @@ " \n", " # Your code here:\n", " pulg = x * 0.393701 \n", + " \n", " return pulg" ] }, @@ -591,7 +597,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 17, "metadata": { "collapsed": true }, @@ -723,7 +729,7 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 56, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -734,7 +740,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 20, "metadata": { "collapsed": true }, @@ -866,7 +872,7 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 57, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -886,7 +892,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 21, "metadata": { "collapsed": true }, @@ -1018,14 +1024,14 @@ "[150 rows x 4 columns]" ] }, - "execution_count": 60, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define constant below:\n", - "\n", + "error = 2\n", "\n", "def add_constant(x):\n", " # This function adds a global constant to our input.\n", @@ -1033,7 +1039,7 @@ " # Output: numeric value\n", " \n", " # Your code here:\n", - " y = x + 2\n", + " y = x + error\n", " return y\n", "\n", "iris_constant = add_constant(iris_numeric)\n", @@ -1051,7 +1057,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -1060,7 +1066,7 @@ "[7.9, 4.4, 6.9, 2.5]" ] }, - "execution_count": 74, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -1073,16 +1079,96 @@ " # output: List of maximum value of each column\n", " \n", " cols = df.columns\n", - " out = []\n", + " out1 = []\n", " for col in cols:\n", - " out.append(max(df[col]))\n", + " out1.append(max(df[col]))\n", " \n", - " return out\n", + " return out1\n", "\n", "get_the_max(iris_numeric)\n", "# iris.columns" ] }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5.0, 5.0, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5.0, 5.5, 4.9, 4.4, 5.1, 5.0, 4.5, 4.4, 5.0, 5.1, 4.8, 5.1, 4.6, 5.3, 5.0, 7.0, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5.0, 5.9, 6.0, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6.0, 5.7, 5.5, 5.5, 5.8, 6.0, 5.4, 6.0, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5.0, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6.0, 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2, 7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6.0, 6.9, 6.7, 6.9, 5.8, 6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9]\n" + ] + } + ], + "source": [ + "# maximum value for each ROW \n", + "def get_the_max(df):\n", + " out2 = []\n", + " for i in range(len(df)):\n", + " out2.append(max(df.iloc[i]))\n", + " return out2\n", + "\n", + "print(get_the_max(iris_numeric)) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "a = get_the_max(iris_numeric)\n", + "b = []\n", + "for item in iris_numeric['sepal_length']:\n", + " b.append(item)\n", + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a == b" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.1" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iris_numeric['sepal_length'][0]" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -1092,14 +1178,13 @@ }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "The data frame has 150 rows, and 5 columns\n", "The data frame has 150 rows, and 5 columns\n" ] } From 5602259d90fc44c55385f979acf1948721983342 Mon Sep 17 00:00:00 2001 From: Max Guzman Date: Mon, 1 Nov 2021 17:06:27 -0600 Subject: [PATCH 3/3] =?UTF-8?q?Solving=20Q3=20[Max=20Guzm=C3=A1n]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.ipynb_checkpoints/Q3-checkpoint.ipynb | 48 +++++++++++++++---- your-code/Q3.ipynb | 48 +++++++++++++++---- 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb index f1faea4..b7b8fc0 100644 --- a/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/Q3-checkpoint.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -22,7 +22,7 @@ "[2, 12, 30]" ] }, - "execution_count": 11, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -32,6 +32,7 @@ "\n", "def multiply(num_tuple):\n", " return num_tuple[0]*num_tuple[1]\n", + "\n", "list(map(multiply, items))" ] }, @@ -85,12 +86,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", "\n", + "positive_numbers = list(filter(lambda x: x > 0, numbers))\n", + "print(positive_numbers)\n", + "\n", "# Enter your code below" ] }, @@ -113,15 +125,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 142, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import langdetect\n", "from functools import reduce\n", - "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", "\n", - "# Enter your code below" + "words = ['good morning', '早上好', 'доброго', 'おはようございます', \n", + " 'everyone', '大家', 'каждый', 'みんな'] \n", + "lista = []\n", + "for palabra in words:\n", + " if (langdetect.detect(palabra)=='en') == True:\n", + " lista.append(palabra)\n", + " else:\n", + " continue\n", + "\n", + "reduce(lambda x, y: x + ' ' + y, lista)\n", + "\n" ] }, { diff --git a/your-code/Q3.ipynb b/your-code/Q3.ipynb index f1faea4..b7b8fc0 100644 --- a/your-code/Q3.ipynb +++ b/your-code/Q3.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -22,7 +22,7 @@ "[2, 12, 30]" ] }, - "execution_count": 11, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -32,6 +32,7 @@ "\n", "def multiply(num_tuple):\n", " return num_tuple[0]*num_tuple[1]\n", + "\n", "list(map(multiply, items))" ] }, @@ -85,12 +86,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 5]\n" + ] + } + ], "source": [ "numbers = [1, 4, -1, -100, 0, 5, -99]\n", "\n", + "positive_numbers = list(filter(lambda x: x > 0, numbers))\n", + "print(positive_numbers)\n", + "\n", "# Enter your code below" ] }, @@ -113,15 +125,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 142, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'good morning everyone'" + ] + }, + "execution_count": 142, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import langdetect\n", "from functools import reduce\n", - "words = ['good morning', '早上好', 'доброго', 'おはようございます', 'everyone', '大家', 'каждый', 'みんな']\n", "\n", - "# Enter your code below" + "words = ['good morning', '早上好', 'доброго', 'おはようございます', \n", + " 'everyone', '大家', 'каждый', 'みんな'] \n", + "lista = []\n", + "for palabra in words:\n", + " if (langdetect.detect(palabra)=='en') == True:\n", + " lista.append(palabra)\n", + " else:\n", + " continue\n", + "\n", + "reduce(lambda x, y: x + ' ' + y, lista)\n", + "\n" ] }, {