From 00d8c40dbd2118c639d7df319cc58b6ad7a69990 Mon Sep 17 00:00:00 2001 From: slmj1990-ai Date: Thu, 19 Feb 2026 17:44:04 +0100 Subject: [PATCH] Lab extra list --- lab-python-list-comprehension.ipynb | 350 +++++++++++++++++++++++++--- 1 file changed, 323 insertions(+), 27 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..ac68d56 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -76,7 +76,79 @@ "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def open_or_senior(data):\n", + " \"\"\"\n", + " Determines membership category for each applicant.\n", + "\n", + " Parameters:\n", + " data (list): A list of [age, handicap] pairs.\n", + "\n", + " Returns:\n", + " list: A list of strings (\"Senior\" or \"Open\").\n", + " \"\"\"\n", + " categories = []\n", + "\n", + " for age, handicap in data:\n", + " if age >= 55 and handicap > 7:\n", + " categories.append(\"Senior\")\n", + " else:\n", + " categories.append(\"Open\")\n", + "\n", + " return categories\n", + "\n", + "\n", + "# Test case from the example\n", + "input_data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "output = open_or_senior(input_data)\n", + "\n", + "print(output)\n", + "# Expected:\n", + "# [\"Open\", \"Open\", \"Senior\", \"Open\", \"Open\", \"Senior\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bbfc119c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "def open_or_senior(data):\n", + " \"\"\"\n", + " Determines membership category for each applicant.\n", + "\n", + " Parameters:\n", + " data (list): A list of [age, handicap] pairs.\n", + "\n", + " Returns:\n", + " list: A list of strings (\"Senior\" or \"Open\").\n", + " \"\"\"\n", + " categories = []\n", + "\n", + " for age, handicap in data:\n", + " if age >= 55 and handicap > 7:\n", + " categories.append(\"Senior\")\n", + " else:\n", + " categories.append(\"Open\")\n", + "\n", + " return categories\n", + "\n", + "\n", + "# Test case from the example\n", + "input_data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "output = open_or_senior(input_data)\n", + "\n", + "print(output)\n", + "# Expected:\n", + "# [\"Open\", \"Open\", \"Senior\", \"Open\", \"Open\", \"Senior\"]\n" ] }, { @@ -95,12 +167,44 @@ }, { "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", + "78\n", + "0\n", + "0\n" + ] + } + ], "source": [ - "# your code goes here" + "def sum_of_multiples(n):\n", + " \"\"\"\n", + " Returns the sum of all multiples of 3 or 5 below n.\n", + " If n is negative, returns 0.\n", + " \"\"\"\n", + " if n < 0:\n", + " return 0\n", + "\n", + " total = 0\n", + "\n", + " for number in range(n):\n", + " if number % 3 == 0 or number % 5 == 0:\n", + " total += number\n", + "\n", + " return total\n", + "\n", + "\n", + "# Test cases\n", + "print(sum_of_multiples(10)) # Expected: 23\n", + "print(sum_of_multiples(20)) # Expected: 78\n", + "print(sum_of_multiples(-5)) # Expected: 0\n", + "print(sum_of_multiples(0)) # Expected: 0\n" ] }, { @@ -127,12 +231,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "[0]\n" + ] + } + ], "source": [ - "# your code goes here" + "def digitize(n):\n", + " \"\"\"\n", + " Takes a non-negative integer and returns\n", + " a list of its digits in order.\n", + " \"\"\"\n", + " return [int(digit) for digit in str(n)]\n", + "\n", + "\n", + "# Test cases\n", + "print(digitize(123)) # Expected: [1, 2, 3]\n", + "print(digitize(1)) # Expected: [1]\n", + "print(digitize(8675309)) # Expected: [8, 6, 7, 5, 3, 0, 9]\n", + "print(digitize(0)) # Expected: [0]\n" ] }, { @@ -155,12 +282,41 @@ }, { "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", + "[-1, 2, -3, 4, -5]\n", + "[]\n", + "[1, 2, 3]\n", + "[-1, -2, -3]\n" + ] + } + ], "source": [ - "# your code goes here" + "def invert(numbers):\n", + " \"\"\"\n", + " Returns a new list with the additive inverse of each number.\n", + " Does not modify the original list.\n", + " \"\"\"\n", + " return [-num for num in numbers]\n", + "\n", + "\n", + "# Test cases\n", + "print(invert([1, 2, 3, 4, 5])) # Expected: [-1, -2, -3, -4, -5]\n", + "print(invert([1, -2, 3, -4, 5])) # Expected: [-1, 2, -3, 4, -5]\n", + "print(invert([])) # Expected: []\n", + "\n", + "# Ensure original list is not modified\n", + "original = [1, 2, 3]\n", + "result = invert(original)\n", + "print(original) # Expected: [1, 2, 3]\n", + "print(result) # Expected: [-1, -2, -3]\n" ] }, { @@ -193,12 +349,30 @@ }, { "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" + "def create_square_dict():\n", + " \"\"\"\n", + " Creates a dictionary with keys from 1 to 15 (inclusive)\n", + " and values equal to the square of each key.\n", + " \"\"\"\n", + " return {key: key**2 for key in range(1, 16)}\n", + "\n", + "\n", + "# Test\n", + "square_dict = create_square_dict()\n", + "print(square_dict)\n" ] }, { @@ -220,12 +394,45 @@ }, { "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", + "[]\n", + "[]\n" + ] + } + ], "source": [ - "# your code goes here" + "def all_pairs(list1, list2):\n", + " \"\"\"\n", + " Returns a list of all possible pairs where\n", + " the first element is from list1 and the\n", + " second element is from list2.\n", + " \"\"\"\n", + " pairs = []\n", + " \n", + " for num1 in list1:\n", + " for num2 in list2:\n", + " pairs.append((num1, num2))\n", + " \n", + " return pairs\n", + "\n", + "\n", + "# Test case\n", + "print(all_pairs([1, 2], [3, 4]))\n", + "\n", + "\n", + "print(all_pairs([], [3, 4]))\n", + "\n", + "\n", + "print(all_pairs([1, 2], []))\n", + "\n" ] }, { @@ -246,12 +453,44 @@ }, { "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", + "[]\n", + "[]\n" + ] + } + ], "source": [ - "# your code goes here" + "def key_value_pairs(data):\n", + " \"\"\"\n", + " Takes a dictionary of lists and returns a list of\n", + " all possible (key, value) pairs.\n", + " \"\"\"\n", + " pairs = []\n", + " \n", + " for key, values in data.items():\n", + " for value in values:\n", + " pairs.append((key, value))\n", + " \n", + " return pairs\n", + "\n", + "\n", + "# Test case\n", + "print(key_value_pairs({\"a\": [1, 2], \"b\": [3, 4]}))\n", + "\n", + "\n", + "print(key_value_pairs({}))\n", + "\n", + "\n", + "print(key_value_pairs({\"x\": []}))\n", + "\n" ] }, { @@ -281,12 +520,41 @@ }, { "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" + "def tuple_list_pairs(data):\n", + " \"\"\"\n", + " Takes a list of tuples. Each tuple contains two lists of integers.\n", + " Returns a list of all possible pairs where:\n", + " - The first integer is from the first list\n", + " - The second integer is from the second list (from the same tuple)\n", + " \"\"\"\n", + " result = []\n", + " \n", + " for list1, list2 in data:\n", + " for num1 in list1:\n", + " for num2 in list2:\n", + " result.append((num1, num2))\n", + " \n", + " return result\n", + "\n", + "\n", + "# Test case\n", + "input_data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "print(tuple_list_pairs(input_data))\n", + "\n", + "\n" ] }, { @@ -308,18 +576,46 @@ }, { "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', 'd'), ('b', 'c'), ('a', 'c')}\n" + ] + } + ], "source": [ - "# your code goes here" + "def character_pairs(strings):\n", + " \"\"\"\n", + " Takes a list of strings and returns a set of all possible\n", + " pairs of characters where:\n", + " - The first character is from one string\n", + " - The second character is from a different string\n", + " \"\"\"\n", + " result = set()\n", + " \n", + " for i in range(len(strings)):\n", + " for j in range(i + 1, len(strings)): # avoid pairing a string with itself\n", + " for char1 in strings[i]:\n", + " for char2 in strings[j]:\n", + " result.add((char1, char2))\n", + " \n", + " return result\n", + "\n", + "\n", + "print(character_pairs([\"ab\", \"cd\"]))\n", + "\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -333,7 +629,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.2" } }, "nbformat": 4,