diff --git a/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb new file mode 100644 index 0000000..6bef7fd --- /dev/null +++ b/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb @@ -0,0 +1,341 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | List, Dict and Set Comprehension" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to work with list, dict and set comprehensions to improve the efficiency and clarity of your Python code." + ] + }, + { + "cell_type": "markdown", + "id": "7b3b329e-c56a-4009-b6f2-8849c02c0cb8", + "metadata": {}, + "source": [ + "Hint: If you're having trouble writing a solution using a comprehension, try writing it out in a more traditional way first. This can help you break the problem down into smaller steps and better understand what you need to do. Once you have a working solution, you can then try to transform it into a comprehension if desired. Comprehensions can often be more concise and readable, but it's important to prioritize clarity and correctness over brevity." + ] + }, + { + "cell_type": "markdown", + "id": "20c3e882-9625-471e-afb4-48a4f40c5d1b", + "metadata": { + "tags": [] + }, + "source": [ + "## Challenge 1 - Katas" + ] + }, + { + "cell_type": "markdown", + "id": "75f816bf-f5a2-49a4-89de-89d8a533155f", + "metadata": {}, + "source": [ + "Do the following katas using list, dict or set comprehension." + ] + }, + { + "cell_type": "markdown", + "id": "0cdcc026-2830-4db5-9ec3-76d71833df93", + "metadata": {}, + "source": [ + "### Katas - 1\n", + "\n", + "The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed.\n", + "\n", + "To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.\n", + "\n", + "**Input**\n", + "\n", + "Input will consist of a list of pairs. Each pair contains information for a single potential member. Information consists of an integer for the person's age and an integer for the person's handicap.\n", + "\n", + "**Output**\n", + "\n", + "Output will consist of a list of string values stating whether the respective member is to be placed in the senior or open category.\n", + "\n", + "**Example**\n", + "\n", + "```python\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "output = [\"Open\", \"Open\", \"Senior\", \"Open\", \"Open\", \"Senior\"]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21625526-3fae-4c55-bab5-f91940070681", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "ad8758b7-bc71-4af8-999c-5e4def116906", + "metadata": {}, + "source": [ + "### Katas - 2\n", + "\n", + "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", + "\n", + "Write a solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0.\n", + "\n", + "Note: If the number is a multiple of both 3 and 5, only count it once." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53061dce-7aa3-4476-8b56-71413c5e135c", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "bed9c59d-159a-41c4-86c0-d02b131d7070", + "metadata": {}, + "source": [ + "### Katas - 3\n", + "\n", + "Given a non-negative integer, return an array / a list of the individual digits in order.\n", + "\n", + "Examples:\n", + "\n", + "```python\n", + "\n", + "123 => [1,2,3]\n", + "\n", + "1 => [1]\n", + "\n", + "8675309 => [8,6,7,5,3,0,9]\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "ab3f6eaa-c74d-45fa-a84a-516ae6ff6e0f", + "metadata": {}, + "source": [ + "### Katas - 4\n", + "\n", + "Given a set of numbers, create a function that returns the additive inverse of each. Each positive becomes negatives, and the negatives become positives.\n", + "\n", + "```python\n", + "invert([1,2,3,4,5]) == [-1,-2,-3,-4,-5]\n", + "invert([1,-2,3,-4,5]) == [-1,2,-3,4,-5]\n", + "invert([]) == []\n", + "```\n", + "\n", + "You can assume that all values are integers. Do not mutate the input array/list.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86ebe759-76d8-4012-8590-c48a473a6099", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "3a9f2c20-48d2-4d08-a8c5-76bb53caaafb", + "metadata": { + "tags": [] + }, + "source": [ + "## Challenge 2 - exercises" + ] + }, + { + "cell_type": "markdown", + "id": "1c3bfaae-d514-4d9d-adab-59b66af621c9", + "metadata": {}, + "source": [ + "Do the following exercises using list, dict or set comprehension." + ] + }, + { + "cell_type": "markdown", + "id": "f5471e4a-a939-4626-9715-ad45f32c2487", + "metadata": {}, + "source": [ + "### Exercise 1\n", + "\n", + "Create a dictionary whose keys (`key`) are integers between 1 and 15 (both inclusive) and the values (`value`) are the square of the key (`key`)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "47dafbe6-5f2c-4f51-94d9-ca52f6d11492", + "metadata": {}, + "source": [ + "### Exercise 2\n", + "\n", + "Write a program that takes two lists of integers, and returns a list of all possible pairs of integers where the first integer is from the first list, and the second integer is from the second list.\n", + "\n", + "Example:\n", + "\n", + "```python\n", + "Input: [1, 2], [3, 4]\n", + "Output: [(1, 3), (1, 4), (2, 3), (2, 4)]\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a1d55cea-96c3-4853-8220-17c0904a8816", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "03110a8a-0dd9-4eb4-b6ce-116233b4d6cf", + "metadata": {}, + "source": [ + "### Exercise 3\n", + "\n", + "Write a program that takes a dictionary of lists as input, and returns a list of all possible key-value pairs, where the key is from the dictionary, and the value is from the corresponding list.\n", + "Example:\n", + " \n", + "```python\n", + "Input: {\"a\": [1, 2], \"b\": [3, 4]}\n", + "Output: [(\"a\", 1), (\"a\", 2), (\"b\", 3), (\"b\", 4)]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0175239c-87fa-4ba0-8776-d62567256db7", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "0cd7ff45-52eb-409c-9805-f838cdd95cc1", + "metadata": {}, + "source": [ + "# Bonus exercises" + ] + }, + { + "cell_type": "markdown", + "id": "16d1e67a-6bd7-4932-a998-4542f06f029a", + "metadata": {}, + "source": [ + "### Exercise 1\n", + "\n", + "Write a program that takes a list of tuples, where each tuple contains two lists of integers, and returns a list of all possible pairs of integers where the first integer is from the first list in a tuple, and the second integer is from the second list in the same tuple.\n", + "\n", + "Example:\n", + "```python\n", + "Input: [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "Output: [(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + }, + { + "cell_type": "markdown", + "id": "737478d3-2e93-445f-b732-00043f9e0541", + "metadata": {}, + "source": [ + "### Exercise 2\n", + "\n", + "Write a program that takes a list of strings, and returns a set of all possible pairs of characters where the first character is from the first string, and the second character is from the second string.\n", + "\n", + "Example:\n", + " \n", + "```python\n", + "Input: [\"ab\", \"cd\"]\n", + "Output: {('a', 'b'), ('c', 'd'), ('a', 'd'), ('c', 'b')}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", + "metadata": {}, + "outputs": [], + "source": [ + "# your code goes here" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..fd1a34b 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "input_data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "output = [\"Senior\" if age >= 55 and handicap > 7 else \"Open\"\n", + " for age, handicap in input_data]\n", + "\n", + "output\n" ] }, { @@ -95,12 +111,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def sum_of_multiples(n):\n", + " if n < 0:\n", + " return 0\n", + " \n", + " return sum([i for i in range(n) if i % 3 == 0 or i % 5 == 0]) \n", + " \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "58cca561-0ede-42eb-91b7-21195137766f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "23" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum_of_multiples(10)\n" ] }, { @@ -127,12 +170,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def digitize(n):\n", + " return [int(d) for d in str(n)]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "649a206b-658c-4b95-8ba6-17e7fc2ad7b3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "digitize(123)\n" ] }, { @@ -155,12 +220,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def invert(lst):\n", + " return [-x for x in lst]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ac6aa2e1-9dc8-4ecd-8314-18798a00b97c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[-1, -2, -3, -4, -5]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "invert([1, 2, 3, 4, 5])\n" ] }, { @@ -193,12 +280,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "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": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "square_dict = {x: x**2 for x in range(1, 16)}\n", + "square_dict\n" ] }, { @@ -220,12 +333,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def all_pairs(list1, list2):\n", + " return [(x, y) for x in list1 for y in list2]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "c7576d09-872c-4fba-90ac-b5c7cb46074f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "all_pairs([1, 2], [3, 4])\n" ] }, { @@ -246,12 +381,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def expand_dict(d):\n", + " return [(key, value) for key, values in d.items() for value in values]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1fec06d9-628a-4ea9-84ed-cef7a0159a33", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "expand_dict({\"a\": [1, 2], \"b\": [3, 4]})\n" ] }, { @@ -281,12 +438,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def all_pairs_from_tuples(data):\n", + " return [(x, y) for a, b in data for x in a for y in b]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "f71e7fd5-297b-4cf1-8f8c-5bdf118de358", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "all_pairs_from_tuples([([1, 2], [3, 4]), ([5, 6], [7, 8])])\n" ] }, { @@ -308,13 +487,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "def character_pairs(strings):\n", + " first, second = strings\n", + " return {(a, b) for a in first for b in second}\n" ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "73e9c90e-ea8d-46d5-bc1b-bbf42439212a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')}" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "character_pairs([\"ab\", \"cd\"])\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "844a01d1-7f48-40cf-a23b-49a1af2bfaa8", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -333,7 +544,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.6" } }, "nbformat": 4,