{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment work SW05 - 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 - Grade conversion \n", "\n", "The grades of 5 students are kept in a dictionary. There are only half grades.\n", "\n", "students = {\"Max\": 4, \"Anna\": 5, \"Luis\": 5.5, \"Sophie\": 5, \"Tom\": 6, \"Bob\": 3.5}\n", "\n", "The key for conversion is as follows (tip: create a second dictionary from it): \n", "\n", "4.0 -> E \n", "4.5 -> D \n", "5.0 -> C \n", "5.5 -> B \n", "6.0 -> A \n", "\n", "Write a Python program that prints the name and grade from A-F to the console. It prints an F if the grade is below 4." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Max: E\n", "Anna: C\n", "Luis: B\n", "Sophie: C\n", "Tom: A\n", "Bob: F\n" ] } ], "source": [ "# Students and grades are given\n", "students = {\"Max\": 4, \"Anna\": 5, \"Luis\": 5.5, \"Sophie\": 5, \"Tom\": 6, \"Bob\": 3.5}\n", "\n", "# your code here\n", "grades = {4:\"E\", 4.5:\"D\", 5:\"C\", 5.5:\"B\", 6:\"A\"}\n", "\n", "for name, vote in students.items():\n", " if vote < 4:\n", " ins = \"F\"\n", "\n", " else:\n", " ins = grades.get(vote, \"F\")\n", "\n", " print(f\"{name}: {ins}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 2nd task - convert sensor values \n", "\n", "Temperature and humidity values ​​were measured by 5 sensors in rel-% and °F and saved in a dictionary. Write a program that converts the temperature of this dictionary into °C and saves it in the JSON file sensor_cels.json. Then read the file back and output the values ​​to the console using pretty print." ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Sensor1': {'hum': 40, 'temp': 25.0},\n", " 'Sensor2': {'hum': 50, 'temp': 30.5},\n", " 'Sensor3': {'hum': 45, 'temp': 27.8},\n", " 'Sensor4': {'hum': 55, 'temp': 22.1},\n", " 'Sensor5': {'hum': 60, 'temp': 35.6}}\n" ] } ], "source": [ "# read sensor values:\n", "import json\n", "import pprint\n", "\n", "sensor_values = {\n", " \"Sensor1\": {\"temp\": 77.0, \"hum\": 40},\n", " \"Sensor2\": {\"temp\": 86.9, \"hum\": 50},\n", " \"Sensor3\": {\"temp\": 82.04, \"hum\": 45},\n", " \"Sensor4\": {\"temp\": 71.78, \"hum\": 55},\n", " \"Sensor5\": {\"temp\": 96.08, \"hum\": 60}\n", "}\n", "\n", "sensor_val_celsius = {}\n", "\n", "# your code here\n", "for sens, values in sensor_values.items():\n", " temp_c = (values[\"temp\"] - 32) * (5/9)\n", " sensor_val_celsius[sens] = {\n", " \"temp\": round(temp_c, 2),\n", " \"hum\": values[\"hum\"]\n", " }\n", "\n", "with open(\"sensor_cels.json\", \"w\") as json_file:\n", " json.dump(sensor_val_celsius, json_file, indent=4)\n", "\n", "with open(\"sensor_cels.json\", \"r\") as json_file:\n", " loaded_data = json.load(json_file)\n", "\n", "pprint.pprint(loaded_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## 3. Task - Morse code \n", "\n", "The aim is to create a Morse code from a text consisting of several words (separated by strokes, without punctuation marks). The Morse code characters are to be output on the console with two strokes separated. A \"|\" is used to separate the words.\n", "\n", "Tip: First, all words should be written in capital letters in a list (split()). The list is iterated through, with each letter of the word being iterated through (loop in a loop). To output to the console without writing to a new line, the print function can be called with the parameter **end** e.g. print(\".-..\", end=\" \")\n", "\n", "Example: Entering \"hello World\" results in:\n", "``` \n", ".... . .-.. .-.. --- | .-- --- .-. .-.. -.. |\n", "```" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".... . .-.. .-.. --- | .-- --- .-. .-.. -.. |\n" ] } ], "source": [ "# given dictionary with morsecode\n", "morsecode = {'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..'}\n", "\n", "text = input(\"Sentence: \")\n", "\n", "# your code here\n", "text = text.upper()\n", "words = text.split()\n", "\n", "for i, word in enumerate(words):\n", " for letter in word:\n", " print(morsecode.get(letter, \"\"), end=\" \")\n", " \n", " if i < len(words) - 1:\n", " print(\"|\", end=\" \")\n", "\n", "print(\"|\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "## Task 4 - Telephone key assignment \n", "Standard telephone keypads contain the digits 0 to 9. The digits 2 to 9 are each assigned letters, as shown in the following table:\n", "\n", "| Number | Letters |\n", "| ------ | ---------- |\n", "| 2 | A B C |\n", "| 3 | D E F |\n", "| 4 | G H I |\n", "| 5 | J K L |\n", "| 6 | M N O |\n", "| 7 | P Q R S |\n", "| 8 | T U V |\n", "| 9 | W X Y Z |\n", "\n", "Many people have difficulty remembering phone numbers, so they use the correspondence between digits and letters to develop seven-letter words (or phrases) that correspond to their phone numbers. For example, a person might use the word \"NUMBERS\" to encode the phone number 686 23 77.\n", "\n", "Suppose you want to find the phone number that matches a seven-digit word.\n", "Write a script that displays the phone number for the given seven-letter string on the screen.\n", "\n", "\n", "*Help given: Usage of a list with the letter groups as a data structure*" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Phone number: 6862377\n" ] } ], "source": [ "\"\"\"Word or Phrase to Phone-Number Generator.\n", "\n", " precondition: The 7 character word may only consist of ascii letters\n", "\"\"\"\n", "\n", "\n", "letters = ['', '', 'ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']\n", "\n", "# your code here\n", "word = input(\"Enter a 7-letter word or phrase: \").upper()\n", "phone_number = ''\n", "\n", "for letter in word:\n", " for i, group in enumerate(letters):\n", " if letter in group:\n", " phone_number += str(i)\n", " break \n", "\n", "print(\"Phone number:\", phone_number.strip())" ] }, { "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 }