diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..3c8bf84 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "output = [\"Senior\" if age > 55 and handicap > 7 else \"Open\" for age, handicap in input]\n", + "list(output)" ] }, { @@ -95,12 +109,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "23" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "def weird_factorial(number):\n", + " return sum(x for x in range(number) if (x % 3 == 0) or (x % 5 == 0))\n", + "\n", + "weird_factorial(10)" ] }, { @@ -127,12 +155,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def split_to_digits(number):\n", + " return [int(x) for x in str(number)]\n", + "\n", + "split_to_digits(123)" ] }, { @@ -158,9 +201,23 @@ "execution_count": null, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "def invert(numbers):\n", + " return [-x for x in numbers]\n", + "\n", + "invert([1,2,3,4,5,-6])" ] }, { @@ -193,12 +250,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{1: 1,\n", + " 2: 4,\n", + " 3: 9,\n", + " 4: 16,\n", + " 5: 25,\n", + " 6: 36,\n", + " 7: 49,\n", + " 8: 64,\n", + " 9: 81,\n", + " 10: 100,\n", + " 11: 121,\n", + " 12: 144,\n", + " 13: 169,\n", + " 14: 196,\n", + " 15: 225}" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "{item: item**2 for item in range(1, 16)}" ] }, { @@ -220,12 +302,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "list1 = [1,2]\n", + "list2 = [3,4]\n", + "[(x, y) for x in list1 for y in list2]" ] }, { @@ -249,9 +344,21 @@ "execution_count": null, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "a = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "[(x, y) for x in a.keys() for y in a[x]]" ] }, { @@ -284,9 +391,22 @@ "execution_count": null, "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "inp = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "\n", + "[(x, y) for tupl in inp for x in tupl[0] for y in tupl[1]]" ] }, { @@ -308,18 +428,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [\"ab\", \"cd\"]\n", + "[(second[0], first[1]) for first in a for second in a]" + ] + }, + { + "cell_type": "markdown", + "id": "7204eace", + "metadata": {}, "source": [ - "# your code goes here" + "Their solution is wrong!! " ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack_prework", "language": "python", "name": "python3" }, @@ -333,7 +473,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.14" } }, "nbformat": 4,