diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..905adac 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Senior', 'Open', 'Senior', 'Open']\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# The Western Suburbs Croquet Club has two categories of membership, Senior and Open. \n", + "# They would like your help with an application form that will tell prospective members which category they will be placed.\n", + "# To be a senior, a member must be at least 55 years old and have a handicap greater than 7. \n", + "# In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.\n", + "# Input will consist of a list of pairs. Each pair contains information for a single potential member. \n", + "# Information consists of an integer for the person's age and an integer for the person's handicap.\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", + "def open_or_senior(data):\n", + " categories = []\n", + " for age, handicap in data:\n", + " if age >= 55 and handicap > 7:\n", + " categories.append(\"Senior\")\n", + " else:\n", + " categories.append(\"Open\")\n", + " return categories\n", + "\n", + "# Example usage:\n", + "members = [(45, 12), (55, 21), (19, -2), (75, 20), (66, 3)]\n", + "print(open_or_senior(members)) # Output: ['Open', 'Senior', 'Open', 'Senior', 'Open']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "b7de347f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "# create the open_or_senior function using comprehension\n", + "def open_or_senior(data):\n", + " return [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in data]\n", + "\n", + "# Example usage:\n", + "members = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "print(open_or_senior(members)) # Output: ['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']" ] }, { @@ -95,12 +148,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n", + "0\n", + "45\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\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. \n", + "# The sum of these multiples is 23.\n", + "# Write a solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. \n", + "# Additionally, if the number is negative, return 0.\n", + "# Note: If the number is a multiple of both 3 and 5, only count it once.\n", + "def solution(number):\n", + " if number < 0:\n", + " return 0\n", + " return sum(x for x in range(number) if x % 3 == 0 or x % 5 == 0)\n", + "\n", + "# Example usage:\n", + "print(solution(10)) # Output: 23\n", + "print(solution(-5)) # Output: 0\n", + "print(solution(15)) # Output: 60" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "5884a263", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n", + "0\n", + "45\n" + ] + } + ], + "source": [ + "# create the solution function using comprehension\n", + "def solution(number):\n", + " return sum(x for x in range(number) if x % 3 == 0 or x % 5 == 0) if number > 0 else 0\n", + "\n", + "# Example usage:\n", + "print(solution(10)) # Output: 23\n", + "print(solution(-5)) # Output: 0\n", + "print(solution(15)) # Output: 60" ] }, { @@ -130,9 +235,35 @@ "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" + "# your code goes here\n", + "\n", + "# Given a non-negative integer, return an array / a list of the individual digits in order.\n", + "# Examples:\n", + "# 123 => [1,2,3]\n", + "# 1 => [1]\n", + "# 8675309 => [8,6,7,5,3,0,9]\n", + "\n", + "def digitize(n):\n", + " return [int(digit) for digit in str(n)] # Convert to string, then each char to int; char is \"1\", \"2\", \"3\"\n", + "\n", + "\n", + "# Example usage:\n", + "print(digitize(123)) # Output: [1, 2, 3\n", + "print(digitize(1)) # Output: [1]\n", + "print(digitize(8675309)) # Output: [8, 6, 7, 5, 3, 0, 9]\n", + "\n" ] }, { @@ -155,12 +286,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "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" + "# your code goes here\n", + "\n", + "# Given a set of numbers, create a function that returns the additive inverse of each. \n", + "# Each positive becomes negatives, and the negatives become positives.\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", + "# You can assume that all values are integers. \n", + "# Do not mutate the input array/list.\n", + "\n", + "def invert(lst):\n", + " return [-x for x in lst] # Return a new list with opposite sign (do not mutate original)\n", + "\n", + "# Example usage:\n", + "print(invert([1, 2, 3, 4, 5])) # Output: [-1, -2, -3, -4, -5]\n", + "print(invert([1, -2, 3, -4, 5])) # Output: [-1, 2, -3, 4, -5]\n", + "print(invert([])) # Output: []\n" ] }, { @@ -193,12 +350,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "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", + "\n", + "# Do the following exercises using list, dict or set comprehension.\n", + "# Create a dictionary whose keys (`key`) are integers between 1 and 15 (both inclusive) \n", + "# and the values (`value`) are the square of the key (`key`).\n", + "\n", + "squared_dict = {key: key**2 for key in range(1, 16)}\n", + "print(squared_dict) # Output: {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" ] }, { @@ -220,12 +392,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "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", + "\n", + "# Do the following exercises using list, dict or set comprehension.\n", + "# Write a program that takes two lists of integers, \n", + "# and returns a list of all possible pairs of integers \n", + "# where the first integer is from the first list, and the second integer is from the second list.\n", + "# Example:\n", + "# Input: [1, 2], [3, 4]\n", + "# Output: [(1, 3), (1, 4), (2, 3), (2, 4)]\n", + "\n", + "list1 = [1, 2]\n", + "list2 = [3, 4]\n", + "pairs = [(a, b) for a in list1 for b in list2]\n", + "print(pairs) # Output: [(1, 3), (1, 4), (2, 3), (2, 4)]" ] }, { @@ -246,12 +439,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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", + "\n", + "# Do the following exercises using list, dict or set comprehension.\n", + "# Write a program that takes a dictionary of lists as input, \n", + "# and returns a list of all possible key-value pairs, \n", + "# where the key is from the dictionary, and the value is from the corresponding list.\n", + "# Example:\n", + "# Input: {\"a\": [1, 2], \"b\": [3, 4]}\n", + "# Output: [(\"a\", 1), (\"a\", 2), (\"b\", 3), (\"b\", 4)]\n", + "\n", + "input_dict = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "pairs = [(key, value) for key, values in input_dict.items() for value in values]\n", + "print(pairs) # Output: [('a', 1), ('a', 2), ('b', 3), ('b', 4)]" ] }, { @@ -281,12 +494,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "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", + "\n", + "# Write a program that takes a list of tuples, \n", + "# where each tuple contains two lists of integers, \n", + "# and returns a list of all possible pairs of integers \n", + "# where the first integer is from the first list in a tuple, \n", + "# and the second integer is from the second list in the same tuple.\n", + "# Example:\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", + "input_tuples = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "pairs = [(a, b) for list1, list2 in input_tuples for a in list1 for b in list2]\n", + "print(pairs) # Output: [(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n" ] }, { @@ -308,18 +542,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('a', 'c'), ('b', 'd'), ('b', 'c'), ('a', 'd')}\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Write a program that takes a list of strings, \n", + "# and returns a set of all possible pairs of characters \n", + "# where the first character is from the first string, \n", + "# and the second character is from the second string.\n", + "# Example:\n", + "# Input: [\"ab\", \"cd\"]\n", + "# Output: {('a', 'b'), ('c', 'd'), ('a', 'd'), ('c', 'b')}\n", + "\n", + "input_strings = [\"ab\", \"cd\"]\n", + "char_pairs = {(a, b) for a in input_strings[0] for b in input_strings[1]}\n", + "print(char_pairs) # Output: {('a', 'b'), ('c', 'd'), ('a', 'd'), ('c', 'b')}\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -333,7 +587,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,