LUNAR
🌙
Dashboard
Browse
SW05_InClass_EN
JUPYTER
View Source
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SW05 - InClass - Repetition and programming \n", "\n", "This file is intended for interactive revision in the first hour of class. You write directly in the lines of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple with strings - ordered, non-mutable sequential datatype \n", "\n", "**Tuples store immutable collections of elements.**\n", "\n", "Given a tuple of words, the following programming is required:\n", "\n", "- Create a tuple named words that contains at least 5 different strings (e.g. names, fruits or any objects).\n", "- Print the first and last element of the tuple.\n", "- Determine and print the length of the tuple.\n", "- Extract and print a sub-tuple from the second to the fourth element (inclusive).\n", "- Check if a certain word (e.g. \"apple\") is present in the tuple and print an appropriate message." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('apple', 'banana', 'cherry', 'date', 'elderberry', 'coconut')\n", "Erstes Element: apple\n", "Letztes Element: coconut\n", "Länge des Tuples: 6\n", "Sub-Tuple vom zweiten bis vierten Element: ('banana', 'cherry', 'date')\n", "Das Wort 'apple' ist im Tuple vorhanden.\n" ] } ], "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given the above tuple of words, perform the following tasks:\n", "\n", "- Find and print all elements that contain a specific letter (e.g. \"B\").\n", "- Find the longest word in the tuple and print it along with its length.\n", "- Create a dictionary that stores each word in the tuple as a key and its length as a value, and output this dictionary.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Alle Wörter, die mit 'B' beginnen: ('banana', 'elderberry')\n", "Das längste Wort ist 'elderberry' mit einer Länge von 10 Zeichen.\n", "Dictionary der Wörter und ihrer Länge: {'apple': 5, 'banana': 6, 'cherry': 6, 'date': 4, 'elderberry': 10}\n" ] } ], "source": [ "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets - non-mutable, elements only once, but can be added, removed \n", "\n", "**Sets store unique, immutable elements**.\n", "\n", "Write a Python function ```common_elements(list1, list2)``` that takes two lists as input and returns the common elements in those two lists as a set.\n", "\n", "e.g. output on the console\n", "```\n", "list_a = [1, 4, 3, 8, 9, 8]\n", "list_b = [2, 2, 4, 9, 8]\n", "print(common_elements(list_a, list_b))\n", "\n", ">>> {8, 9, 4}\n", "```" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{8, 9, 4}\n" ] } ], "source": [ "# non efficient way with loops\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{8, 9, 4}\n" ] } ], "source": [ "# classic way with conversion into sets\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a Python function ```exclusive_elements(list1, list2)``` that takes two lists as input and returns the common difference of the elements in those two lists as a set.\n", "\n", "e.g. output on the console\n", "```\n", "list_a = [1, 4, 3, 8, 9]\n", "list_b = [2, 2, 4, 9, 8]\n", "print(exclusive_elements(list_a, list_b))\n", "\n", ">>> {1, 2, 3}\n", "```" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3}\n" ] } ], "source": [ "# classic way with conversion into sets\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary - lookup table of english-spanish \n", "\n", "**Dictionaries store key-value pairs**.\n", "\n", "Create a Python function ```translate_to_spanish(text, lookup_table)``` that takes an English text (text, a string) and a dictionary (lookup_table) as input. The function should translate each word in the text into Spanish using the lookup table. If a word is not present in the table, it should be left unchanged. The translated text should then be returned as a string." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hola mundo esto es un prueba Yes \n" ] } ], "source": [ "# lookup table as dictionary given:\n", "lookup_table = {\n", " \"hello\": \"hola\",\n", " \"world\": \"mundo\",\n", " \"this\": \"esto\",\n", " \"is\": \"es\",\n", " \"a\": \"un\",\n", " \"test\": \"prueba\"\n", "}\n", "text = \"Hello world this is a test Yes\"\n", "\n", "# here your code - remark - we aren't using .join() because we didn't introduce this function\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "end " ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }