{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW06 - 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", "## Task 1 - potential energy function \n", "\n", "Implement the function `pot_energy(...)`, which calculates and returns the potential energy of an object from the two parameters `height, mass`. Use SI standards, i.e. [kg] and [m] and the energy in [Ws].\n", "\n", "Test your function with the mass of a 1.98 ton car that is on the Grimsel Pass at 2163 m above sea level. For the acceleration, take 9.81 m/s^2.\n", "\n", "Required formatted console output:\n", "\n", "```\n", "your car with 1980 kg up 2163 m has a potential energy of 42014 kJ\n", "```" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "your car with 1980 kg up 2163 m has a potential energy of 42014 kJ\n" ] } ], "source": [ "mass = input(\"Mass of the vehicle (kg): \")\n", "height = input(\"Mean sea level (m): \")\n", "\n", "def pot_energy(x,y):\n", " x,y = int(mass), int(height)\n", " en = int((x*y*9.81)/1000)+1 #rounding up the value with +1 as required in the output\n", " return en\n", "\n", "print(f\"your car with {mass} kg up {height} m has a potential energy of {int(pot_energy(mass,height))} kJ\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 2 - Factorial function \n", "\n", "Define the function `factorial(...)`, which takes the parameter n as an integer and calculates and returns the n-th factorial of the number.\n", "Then call this function individually with the numbers from 1 to 5 in a loop and output the results of the function on the console using print.\n", "\n", "*Note: For the factorial for n < 1, return 1.*\n", "\n", "Required console output:\n", "\n", "```python\n", "1\n", "2\n", "6\n", "24\n", "120\n", "```" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "6\n", "24\n", "120\n" ] } ], "source": [ "def factorial(n):\n", " if n >= 1:\n", " result = 1\n", " for i in range(1,n+1):\n", " result *= i\n", " print(result)\n", " \n", " else:\n", " return 1\n", " \n", "factorial(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 3 - reverse-a-word function \n", "\n", "Define the function `reverse_word(...)`, which takes the parameter word as a string and returns the word in all uppercase letters in reverse order. (The function itself should not \"print\" anything!)\n", "\n", "*Tip: use .upper() to generate uppercase letters. Assume that only letters and only 1 word are passed*\n", "\n", "Test your function with the following words and return the output to the console\n", "```python\n", "word_1 = \"pineapple\"\n", "word_2 = \"Coconut\"\n", "word_3 = \"cherry\"\n", "```\n", "\n", "Required console output:\n", "\n", "```python\n", "ELPPAENIP\n", "TUNOCOC\n", "YRREHC\n", "```" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ELPPAENIP\n", "TUNOCOC\n", "YRREHC\n" ] } ], "source": [ "def reverse_word(word):\n", " upper_word = word.upper()\n", " return upper_word[::-1]\n", "\n", "word_1 = \"pineapple\"\n", "word_2 = \"Coconut\"\n", "word_3 = \"cherry\"\n", "\n", "print(reverse_word(word_1))\n", "print(reverse_word(word_2))\n", "print(reverse_word(word_3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 4 - Palindrome function \n", "\n", "Define the function `is_palindrome(word)`, which returns `True` if the given string is a palindrome and `False` if not. Case is not taken into account.\n", "\n", "Test your function with the list of the following words. Write the result of the list test into a new list and output it:\n", "\n", "```python\n", "word_list = [\"Pineapple\", \"Anna\", \"Hannah\", \"Jojo\", \"Malayalam\", \"Pipi\", \"Tattarrattat\"]\n", "```\n", "\n", "Required console output with above list to check:\n", "```python\n", "[False, True, True, False, True, False, True]\n", "```\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False, True, True, False, True, False, True]\n" ] } ], "source": [ "def is_palindrome(word):\n", " return str(word.upper()) == str(word.upper()[::-1])\n", "\n", "word_list = [\"Pineapple\", \"Anna\", \"Hannah\", \"Jojo\", \"Malayalam\", \"Pipi\", \"Tattarrattat\"]\n", "\n", "list_pal = []\n", "\n", "for i in word_list:\n", " list_pal.append(is_palindrome(i))\n", "\n", "print(list_pal)" ] }, { "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": 4 }