123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true,
- "pycharm": {
- "name": "#%% md\n"
- },
- "slideshow": {
- "slide_type": "slide"
- }
- },
- "source": [
- "_Aufgabe 1_\n",
- "\n",
- "Schreiben Sie eine Funktion <code>schnittmenge</code>, 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 <code>pascal</code>, der eine Liste <code>z</code> von Integer-Zahlen \n",
- "übergeben wird. Sei <code>n</code> die Länge dieser Liste. \n",
- "Das Ergebnis soll eine neue Liste sein mit folgenden Einträgen\n",
- "\n",
- "Falls <code>n == 0</code>:\n",
- "\n",
- " <code>[ 1 ]</code>\n",
- "\n",
- "Falls <code>n == 1</code>:\n",
- "\n",
- " <code>[ 1 , 1 ]</code>\n",
- "\n",
- "Sonst:\n",
- "\n",
- " <code>[ 1 , z[0]+z[1] , z[1]+z[2] , ... , z[n-2]+z[n-1] , 1 ]</code>"
- ]
- },
- {
- "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
- }
|