diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..9f6d92f 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -74,9 +74,34 @@ "execution_count": null, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], "source": [ - "# your code goes here" + "def open_or_senior(data):\n", + " return [\n", + " \"Senior\" if age >= 55 and handicap > 7 else \"Open\"\n", + " for age, handicap in data\n", + " ]\n", + "\n", + "input_data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "output = open_or_senior(input_data)\n", + "\n", + "print(output)\n", + "\n", + "\n", + "## Lambda alternative\n", + "\"\"\"open_or_senior = lambda data: [\n", + " \"Senior\" if a >= 55 and h > 7 else \"Open\"\n", + " for a, h in data\n", + "]\"\"\"" ] }, { @@ -95,12 +120,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ - "# your code goes here" + "def solution(number):\n", + " if number < 0:\n", + " return 0\n", + " \n", + " return sum(\n", + " n for n in range(number)\n", + " if n % 3 == 0 or n % 5 == 0\n", + " )\n", + "\n", + "print(solution(10))" ] }, { @@ -130,9 +172,37 @@ "execution_count": null, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1]\n", + "[8, 6, 7, 5, 3, 0, 9]\n" + ] + } + ], "source": [ - "# your code goes here" + "def digitize(n):\n", + " return [int(d) for d in str(n)]\n", + "\n", + "print(digitize(123)) # [1, 2, 3]\n", + "print(digitize(1)) # [1]\n", + "print(digitize(8675309)) # [8, 6, 7, 5, 3, 0, 9]\n", + "\n", + "##Alternative (without converting to string)\n", + "\n", + "\"\"\"def digitize(n):\n", + " digits = []\n", + " if n == 0:\n", + " return [0]\n", + " \n", + " while n > 0:\n", + " digits.append(n % 10)\n", + " n //= 10\n", + " \n", + " return digits[::-1]\"\"\"" ] }, { @@ -155,12 +225,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n", + "[-1, 2, -3, 4, -5]\n", + "[]\n" + ] + } + ], "source": [ - "# your code goes here" + "def invert(lst):\n", + " return [-x for x in lst]\n", + "\n", + "print(invert([1,2,3,4,5])) # [-1,-2,-3,-4,-5]\n", + "print(invert([1,-2,3,-4,5])) # [-1,2,-3,4,-5]\n", + "print(invert([])) # []" ] }, { @@ -196,9 +281,34 @@ "execution_count": null, "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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\n" + ] + } + ], "source": [ - "# your code goes here" + "squares = {x: x**2 for x in range(1, 16)}\n", + "\n", + "# for k, v in squares.items():\n", + "# print(f\"{k} -> {v}\")" ] }, { @@ -220,12 +330,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], "source": [ - "# your code goes here" + "def all_pairs(list1, list2):\n", + " return [(x, y) for x in list1 for y in list2]\n", + "\n", + "print(all_pairs([1, 2], [3, 4]))\n" ] }, { @@ -246,12 +367,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], "source": [ - "# your code goes here" + "def expand_dict(d):\n", + " return [(key, value) for key, values in d.items() for value in values]\n", + "\n", + "data = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "\n", + "print(expand_dict(data))" ] }, { @@ -281,12 +415,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n" + ] + } + ], "source": [ - "# your code goes here" + "def all_pairs_from_tuples(data):\n", + " return [\n", + " (x, y)\n", + " for list1, list2 in data\n", + " for x in list1\n", + " for y in list2\n", + " ]\n", + "\n", + "input_data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "\n", + "print(all_pairs_from_tuples(input_data))" ] }, { @@ -308,18 +460,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('a', 'd'), ('c', 'b'), ('c', 'd'), ('a', 'b')}\n" + ] + } + ], "source": [ - "# your code goes here" + "def char_pairs(strings):\n", + " first_chars = [s[0] for s in strings]\n", + " second_chars = [s[1] for s in strings]\n", + " \n", + " return {(x, y) for x in first_chars for y in second_chars}\n", + "\n", + "print(char_pairs([\"ab\", \"cd\"]))\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -333,7 +499,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,