{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f09b762d-ce55-4e77-9676-f089ba3ac5f1",
   "metadata": {},
   "source": [
    "# Python 101, Klausur 2026-01-10, 15:30 Uhr\n",
    "\n",
    "Prof. Dr. Johannes Busse\n",
    "\n",
    "(Aufgabengruppe b)\n",
    "\n",
    "ACHTUNG: Dieses Notebook ist ein erlaubtes Hilfsmittel. Es muss nicht benutzt werden, aber die Benutzung könnte helfen, die Papierklausur besser zu machen. Bewertungsrelevant ist ausschließlich die zugehörige Papierklausur."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d50b789-4602-4c9b-810d-53a134ca048b",
   "metadata": {},
   "source": [
    "# Aufgabe 1: Benzinpreise (b)\n",
    "\n",
    "gegeben: Wir erheben Benzinpreise in verschiedenen Regionen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "039bd2a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# m ... Preis für 1l Benzin in Cent -- leider als String gegeben\n",
    "m = [ '180', '183', '177', '164', '167', '167']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a49416f6",
   "metadata": {},
   "source": [
    "gesucht: durchschnittlicher Benzinpreis `schnitt`, billigster Preis `billig`, teuerster Preis `teuer`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6675ac7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ergebnis-Variablen definieren\n",
    "schnitt, billig, teuer = 0,0,0\n",
    "\n",
    "# hier Ihre Programmierung\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c292483e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'schnitt=173.0, billig=164, teuer=183'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "ergebnis = f\"{schnitt=}, {billig=}, {teuer=}\"\n",
    "ergebnis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d85c5297",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "ergebnis == 'schnitt=173.0, billig=164, teuer=183'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d7325c1",
   "metadata": {},
   "source": [
    "# Aufgabe 2: Vokale im String (b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcf924dc",
   "metadata": {},
   "source": [
    "gegeben: Ein String `s`, Beispiel:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "96373533-7256-4ab4-abcb-4bb95d83e9c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"Hallo Erika Musterfrau!\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e577c9f4",
   "metadata": {},
   "source": [
    "gesucht: Wie viele Vokale (\"aeiou\") enthält der String?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c61da5fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "s2 = {}\n",
    "vokale = \"aeiou\"\n",
    "\n",
    "# Hier Ihre Lösung:\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b9e0c232",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f8c16089",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "s2 == 8"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23bed24c",
   "metadata": {},
   "source": [
    "# Aufgabe 3: Wechselsumme (b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0e37237",
   "metadata": {},
   "source": [
    "gegeben: Eine Notebook-Zelle, die aus einer Integer-Zahl die Wechselsumme (alternierende Quersumme) berechnte:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2bbe70e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zahl = 12345\n",
    "wechselsumme = sum( int(ziffer) * (-1)**i for i, ziffer in enumerate(f\"{zahl}\" ))\n",
    "wechselsumme"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f307b8c",
   "metadata": {},
   "source": [
    "**Teil 1** gesucht: Wir benötigen eine Funktion `wechselsumme(x)`, die diese Berechnung anstellt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8b257061",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ihre Programmierung\n",
    "def wechselsumme(x):\n",
    "    return sum( int(z) * (-1)**i for i, z in enumerate(f\"{x}\") )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "56bb530f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "wechselsumme(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "01ab822c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "wechselsumme(12345) == 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2160711",
   "metadata": {},
   "source": [
    "**Teil2** gegeben: Eine Liste `l` von Zahlen:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b062b75",
   "metadata": {},
   "outputs": [],
   "source": [
    "l = [ 123, 2345, 34567 ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b5f8953",
   "metadata": {},
   "source": [
    "Gesucht: eine Liste `q`, die die Wechelsumme jeder Zahl in `l` enthält."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b7efd61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Ellipsis"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Variable anlegen\n",
    "w = []\n",
    "\n",
    "# Ihre Programmierung\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e76bfaf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, -2, 5]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d1c0a332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "w == [2, -2, 5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36d74a68",
   "metadata": {},
   "source": [
    "# Aufgabe 4: Familie 2 CSV (b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31037f93",
   "metadata": {},
   "source": [
    "gegeben: Eine Tabelle von Kind, Vater, Mutter zeilenweiser Darstellung."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "95888209",
   "metadata": {},
   "outputs": [],
   "source": [
    "werte = [\n",
    "    [ \"Kind\", \"Vater\", \"Mutter\" ],\n",
    "    ['Kain', 'Adam', 'Eva'],\n",
    "    ['Ismail', 'Ibrahim', 'Hadschar'],\n",
    "    ['Isaak', 'Abraham', 'Sara'] ] "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c46a99f",
   "metadata": {},
   "source": [
    "gesucht: ein einziger String, der diese Tabelle in Form einer CSV-Datei repräsentiert."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a8b23312",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variable definieren\n",
    "csv = \"\" \n",
    "\n",
    "# Ihre Lösung\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "57d6a624",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Kind;Vater;Mutter\n",
      "Kain;Adam;Eva\n",
      "Ismail;Ibrahim;Hadschar\n",
      "Isaak;Abraham;Sara\n"
     ]
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "print(csv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "eba4307c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "csv == \"\"\"Kind;Vater;Mutter\n",
    "Kain;Adam;Eva\n",
    "Ismail;Ibrahim;Hadschar\n",
    "Isaak;Abraham;Sara\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e1863da",
   "metadata": {},
   "source": [
    "# Aufgabe 5: CSV 2 Messwerte (b)\n",
    "\n",
    "gegeben: In einer CSV-Datei sind Messwerte gegeben (z.B. Geschlecht, Körpergröße, Alter). Wenn wir die Datei einlesen erhalten wir folgenden String `data`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "7fffecb4-c452-40da-a9d4-6399bad07b96",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = \"\"\"m;180;21\n",
    "m;183;27\n",
    "m;177;23\n",
    "f;164;19\n",
    "f;167;25\n",
    "f;167;24\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dcc9f98-1e36-40ef-859f-a46e126e4a5a",
   "metadata": {},
   "source": [
    "Gesucht: Wir benötigen eine Tabelle in der Form einer Liste von Listen, zeilenweise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1917efb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variable definieren\n",
    "table = []\n",
    "\n",
    "# Ihre Programmierung\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "477ebf3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['m', '180', '21'],\n",
       " ['m', '183', '27'],\n",
       " ['m', '177', '23'],\n",
       " ['f', '164', '19'],\n",
       " ['f', '167', '25'],\n",
       " ['f', '167', '24']]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnise\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "73a51266",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "table == [['m', '180', '21'],\n",
    " ['m', '183', '27'],\n",
    " ['m', '177', '23'],\n",
    " ['f', '164', '19'],\n",
    " ['f', '167', '25'],\n",
    " ['f', '167', '24']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08bd2e31",
   "metadata": {},
   "source": [
    "# Aufgabe 6: Synonyme segmentieren (b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61f085fb",
   "metadata": {},
   "source": [
    "gegeben ist eine Datei von Synonym-Gruppen:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "2f008f73",
   "metadata": {},
   "outputs": [],
   "source": [
    "syngroups = \"\"\"Geld Mäuse Zaster Schotter Kohle\n",
    "Stuhl Hocker Schemel\n",
    "Hallo Privjet\n",
    "Bye Ade\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b7e8347",
   "metadata": {},
   "source": [
    "gesucht: Zähle Anzahl der Synonymgruppen (Zeilen) und Wörter insgesamt."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "e231f7ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Variablen definieren\n",
    "num_zeilen, num_woerter = 0, 0\n",
    "\n",
    "# hier Ihre Programmierung\n",
    "... # \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "bc0ead36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'num_zeilen=4, num_woerter=12'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihr Ergebnis\n",
    "f\"{num_zeilen=}, {num_woerter=}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "51d36a0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "num_zeilen ==4,  num_woerter == 12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e82d9133",
   "metadata": {},
   "source": [
    "# Aufgabe 7: String to Number (b)\n",
    "\n",
    "gegeben: Ein Dict von Name und Geburtsjahr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "2bfe24ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "# g ... Geburtstage\n",
    "g = { \"Anna\": \"2005\", \"Ben\": \"2004\", \"Charly\": \"2005\" }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f80acb",
   "metadata": {},
   "source": [
    "gesucht: Eine etwas höherwertige Repräsention der Geburtstage als ein Dictionary von Zahlenwerten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "1c306662",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ergebnis-Variable definieren\n",
    "g1 = {}\n",
    "\n",
    "# Hier die Lösung programmieren\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "f8c16826",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Anna': 2005, 'Ben': 2004, 'Charly': 2005}\n"
     ]
    }
   ],
   "source": [
    "# Ihre Lösung\n",
    "print(g1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ac26ada7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "g1 =={'Anna': 2005, 'Ben': 2004, 'Charly': 2005}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "194276b6",
   "metadata": {},
   "source": [
    "# Aufgabe 8: Geburtstagsdict (a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e489f212",
   "metadata": {},
   "source": [
    "gegeben: Ein Dict von Name und Geburtsdatum. Das Geburtsdatum ist als Liste von Strings in der Form `[ jjjj, mm, tt ]` gegeben."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "cb1104b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# g2 ... Geburtstage\n",
    "g2 = {'Anna': ['2005', '02', '10'], 'Ben': ['2004', '02', '23'], 'Charly': ['2005', '02', '10']}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffee4bfe",
   "metadata": {},
   "source": [
    "gesucht: Eine noch höherwertige Repräsention der Geburtstage als ein Dictionary von Dictionaries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "59810058",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ergebnis-Variable definieren\n",
    "g3 = {}\n",
    "\n",
    "# Hier die Lösung programmieren\n",
    "... # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6cc0642e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Anna': {'jahr': '2005', 'monat': '02', 'tag': '10'},\n",
       " 'Ben': {'jahr': '2004', 'monat': '02', 'tag': '23'},\n",
       " 'Charly': {'jahr': '2005', 'monat': '02', 'tag': '10'}}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihre Lösung\n",
    "g3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "ea3d97e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test \n",
    "g3 == {'Anna': {'jahr': '2005', 'monat': '02', 'tag': '10'},\n",
    " 'Ben': {'jahr': '2004', 'monat': '02', 'tag': '23'},\n",
    " 'Charly': {'jahr': '2005', 'monat': '02', 'tag': '10'}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6f6a169-b472-4087-8037-669c1e8fddd9",
   "metadata": {},
   "source": [
    "# Aufgabe 9: Geburtstage im gleichen Jahr (a, b)\n",
    "\n",
    "gegeben: Eine (z.B. Excel- oder CSV-)Tabelle, die (z.B. mit Pandas) bereits eingelesen und in ein Dict aus Dicts überführt wurde:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "2fc190ba-4623-4e9d-8907-c7e0ac7a94a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# g3 ... Geburtstage als Dict von Dicts\n",
    "g3 = {'Anna': {'jahr': '2005', 'monat': '02', 'tag': '10'},\n",
    " 'Ben': {'jahr': '2004', 'monat': '02', 'tag': '23'},\n",
    " 'Charly': {'jahr': '2005', 'monat': '02', 'tag': '10'}}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d16cd41-e231-4164-b24c-42f33855bb73",
   "metadata": {},
   "source": [
    "Wir haben hier ja eine Abbildung von Person nach Geburtstag. \n",
    "\n",
    "gesucht:\n",
    "* Wir wollen zu jedem Jahr wissen, welche Personen in diesem Jahr Geburtstag haben. (Das ist eine Art Umkehr-Abbildung von `g3`).\n",
    "\n",
    "Die Datenstruktur des Ergebnisses soll das Dict `g3_invers` sein, das zu jedem Jahr eine Menge von Personen enthält, siehe unten \"Test\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "c3f80eef-c517-4da7-a7c8-81294be27faa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ergebnis-Variable definieren\n",
    "g3_invers = {}\n",
    "\n",
    "# hier Ihre Lösung\n",
    "... # \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "32f2733e-e48e-4652-bb70-e9f927c86b86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2005': {'Anna', 'Charly'}, '2004': {'Ben'}}"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Ihre Lösung\n",
    "g3_invers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "7f1a16d3-9018-4adf-b9cd-6db5c0405959",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test\n",
    "g3_invers == {'2005': {'Anna', 'Charly'}, '2004': {'Ben'}}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 5
}
