You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

042a Uebung.ipynb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "collapsed": true,
  7. "pycharm": {
  8. "name": "#%% md\n"
  9. },
  10. "slideshow": {
  11. "slide_type": "slide"
  12. }
  13. },
  14. "source": [
  15. "_Aufgabe 1_\n",
  16. "\n",
  17. "Schreiben Sie eine Funktion <code>schnittmenge</code>, die zwei Listen als Parameter erwartet\n",
  18. "und eine Liste zurückgibt, die nur die Elemente beinhaltet, die in beiden übergebenen Listen enthalten waren."
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "# Todo: Hier die Funktion anlegen\n",
  28. "print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))"
  29. ]
  30. },
  31. {
  32. "cell_type": "code",
  33. "execution_count": null,
  34. "metadata": {
  35. "pycharm": {
  36. "is_executing": false,
  37. "name": "#%%\n"
  38. },
  39. "slideshow": {
  40. "slide_type": "slide"
  41. }
  42. },
  43. "outputs": [],
  44. "source": [
  45. "def schnittmenge(liste1, liste2):\n",
  46. " ergebnis = []\n",
  47. " for element in liste2:\n",
  48. " if element in liste1:\n",
  49. " ergebnis.append(element)\n",
  50. " return ergebnis\n",
  51. "\n",
  52. "print(schnittmenge([1, 2, 3, 10], [3, 4, 1]))"
  53. ]
  54. },
  55. {
  56. "cell_type": "markdown",
  57. "metadata": {
  58. "slideshow": {
  59. "slide_type": "slide"
  60. }
  61. },
  62. "source": [
  63. "_Aufgabe 2_\n",
  64. "\n",
  65. "Schreiben Sie eine Funktion <code>pascal</code>, der eine Liste <code>z</code> von Integer-Zahlen \n",
  66. "übergeben wird. Sei <code>n</code> die Länge dieser Liste. \n",
  67. "Das Ergebnis soll eine neue Liste sein mit folgenden Einträgen\n",
  68. "\n",
  69. "Falls <code>n == 0</code>:\n",
  70. "\n",
  71. "&nbsp;<code>[ 1 ]</code>\n",
  72. "\n",
  73. "Falls <code>n == 1</code>:\n",
  74. "\n",
  75. "&nbsp;<code>[ 1 , 1 ]</code>\n",
  76. "\n",
  77. "Sonst:\n",
  78. "\n",
  79. "&nbsp;<code>[ 1 , z[0]+z[1] , z[1]+z[2] , ... , z[n-2]+z[n-1] , 1 ]</code>"
  80. ]
  81. },
  82. {
  83. "cell_type": "code",
  84. "execution_count": null,
  85. "metadata": {
  86. "slideshow": {
  87. "slide_type": "slide"
  88. }
  89. },
  90. "outputs": [],
  91. "source": [
  92. "# Todo: Hier die Funktion anlegen\n",
  93. "\n",
  94. "print(pascal([]))\n",
  95. "print(pascal([5]))\n",
  96. "print(pascal([5, 2, 7, 3]))"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": null,
  102. "metadata": {
  103. "slideshow": {
  104. "slide_type": "subslide"
  105. }
  106. },
  107. "outputs": [],
  108. "source": [
  109. "def pascal(z):\n",
  110. " erg = [1]\n",
  111. " erg.extend([a+b for a, b in zip(z[:-1], z[1:])])\n",
  112. " if len(z) > 0:\n",
  113. " erg.append(1)\n",
  114. " return erg\n",
  115. "\n",
  116. "print(pascal([]))\n",
  117. "print(pascal([5]))\n",
  118. "print(pascal([5, 2, 7, 3]))"
  119. ]
  120. }
  121. ],
  122. "metadata": {
  123. "celltoolbar": "Slideshow",
  124. "kernelspec": {
  125. "display_name": "Python 3 (ipykernel)",
  126. "language": "python",
  127. "name": "python3"
  128. },
  129. "language_info": {
  130. "codemirror_mode": {
  131. "name": "ipython",
  132. "version": 3
  133. },
  134. "file_extension": ".py",
  135. "mimetype": "text/x-python",
  136. "name": "python",
  137. "nbconvert_exporter": "python",
  138. "pygments_lexer": "ipython3",
  139. "version": "3.9.9"
  140. },
  141. "pycharm": {
  142. "stem_cell": {
  143. "cell_type": "raw",
  144. "metadata": {
  145. "collapsed": false
  146. },
  147. "source": []
  148. }
  149. }
  150. },
  151. "nbformat": 4,
  152. "nbformat_minor": 1
  153. }