diff --git a/your-code/comprehension_completed.ipynb b/your-code/comprehension_completed.ipynb new file mode 100644 index 0000000..d3fd3b3 --- /dev/null +++ b/your-code/comprehension_completed.ipynb @@ -0,0 +1,633 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "l7AvVoWqk1ib" + }, + "source": [ + "# List Comprehensions\n", + "\n", + "Complete the following set of exercises to solidify your knowledge of list comprehensions." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "SngrkTaRk1id" + }, + "outputs": [], + "source": [ + "import os;" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WlyrQB6ok1ie" + }, + "source": [ + "#### 1. Use a list comprehension to create and print a list of consecutive integers starting with 1 and ending with 50." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cFVJWr5bk1ie", + "outputId": "b05aa233-238a-4436-fda8-a45279906e02" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1,\n", + " 2,\n", + " 3,\n", + " 4,\n", + " 5,\n", + " 6,\n", + " 7,\n", + " 8,\n", + " 9,\n", + " 10,\n", + " 11,\n", + " 12,\n", + " 13,\n", + " 14,\n", + " 15,\n", + " 16,\n", + " 17,\n", + " 18,\n", + " 19,\n", + " 20,\n", + " 21,\n", + " 22,\n", + " 23,\n", + " 24,\n", + " 25,\n", + " 26,\n", + " 27,\n", + " 28,\n", + " 29,\n", + " 30,\n", + " 31,\n", + " 32,\n", + " 33,\n", + " 34,\n", + " 35,\n", + " 36,\n", + " 37,\n", + " 38,\n", + " 39,\n", + " 40,\n", + " 41,\n", + " 42,\n", + " 43,\n", + " 44,\n", + " 45,\n", + " 46,\n", + " 47,\n", + " 48,\n", + " 49,\n", + " 50]" + ] + }, + "metadata": {}, + "execution_count": 2 + } + ], + "source": [ + "consecutive_integrers = [x for x in range(1,51)]\n", + "\n", + "consecutive_integrers" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1lGLECPNk1ie" + }, + "source": [ + "#### 2. Use a list comprehension to create and print a list of even numbers starting with 2 and ending with 200." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "SqCLuBBEk1ie", + "outputId": "ea3ed4c1-6d7d-4ee1-fd92-8cbdabbc45b6" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[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, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" + ] + } + ], + "source": [ + "even_numbers = [i for i in range(2, 201) if i %2 == 0]\n", + "\n", + "print(even_numbers)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sdikepxck1ie" + }, + "source": [ + "#### 3. Use a list comprehension to create and print a list containing all elements of the 10 x 4 array below." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "O1X0BDQJk1if" + }, + "outputs": [], + "source": [ + "a = [[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", + " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", + " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", + " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", + " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", + " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", + " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", + " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", + " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]];" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5Yg1en3Qk1if", + "outputId": "8636e937-791d-4eab-9f13-097ee092d04f" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.84062117,\n", + " 0.48006452,\n", + " 0.7876326,\n", + " 0.77109654,\n", + " 0.44409793,\n", + " 0.09014516,\n", + " 0.81835917,\n", + " 0.87645456,\n", + " 0.7066597,\n", + " 0.09610873,\n", + " 0.41247947,\n", + " 0.57433389,\n", + " 0.29960807,\n", + " 0.42315023,\n", + " 0.34452557,\n", + " 0.4751035,\n", + " 0.17003563,\n", + " 0.46843998,\n", + " 0.92796258,\n", + " 0.69814654,\n", + " 0.41290051,\n", + " 0.19561071,\n", + " 0.16284783,\n", + " 0.97016248,\n", + " 0.71725408,\n", + " 0.87702738,\n", + " 0.31244595,\n", + " 0.76615487,\n", + " 0.20754036,\n", + " 0.57871812,\n", + " 0.07214068,\n", + " 0.40356048,\n", + " 0.12149553,\n", + " 0.53222417,\n", + " 0.9976855,\n", + " 0.12536346,\n", + " 0.80930099,\n", + " 0.50962849,\n", + " 0.94555126,\n", + " 0.33364763]" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ], + "source": [ + "a = [[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", + " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", + " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", + " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", + " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", + " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", + " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", + " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", + " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]];\n", + "\n", + "\n", + "\n", + "\n", + "l = [x for i in a for x in i]\n", + "\n", + "l" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cPwYEpLFk1if" + }, + "source": [ + "#### 4. Add a condition to the list comprehension above so that only values greater than or equal to 0.5 are printed." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "jVznEEeak1ig", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + }, + "outputId": "040ae0fd-0080-42ac-87ad-9b97b5784903" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.84062117,\n", + " 0.7876326,\n", + " 0.77109654,\n", + " 0.81835917,\n", + " 0.87645456,\n", + " 0.7066597,\n", + " 0.57433389,\n", + " 0.92796258,\n", + " 0.69814654,\n", + " 0.97016248,\n", + " 0.71725408,\n", + " 0.87702738,\n", + " 0.76615487,\n", + " 0.57871812,\n", + " 0.53222417,\n", + " 0.9976855,\n", + " 0.80930099,\n", + " 0.50962849,\n", + " 0.94555126]" + ] + }, + "metadata": {}, + "execution_count": 24 + } + ], + "source": [ + "a = [[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", + " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", + " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", + " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", + " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", + " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", + " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", + " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", + " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]];\n", + "\n", + "\n", + "l = []\n", + "\n", + "#for row in a:\n", + " # for element in row:\n", + " # if element >= 0.5:\n", + " # l.append(element)\n", + "\n", + "l = [element for row in a for element in row if element >= 0.5]\n", + "\n", + "l" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bJ9HeKEPk1ig" + }, + "source": [ + "#### 5. Use a list comprehension to create and print a list containing all elements of the 5 x 2 x 3 array below." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "QUO5gS5lk1ig" + }, + "outputs": [], + "source": [ + "b = [[[0.55867166, 0.06210792, 0.08147297],\n", + " [0.82579068, 0.91512478, 0.06833034]],\n", + "\n", + " [[0.05440634, 0.65857693, 0.30296619],\n", + " [0.06769833, 0.96031863, 0.51293743]],\n", + "\n", + " [[0.09143215, 0.71893382, 0.45850679],\n", + " [0.58256464, 0.59005654, 0.56266457]],\n", + "\n", + " [[0.71600294, 0.87392666, 0.11434044],\n", + " [0.8694668 , 0.65669313, 0.10708681]],\n", + "\n", + " [[0.07529684, 0.46470767, 0.47984544],\n", + " [0.65368638, 0.14901286, 0.23760688]]];" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "tqYt-G8Ok1ig", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + }, + "outputId": "58a61f49-f977-4317-ce70-ef87c71e18fe" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.55867166,\n", + " 0.06210792,\n", + " 0.08147297,\n", + " 0.82579068,\n", + " 0.91512478,\n", + " 0.06833034,\n", + " 0.05440634,\n", + " 0.65857693,\n", + " 0.30296619,\n", + " 0.06769833,\n", + " 0.96031863,\n", + " 0.51293743,\n", + " 0.09143215,\n", + " 0.71893382,\n", + " 0.45850679,\n", + " 0.58256464,\n", + " 0.59005654,\n", + " 0.56266457,\n", + " 0.71600294,\n", + " 0.87392666,\n", + " 0.11434044,\n", + " 0.8694668,\n", + " 0.65669313,\n", + " 0.10708681,\n", + " 0.07529684,\n", + " 0.46470767,\n", + " 0.47984544,\n", + " 0.65368638,\n", + " 0.14901286,\n", + " 0.23760688]" + ] + }, + "metadata": {}, + "execution_count": 38 + } + ], + "source": [ + "b = [[[0.55867166, 0.06210792, 0.08147297],\n", + " [0.82579068, 0.91512478, 0.06833034]],\n", + "\n", + " [[0.05440634, 0.65857693, 0.30296619],\n", + " [0.06769833, 0.96031863, 0.51293743]],\n", + "\n", + " [[0.09143215, 0.71893382, 0.45850679],\n", + " [0.58256464, 0.59005654, 0.56266457]],\n", + "\n", + " [[0.71600294, 0.87392666, 0.11434044],\n", + " [0.8694668 , 0.65669313, 0.10708681]],\n", + "\n", + " [[0.07529684, 0.46470767, 0.47984544],\n", + " [0.65368638, 0.14901286, 0.23760688]]];\n", + "\n", + "#z = []\n", + "\n", + "#for xy in b:\n", + " # for ma in xy:\n", + " # for fo in ma:\n", + " # z.append(fo)\n", + "\n", + "z = [fo for xy in b for ma in xy for fo in ma]\n", + "\n", + "z" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ibxgCwi4k1ig" + }, + "source": [ + "#### 6. Add a condition to the list comprehension above so that the last value in each subarray is printed, but only if it is less than or equal to 0.5." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "id": "xrm4CHofk1ih", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + }, + "outputId": "5532b011-1679-469f-c3b4-22d715cefde3" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[0.06210792,\n", + " 0.08147297,\n", + " 0.06833034,\n", + " 0.05440634,\n", + " 0.30296619,\n", + " 0.06769833,\n", + " 0.09143215,\n", + " 0.45850679,\n", + " 0.11434044,\n", + " 0.10708681,\n", + " 0.07529684,\n", + " 0.46470767,\n", + " 0.47984544,\n", + " 0.14901286,\n", + " 0.23760688]" + ] + }, + "metadata": {}, + "execution_count": 43 + } + ], + "source": [ + "b = [[[0.55867166, 0.06210792, 0.08147297],\n", + " [0.82579068, 0.91512478, 0.06833034]],\n", + "\n", + " [[0.05440634, 0.65857693, 0.30296619],\n", + " [0.06769833, 0.96031863, 0.51293743]],\n", + "\n", + " [[0.09143215, 0.71893382, 0.45850679],\n", + " [0.58256464, 0.59005654, 0.56266457]],\n", + "\n", + " [[0.71600294, 0.87392666, 0.11434044],\n", + " [0.8694668 , 0.65669313, 0.10708681]],\n", + "\n", + " [[0.07529684, 0.46470767, 0.47984544],\n", + " [0.65368638, 0.14901286, 0.23760688]]];\n", + "\n", + "#z = []\n", + "\n", + "#for xy in b:\n", + " #for ma in xy:\n", + " # for fo in ma:\n", + " # if fo <= 0.5:\n", + " # z.append(fo)\n", + "\n", + "z = [fo for xy in b for ma in xy for fo in ma if fo <=0.5]\n", + "\n", + "z\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KYkJlD9Dk1ih" + }, + "source": [ + "#### 7. Use a list comprehension to select and print the names of all CSV files in the */data* directory. Mind slashes." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "id": "F81Blatnk1ih", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 279 + }, + "outputId": "69cdea41-0d93-4c13-f2e4-2aeb9d861954" + }, + "outputs": [ + { + "output_type": "error", + "ename": "FileNotFoundError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"D:/usuarios/marco ayora/Desktop/IronHack/lab-control-flow/lab-list-comprehension/data\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mx2\u001b[0m \u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'D:/usuarios/marco ayora/Desktop/IronHack/lab-control-flow/lab-list-comprehension/data'" + ] + } + ], + "source": [ + "import os\n", + "\n", + "x = os.listdir(\"D:/usuarios/marco ayora/Desktop/IronHack/lab-control-flow/lab-list-comprehension/data\")\n", + "\n", + "x2 =[]\n", + "\n", + "for i in extract:\n", + " if i[-3] == \"csv\":\n", + " x2.append(i)\n", + "\n", + "x3 = [i for i in extract if i [-3:] == \"csv\"]\n", + "\n", + "extract3\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lBP84WNmk1ih" + }, + "source": [ + "### Bonus" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S4tdEou8k1ih" + }, + "source": [ + "Try to solve these katas using list comprehensions." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sGzr_zGGk1ih" + }, + "source": [ + "**Easy**\n", + "- [Insert values](https://www.codewars.com/kata/invert-values)\n", + "- [Sum Square(n)](https://www.codewars.com/kata/square-n-sum)\n", + "- [Digitize](https://www.codewars.com/kata/digitize)\n", + "- [List filtering](https://www.codewars.com/kata/list-filtering)\n", + "- [Arithmetic list](https://www.codewars.com/kata/541da001259d9ca85d000688)\n", + "\n", + "**Medium**\n", + "- [Multiples of 3 or 5](https://www.codewars.com/kata/514b92a657cdc65150000006)\n", + "- [Count of positives / sum of negatives](https://www.codewars.com/kata/count-of-positives-slash-sum-of-negatives)\n", + "- [Categorize new member](https://www.codewars.com/kata/5502c9e7b3216ec63c0001aa)\n", + "\n", + "**Advanced**\n", + "- [Queue time counter](https://www.codewars.com/kata/queue-time-counter)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.2" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file