LUNAR
🌙
Dashboard
Browse
SW04_Auftrag_en
JUPYTER
View Source
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW04 - PYTHON BASICS \n", "\n", "These are the self-study tasks for the semester week, which you will solve within a week in your JupyterHub environment. After completing your work, download a copy of the Jupyter notebook file locally to your laptop (right-click on the file in the JupyterHub file browser -> Download).\n", "\n", "On ILIAS you will find the corresponding scheduled work assignments every week, where you will upload your solved Jupyter notebook file. After you have submitted your work, you will receive a corresponding sample solution for the work assignment. Your submission will not be corrected. Although the work assignments are marked \"mandatory\", there are no tests in the module. The grades from the tests during the semester count.\n", "\n", "We wish you much success!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 1. Task - Fizzbuzz return in a list \n", "\n", "To do this, use code for Fizzbuzz from the previous exercise and extend it,\n", "that the numbers from 100 up to and including 88 are written with the strings *fizz, buzz and fizzbuzz* into the list with variable names *fb_list*.\n", "Use the same data type for all elements in the list, i.e. string and numbers too!\n", "First print the entire list and then print each element of the list individually to the console." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Buzz', 'Fizz', '98', '97', 'Fizz', 'Buzz', '94', 'Fizz', '92', '91', 'FizzBuzz', '89', '88']\n", "Buzz\n", "Fizz\n", "98\n", "97\n", "Fizz\n", "Buzz\n", "94\n", "Fizz\n", "92\n", "91\n", "FizzBuzz\n", "89\n", "88\n" ] } ], "source": [ "x = 101\n", "fb_list = list()\n", "\n", "while x > 88:\n", " x -= 1\n", "\n", " if x % 3 == 0 and x % 5 == 0:\n", " fb_list.append(\"FizzBuzz\")\n", "\n", " elif x % 3 == 0:\n", " fb_list.append(\"Fizz\")\n", "\n", " elif x % 5 == 0:\n", " fb_list.append(\"Buzz\")\n", "\n", " else:\n", " fb_list.append(str(x))\n", "\n", "print(fb_list)\n", "\n", "for element in fb_list:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 2nd Task - Day of Year \n", "\n", "Write a program to calculate the number of days since the beginning of the year.\n", "The current date is entered in the format DDMMYYYY, e.g. 30032002 for March 30, 2002, which is the 89th day.\n", "\n", "Use the console to display which anniversary of the year this is. Take leap years into account using the property:\n", "- A year is a leap year if it is divisible by 4.\n", "- However, a year is not a leap year if it is divisible by 100, unless it is also divisible by 400.\n", "\n", "*Tip: A list could be a suitable data structure for keeping the days per month.*\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "139\n" ] } ], "source": [ "days_per_month = [31,None,31,30,31,30,31,31,30,31,30,31]\n", "\n", "date = input(\"Enter the current date (DDMMYYYY): \")\n", "day = int(date[:2])\n", "month = int(date[2:4])\n", "year = int(date[4:8])\n", "\n", "while True:\n", " if year % 400 == 0:\n", " days_per_month[1] = 29\n", " break\n", "\n", " elif year % 100 == 0:\n", " days_per_month[1] = 28\n", " break\n", "\n", " elif year % 4 == 0:\n", " days_per_month[1] = 29\n", " break \n", "\n", " else:\n", " days_per_month[1] = 28\n", " break\n", "\n", "total = sum(days_per_month[:month-1])\n", "print(day + total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 3. Task - Slicing Alphabet \n", "\n", "Given the following string:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alphabet = 'abcdefghijklmnopqrstuvwxyz'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the correct code one by one, each with a print call for the output:\n", "\n", "1. The first half of the string using only start **and** end indices\n", "2. The first half of the string, using only the end index.\n", "3. The second half of the string using start and end indices.\n", "4. The second half of the string, using only the starting index.\n", "5. Every second letter in the string that starts with 'a'.\n", "6. The entire string in reverse order.\n", "7. Every third letter of the string reversed, starting with 'z'.\n", "\n", "*Tip: Use the len() function for the length of a string or the last index*\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdefghijklm\n", "abcdefghijklm\n", "nopqrstuvwxyz\n", "nopqrstuvwxyz\n", "acegikmoqsuwy\n", "zyxwvutsrqponmlkjihgfedcba\n", "zwtqnkheb\n" ] } ], "source": [ "alphabet = 'abcdefghijklmnopqrstuvwxyz'\n", "l = len(alphabet)\n", "\n", "# Number 1\n", "print(alphabet[0:(l//2)])\n", "\n", "# Number 2\n", "print(alphabet[:(l//2)])\n", "\n", "# Number 3\n", "print(alphabet[(l//2):l])\n", "\n", "# Number 4\n", "print(alphabet[(l//2):])\n", "\n", "# Number 5\n", "print(alphabet[:l:2])\n", "\n", "# Number 6\n", "print(alphabet[::-1])\n", "\n", "# Number 7\n", "print(alphabet[::-1][::3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 4. Task - Multiplication table \n", "\n", "Write a program that displays an n x m multiplication table on the console. The n is in the row and the m is in the column. m and n run from 1 to 10.\n", "\n", "The table should appear on the console as follows:\n", "\n", "```Python\n", "1 2 3 4 5 6 7 8 9 10\n", "2 4 6 8 10 12 14 16 18 20\n", "3 6 9 12 15 18 21 24 27 30\n", "4 8 12 16 20 24 28 32 36 40\n", "5 10 15 20 25 30 35 40 45 50\n", "6 12 18 24 30 36 42 48 54 60\n", "7 14 21 28 35 42 49 56 63 70\n", "8 16 24 32 40 48 56 64 72 80\n", "9 18 27 36 45 54 63 72 81 90\n", "10 20 30 40 50 60 70 80 90 100\n", "```\n", "\n", "*Tips:\n", "Use two nested loops, one for the columns and one for the rows.\n", "Use f-string and the end parameter like ```print(f\"{somevalue:<3d}, end=\"\")``` for formatting on one line*" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6 7 8 9 10 \n", "2 4 6 8 10 12 14 16 18 20 \n", "3 6 9 12 15 18 21 24 27 30 \n", "4 8 12 16 20 24 28 32 36 40 \n", "5 10 15 20 25 30 35 40 45 50 \n", "6 12 18 24 30 36 42 48 54 60 \n", "7 14 21 28 35 42 49 56 63 70 \n", "8 16 24 32 40 48 56 64 72 80 \n", "9 18 27 36 45 54 63 72 81 90 \n", "10 20 30 40 50 60 70 80 90 100 \n" ] } ], "source": [ "for line in range(1,11):\n", " for column in range(1,11):\n", " print(f\"{line * column:<3}\", end=\" \")\n", " \n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "end " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }