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..dbcef7d --- /dev/null +++ b/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb @@ -0,0 +1,450 @@ +{ + "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": 1, + "id": "21625526-3fae-4c55-bab5-f91940070681", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "input_data = [[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_data]\n", + "print (output)\n" + ] + }, + { + "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": 3, + "id": "53061dce-7aa3-4476-8b56-71413c5e135c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "number = 10\n", + "solution = sum([num for num in range(number) if num % 3== 0 or num %5 == 0])if number > 0 else 0\n", + "\n", + "print(solution)" + ] + }, + { + "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": 4, + "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 9, 9, 9, 9, 7, 6, 5, 4]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "int_num = 999997654\n", + "list_array = [int(digit) for digit in str(int_num)]\n", + "print(list_array)" + ] + }, + { + "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": 5, + "id": "86ebe759-76d8-4012-8590-c48a473a6099", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, 2, -3, 4, -5]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "def invert(lst):\n", + " return [-num for num in lst]\n", + "print(invert([1,-2,3,-4,5]))" + ] + }, + { + "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": 6, + "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "square_dic= {num : num**2 for num in range (1,16)}\n", + "print(square_dic)" + ] + }, + { + "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": 7, + "id": "a1d55cea-96c3-4853-8220-17c0904a8816", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "list1= [1, 2]\n", + "list2= [3, 4]\n", + "pairs_list = [(l1 , l2) for l1 in list1 for l2 in list2]\n", + "print(pairs_list)\n" + ] + }, + { + "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": 8, + "id": "0175239c-87fa-4ba0-8776-d62567256db7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "data_dic= {\"a\": [1, 2], \"b\": [3, 4]}\n", + "key_pairs= [(key, value) for key in data_dic for value in data_dic[key]]\n", + "\n", + "print(key_pairs)" + ] + }, + { + "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": 9, + "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", + "metadata": {}, + "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\n", + "data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "tuple_lst = [(no1 , no2) for lst1, lst2 in data for no1 in lst1 for no2 in lst2]\n", + "print(tuple_lst)" + ] + }, + { + "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": 10, + "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('b', 'd'), ('a', 'c'), ('a', 'd'), ('b', 'c')}\n" + ] + } + ], + "source": [ + "# your code goes here\n", + "data = [\"ab\", \"cd\"]\n", + "set_result= {(st1, st2) for st1 in data [0] for st2 in data [1]}\n", + "print(set_result)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c4ca52f-3b0d-4ada-ac55-80bd6a947933", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..dbcef7d 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "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" + "# your code goes here\n", + "input_data = [[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_data]\n", + "print (output)\n" ] }, { @@ -95,12 +106,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "number = 10\n", + "solution = sum([num for num in range(number) if num % 3== 0 or num %5 == 0])if number > 0 else 0\n", + "\n", + "print(solution)" ] }, { @@ -127,12 +150,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 9, 9, 9, 9, 7, 6, 5, 4]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "int_num = 999997654\n", + "list_array = [int(digit) for digit in str(int_num)]\n", + "print(list_array)" ] }, { @@ -155,12 +189,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, 2, -3, 4, -5]\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def invert(lst):\n", + " return [-num for num in lst]\n", + "print(invert([1,-2,3,-4,5]))" ] }, { @@ -193,12 +238,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "square_dic= {num : num**2 for num in range (1,16)}\n", + "print(square_dic)" ] }, { @@ -220,12 +275,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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" + "# your code goes here\n", + "list1= [1, 2]\n", + "list2= [3, 4]\n", + "pairs_list = [(l1 , l2) for l1 in list1 for l2 in list2]\n", + "print(pairs_list)\n" ] }, { @@ -246,12 +313,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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" + "# your code goes here\n", + "data_dic= {\"a\": [1, 2], \"b\": [3, 4]}\n", + "key_pairs= [(key, value) for key in data_dic for value in data_dic[key]]\n", + "\n", + "print(key_pairs)" ] }, { @@ -281,12 +360,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "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" + "# your code goes here\n", + "data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "tuple_lst = [(no1 , no2) for lst1, lst2 in data for no1 in lst1 for no2 in lst2]\n", + "print(tuple_lst)" ] }, { @@ -308,20 +398,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('b', 'd'), ('a', 'c'), ('a', 'd'), ('b', 'c')}\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "data = [\"ab\", \"cd\"]\n", + "set_result= {(st1, st2) for st1 in data [0] for st2 in data [1]}\n", + "print(set_result)\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c4ca52f-3b0d-4ada-ac55-80bd6a947933", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -333,7 +442,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,