{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"pycharm": {
"name": "#%% md\n"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"_Aufgabe 1_\n",
"\n",
"Schreiben Sie eine Funktion schnittmenge
, die zwei Listen als Parameter erwartet\n",
"und eine Liste zurückgibt, die nur die Elemente beinhaltet, die in beiden übergebenen Listen enthalten waren."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Todo: Hier die Funktion anlegen\n",
"print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": false,
"name": "#%%\n"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def schnittmenge(liste1, liste2):\n",
" ergebnis = []\n",
" for element in liste2:\n",
" if element in liste1:\n",
" ergebnis.append(element)\n",
" return ergebnis\n",
"\n",
"print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"_Aufgabe 2_\n",
"\n",
"Schreiben Sie eine Funktion pascal
, der eine Liste z
von Integer-Zahlen \n",
"übergeben wird. Sei n
die Länge dieser Liste. \n",
"Das Ergebnis soll eine neue Liste sein mit folgenden Einträgen\n",
"\n",
"Falls n == 0
:\n",
"\n",
" [ 1 ]
\n",
"\n",
"Falls n == 1
:\n",
"\n",
" [ 1 , 1 ]
\n",
"\n",
"Sonst:\n",
"\n",
" [ 1 , z[0]+z[1] , z[1]+z[2] , ... , z[n-2]+z[n-1] , 1 ]
"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# Todo: Hier die Funktion anlegen\n",
"\n",
"print(pascal([]))\n",
"print(pascal([5]))\n",
"print(pascal([5, 2, 7, 3]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"def pascal(z):\n",
" erg = [1]\n",
" erg.extend([a+b for a, b in zip(z[:-1], z[1:])])\n",
" if len(z) > 0:\n",
" erg.append(1)\n",
" return erg\n",
"\n",
"print(pascal([]))\n",
"print(pascal([5]))\n",
"print(pascal([5, 2, 7, 3]))"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.9.9"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}