From 418c5ad6d8e078dbda921033b3f75713d0e3ab4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20St=C3=A4ding?= Date: Thu, 5 Oct 2023 02:20:37 +0200 Subject: [PATCH] Almost finished with 05_Listen --- lecture/05_Listen/05_Listen.tex | 254 +++++++++--------- .../05_Listen/listings/OutofBounce_Listing.py | 8 - .../05_Listen/listings/destructure_list.kts | 7 + .../05_Listen/listings/destructure_string.kts | 7 + lecture/05_Listen/listings/foreach_Listing.py | 3 - .../05_Listen/listings/foreach_Listing2.py | 3 - .../05_Listen/listings/in_operator_list.kts | 5 + .../05_Listen/listings/in_operator_string.kts | 3 + lecture/05_Listen/listings/intervalSlice.py | 7 - .../listings/intervalSlice_no_solution.py | 6 - lecture/05_Listen/listings/list_iteration.kts | 5 + lecture/05_Listen/listings/list_length.py | 3 - .../listings/list_length_no_solution.py | 3 - .../05_Listen/listings/list_mutability.kts | 4 + lecture/05_Listen/listings/list_pop.py | 5 - lecture/05_Listen/listings/list_slice.kts | 4 + .../05_Listen/listings/list_slice_list.kts | 4 + .../listings/list_slice_list_no_solution.kts | 3 + .../listings/list_slice_no_solution.kts | 3 + lecture/05_Listen/listings/list_syntax.py | 13 - .../listings/list_syntax_immutable_access.kts | 2 + .../listings/list_syntax_immutable_basic.kts | 2 + .../list_syntax_immutable_no_type.kts | 2 + lecture/05_Listen/listings/listcopy1.kts | 8 + lecture/05_Listen/listings/listcopy1.py | 8 - lecture/05_Listen/listings/listcopy2.kts | 9 + lecture/05_Listen/listings/listcopy2.py | 9 - .../05_Listen/listings/out_of_bounds_code.kts | 5 + .../listings/out_of_bounds_error.txt | 3 + lecture/05_Listen/listings/partlySlice.py | 7 - .../listings/partlySlice_no_solution.py | 6 - lecture/05_Listen/listings/range_char_example | 0 .../listings/range_char_example_minus.kts | 3 + .../listings/range_char_example_plus.kts | 3 + lecture/05_Listen/listings/simpleSlice.py | 4 - .../listings/simpleSlice_no_solution.py | 3 - 36 files changed, 206 insertions(+), 218 deletions(-) delete mode 100644 lecture/05_Listen/listings/OutofBounce_Listing.py create mode 100644 lecture/05_Listen/listings/destructure_list.kts create mode 100644 lecture/05_Listen/listings/destructure_string.kts delete mode 100644 lecture/05_Listen/listings/foreach_Listing.py delete mode 100644 lecture/05_Listen/listings/foreach_Listing2.py create mode 100644 lecture/05_Listen/listings/in_operator_list.kts create mode 100644 lecture/05_Listen/listings/in_operator_string.kts delete mode 100644 lecture/05_Listen/listings/intervalSlice.py delete mode 100644 lecture/05_Listen/listings/intervalSlice_no_solution.py create mode 100644 lecture/05_Listen/listings/list_iteration.kts delete mode 100644 lecture/05_Listen/listings/list_length.py delete mode 100644 lecture/05_Listen/listings/list_length_no_solution.py create mode 100644 lecture/05_Listen/listings/list_mutability.kts delete mode 100644 lecture/05_Listen/listings/list_pop.py create mode 100644 lecture/05_Listen/listings/list_slice.kts create mode 100644 lecture/05_Listen/listings/list_slice_list.kts create mode 100644 lecture/05_Listen/listings/list_slice_list_no_solution.kts create mode 100644 lecture/05_Listen/listings/list_slice_no_solution.kts delete mode 100644 lecture/05_Listen/listings/list_syntax.py create mode 100644 lecture/05_Listen/listings/listcopy1.kts delete mode 100644 lecture/05_Listen/listings/listcopy1.py create mode 100644 lecture/05_Listen/listings/listcopy2.kts delete mode 100644 lecture/05_Listen/listings/listcopy2.py create mode 100644 lecture/05_Listen/listings/out_of_bounds_code.kts create mode 100644 lecture/05_Listen/listings/out_of_bounds_error.txt delete mode 100644 lecture/05_Listen/listings/partlySlice.py delete mode 100644 lecture/05_Listen/listings/partlySlice_no_solution.py create mode 100644 lecture/05_Listen/listings/range_char_example create mode 100644 lecture/05_Listen/listings/range_char_example_minus.kts create mode 100644 lecture/05_Listen/listings/range_char_example_plus.kts delete mode 100644 lecture/05_Listen/listings/simpleSlice.py delete mode 100644 lecture/05_Listen/listings/simpleSlice_no_solution.py diff --git a/lecture/05_Listen/05_Listen.tex b/lecture/05_Listen/05_Listen.tex index 4c1bf87..45c4d71 100644 --- a/lecture/05_Listen/05_Listen.tex +++ b/lecture/05_Listen/05_Listen.tex @@ -117,74 +117,107 @@ \section{Syntax}\label{sec:syntax} \end{tikzpicture} \end{frame} -\section{Zugriffsfehler}\label{sec:zugriffsfehler} \begin{frame} \slidehead - \pythonfile{listings/OutofBounce_Listing.py} + \kotlinfile{listings/list_syntax_immutable_access.kts} +\end{frame} + +\begin{frame} + \slidehead + \kotlinfile{listings/list_syntax_immutable_ends.kts} +\end{frame} + +\livecoding + +\section{Iterieren}\label{sec:iterieren} +\subsection{Zugriffsfehler}\label{subsec:zugriffsfehler} +\begin{frame}[fragile] + \slidehead + \kotlinfile{listings/out_of_bounds_code.kts} + \pause + \begin{commandshell}[fontsize=\footnotesize][minted language=text,top=0cm,bottom=0cm] + java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 + at java.base/java.util.Arrays$ArrayList.get(Arrays.java:4165) + at Foo.(foo.kts:4) + \end{commandshell} Bedeutung: \begin{itemize} - \item In Zeile 3 der Datei \texttt{dateiName.py} wurde ein Index angefordert, der nicht innerhalb der Liste liegt + \item In Zeile 4 der Datei \texttt{foo.kts} wurde ein Index angefordert, der nicht innerhalb der Liste liegt \end{itemize} +\end{frame} +\subsection{Mit For-Schleife} +\begin{frame} + \slidehead + \kotlinfile{listings/list_iteration.kts} + \pause + \begin{block}{Hinweis} + Alles, was \enquote{iterable} ist, kann nach dem \kotlininline{in} (auch \kotlininline{String}!) + \end{block} \end{frame} -%\nextvid{Nützliche Listen-Funktionen}{Listen Beispiele} +\livecoding -%Deckblatt -%\titlegraphic*{\includegraphics{media/cern.jpg}} -% Photo by Samuel Zeller on Unsplash. Source: https://unsplash.com/photos/JuFcQxgCXwA Licence is completely free to do anything: "More precisely, Unsplash grants you an irrevocable, nonexclusive, worldwide copyright license to download, copy, modify, distribute, perform, and use photos from Unsplash for free, including for commercial purposes, without permission from or attributing the photographer or Unsplash." -% -\subtitle{Listen} -\subtitle{Kapitel 5: ganz viele Variablen in einer} -% \titlegraphic{ -% \begin{columns} -% \begin{column}{6cm} -% \vspace{18mm} -% \begin{center} -% {\huge Listen-Funktionen} -% \end{center} -% \end{column} -% \end{columns}} -% \maketitle +\section{In-Operator als Abfrage}\label{sec:in-operator-als-abfrage} -\section{Nützliche Listen-Funktionen}\label{sec:nutzliche-listen-funktionen} \begin{frame} \slidehead - \begin{itemize} - \item Länge einer Liste: - \only<1>{\pythonfile{listings/list_length_no_solution.py}} - \only<2>{\pythonfile{listings/list_length.py}} - \end{itemize} + Der \kotlininline{in}-Operator kann auch überprüfen, ob sich ein Wert in der Liste befindet + \pause + \kotlinfile{listings/in_operator_list.kts} + \pause + \begin{block}{Hinweis} + Alles, was \enquote{iterable} ist, kann nach dem \kotlininline{in} (auch \kotlininline{String}!) + \end{block} + \pause \end{frame} \begin{frame} \slidehead - \begin{itemize} - \item Element an Liste anhängen: - \pythonfile{listings/list_append.py} - \pause - \item Element aus Liste entfernen: - \pythonfile{listings/list_pop.py} - \end{itemize} + \kotlinfile{listings/in_operator_string.kts} + \pause + \vspace{2em} + \centering + \enquote{Das F in TU Darmstadt steht für Freizeit} \end{frame} \livecoding -%\nextvid{Slicing}{Listen-Funktionen Beispiel} -% -\subtitle{Kapitel 5: ganz viele Variablen in einer} +\section{Destructuring}\label{sec:destructuring} +\begin{frame} + \slidehead + Mit destructuring können die ersten $n$ Werte aus einer Liste direkt in Variablen gespeichert werden + \pause + \kotlinfile[][top=0cm,bottom=0cm]{listings/destructure_list.kts} + \pause + \begin{block}{Hinweis} + Das geht auch mit einem \kotlininline{String} (fast) + \end{block} +\end{frame} -% %Deckblatt -% \titlegraphic{ -% \begin{columns} -% \begin{column}{6cm} -% \vspace{18mm} -% \begin{center} -% {\huge Slicing} -% \end{center} -% \end{column} -% \end{columns}} -% \maketitle +\begin{frame} + \slidehead + \kotlinfile[][top=0cm,bottom=0cm]{listings/destructure_string.kts} +\end{frame} + +\livecoding + +\section{Mutability} +\begin{frame} + \slidehead + \begin{itemize}[<+->] + \item In Kotlin, ist fast alles \enquote{immutable-by-default} + \item Die Liste, die von \kotlininline{listOf()} erstellt wird, kann nicht modifiziert werden + \item Mit \kotlininline{mutableListOf()} kann eine Liste erstellt werden, die modifizierbar ist + \end{itemize} + \onslide<4-> + \kotlinfile{listings/list_mutability.kts} + \begin{block}{Frage am Rande} + Wie unterscheidet sich die \enquote{nicht-Veränderbarkeit} von \kotlininline{val} und \kotlininline{listOf()}? + \end{block} +\end{frame} + +\livecoding \section{Listen-Slicing} \begin{frame} @@ -192,122 +225,83 @@ \section{Listen-Slicing} \begin{itemize} \item Listen können mittels Slicing zugeschnitten werden \pause - \item Dazu wird \textbf{[a:b]} hinter die Liste geschrieben + \item Dazu wird \kotlininline{.slice(a until b)} hinter die Liste geschrieben \item Dabei ist \textbf{a} der Anfangsindex, und \textbf{b} der Endindex \pause \item Wichtig: Der Wert an Stelle \textbf{b} wird nicht mit kopiert - \item Dies funktioniert auch mit Strings! + \item Dies funktioniert auch mit \kotlininline{String}! \end{itemize} \pause \begin{block}{Beispiel:} - \only<4>{\pythonfile{listings/simpleSlice_no_solution.py}} - \only<5>{\pythonfile{listings/simpleSlice.py}} + \only<4>{\kotlinfile{listings/list_slice_no_solution.kts}} + \only<5>{\kotlinfile{listings/list_slice.kts}} \end{block} \end{frame} \begin{frame} \slidehead - \begin{itemize} - \item Es ist möglich einen oder beide der Parameter wegzulassen - \pause - \item \textbf{[a:]} gibt alle Elemente ab \textbf{a} zurück - \pause - \item \textbf{[:b]} gibt alle Elemente bis (exklusive) \textbf{b} zurück - \pause - \item \textbf{[:]} gibt alle Elemente zurück - \end{itemize} - \pause - \vspace{-0.1cm} - \begin{block}{Beispiel:} - \only<5>{\pythonfile{listings/partlySlice_no_solution.py}} - \only<6>{\pythonfile{listings/partlySlice.py}} - \end{block} + Es ist möglich einzelne Indizes anzugeben + \kotlinfile{listings/list_slice_list.kts} \end{frame} +\livecoding + +\section{Ranges} +\subsection{Deep Dive} \begin{frame} \slidehead + Ranges haben einen Start- und Endwert \begin{itemize} - \item Es ist auch möglich in einem Intervall zu slicen - \pause - \item Dazu wird ein dritter Parameter hinzugefügt: \textbf{[a:b:c]} gibt von \textbf{a} bis (exklusive) \textbf{b} jedes \textbf{c}-te Element aus - \pause - \item \textbf{[::-1]} gibt die Liste in umgekehrter Reihenfolge aus + \item<1-> \kotlininline{a until b} ist ein Range von \kotlininline{a} bis \kotlininline{b - 1} inklusive + \item<2-> \kotlininline{a .. b} ist ein Range von \kotlininline{a} bis \kotlininline{b} inklusive + \item<3-> \kotlininline{a downTo b} ist ein Range von \kotlininline{a} bis \kotlininline{b} inklusive, falls \kotlininline{a > b} + \item<4-> \kotlininline{a .. b step c} ist ein Range von \kotlininline{a} bis \kotlininline{b} inklusive, in sprüngen von \kotlininline{c} + \end{itemize} + \onslide<5-> + Ranges können auch zusammengesetzt werden + \begin{itemize} + \item<6-> \kotlininline{(a .. b) + (c .. d)} + \item<7-> \kotlininline{(a .. d) - (b .. c)} + \end{itemize} + \onslide<8-> + Ranges können in eine Liste umgewandelt werden + \begin{itemize} + \item<9-> \kotlininline{(a .. b).toList()} + \item<9-> \kotlininline{(a .. b).toMutableList()} \end{itemize} - \pause - \vspace{-0.1cm} - \begin{block}{Beispiel:} - \only<4>{\pythonfile{listings/intervalSlice_no_solution.py}} - \only<5>{\pythonfile{listings/intervalSlice.py}} - \end{block} \end{frame} -\livecoding - -%\nextvid{Foreach-Schleifen}{Slicing Beispiele} - -%Deckblatt -\subtitle{Kapitel 5: ganz viele Variablen in einer} -% \titlegraphic{ -% \begin{columns} -% \begin{column}{10cm} -% \vspace{1.5cm} -% \begin{center} -% {\huge Foreach-Schleifen} -% \end{center} -% \end{column} -% \end{columns}} -% \subtitle{Listen} -% \maketitle - -\section{For- vs. Foreach-Schleife} \begin{frame} \slidehead + Beispiel: Gebe die Bustaben von \enquote{a} bis \enquote{c} und von \enquote{x} bis \enquote{z} aus + \pause + \kotlinfile{listings/range_char_example_plus.kts} +\end{frame} - \begin{itemize} - \item Häufig soll durch eine bestimmte Liste iteriert werden: - \pythonfile{listings/foreach_Listing2.py} - \pause - - \item Deshalb Foreach-Schleife: - \pythonfile{listings/foreach_Listing.py} - \end{itemize} +\begin{frame} + \slidehead + Beispiel: Gebe alle Bustaben aus, außer die von \enquote{e} bis \enquote{m} + \pause + \kotlinfile{listings/range_char_example_minus.kts} \end{frame} \livecoding -%\nextvid{Listen kopieren}{Foreach Beispiel} - - -%Deckblatt -\subtitle{Kapitel 5: ganz viele Variablen in einer} - -% \titlegraphic{ -% \begin{columns} -% \begin{column}{6cm} -% \vspace{18mm} -% \begin{center} -% {\huge Listen Kopieren} -% \end{center} -% \end{column} -% \end{columns}} -% \maketitle - \section{Listen kopieren} \begin{frame} \slidehead - \pythonfile{listings/listcopy1.py} + \kotlinfile{listings/listcopy1.kts} \end{frame} - \subsection{Analyse} \begin{frame} \slidehead \vspace{2ex} \centering \begin{tikzpicture} - \node[draw, thick] at (-1,0) {\pythoninline{meineListe}}; - \node[draw, thick] at (1,0) {\pythoninline{kopie}}; + \node[draw, thick] at (-1,0) {\kotlininline{meineListe}}; + \node[draw, thick] at (1,0) {\kotlininline{kopie}}; \node[draw, thick, fill=lightpetrol] at (0,2) {Speicherbereich}; @@ -320,14 +314,14 @@ \subsection{Analyse} \item Bei der Zuweisung mit \textbf{=} wurde nur die Zieladresse kopiert \item Deshalb wird die gleiche Liste referenziert - \item Eine Änderung von \pythoninline{kopie} bewirkt eine Änderung in \pythoninline{meineListe} (und umgekehrt) + \item Eine Änderung von \kotlininline{kopie} bewirkt eine Änderung in \kotlininline{meineListe} (und umgekehrt) \end{itemize} \end{frame} \begin{frame} \slidehead - \pythonfile{listings/listcopy2.py} + \kotlinfile{listings/listcopy2.kts} \end{frame} \subsection*{Analyse} @@ -337,8 +331,8 @@ \subsection*{Analyse} \vspace{2ex} \centering \begin{tikzpicture} - \node[draw, thick] at (-2,0) {\pythoninline{meineListe}}; - \node[draw, thick] at (2,0) {\pythoninline{kopie}}; + \node[draw, thick] at (-2,0) {\kotlininline{meineListe}}; + \node[draw, thick] at (2,0) {\kotlininline{kopie}}; \node[draw, thick, fill=lightpetrol] at (-2,2) {Speicherbereich}; @@ -351,7 +345,7 @@ \subsection*{Analyse} \vspace{2ex} \begin{itemize} - \item \pythoninline{kopie} liegt nun in einem neuen, unabhängigen Speicherbereich + \item \kotlininline{kopie} liegt nun in einem neuen, unabhängigen Speicherbereich \end{itemize} \end{frame} diff --git a/lecture/05_Listen/listings/OutofBounce_Listing.py b/lecture/05_Listen/listings/OutofBounce_Listing.py deleted file mode 100644 index 156b150..0000000 --- a/lecture/05_Listen/listings/OutofBounce_Listing.py +++ /dev/null @@ -1,8 +0,0 @@ -liste = [1, 2, 3, 4] -for i in range(5): - print(liste[i]) - -# Traceback (most recent call last): -# File "dateiName.py", line 3, in -# print(liste[i]) -# IndexError: list index out of range diff --git a/lecture/05_Listen/listings/destructure_list.kts b/lecture/05_Listen/listings/destructure_list.kts new file mode 100644 index 0000000..a7a93a4 --- /dev/null +++ b/lecture/05_Listen/listings/destructure_list.kts @@ -0,0 +1,7 @@ +val myList = listOf('h', 'a', 'l', 'l', 'o') + +val (a, b, c) = myList + +println(a) +println(b) +println(c) diff --git a/lecture/05_Listen/listings/destructure_string.kts b/lecture/05_Listen/listings/destructure_string.kts new file mode 100644 index 0000000..56da187 --- /dev/null +++ b/lecture/05_Listen/listings/destructure_string.kts @@ -0,0 +1,7 @@ +val myString = "hallo".toList() + +val (a, b, c) = myString + +println(a) // h +println(b) // a +println(c) // l diff --git a/lecture/05_Listen/listings/foreach_Listing.py b/lecture/05_Listen/listings/foreach_Listing.py deleted file mode 100644 index 4cbcbdc..0000000 --- a/lecture/05_Listen/listings/foreach_Listing.py +++ /dev/null @@ -1,3 +0,0 @@ -alle_staedte = ["Toronto", "Stockholm", "Amsterdam"] -for stadt in alle_staedte: - print(stadt) diff --git a/lecture/05_Listen/listings/foreach_Listing2.py b/lecture/05_Listen/listings/foreach_Listing2.py deleted file mode 100644 index 5f4817e..0000000 --- a/lecture/05_Listen/listings/foreach_Listing2.py +++ /dev/null @@ -1,3 +0,0 @@ -alle_staedte = ["Toronto", "Stockholm", "Amsterdam"] -for i in range(3): - print(alle_staedte[i]) diff --git a/lecture/05_Listen/listings/in_operator_list.kts b/lecture/05_Listen/listings/in_operator_list.kts new file mode 100644 index 0000000..71202fa --- /dev/null +++ b/lecture/05_Listen/listings/in_operator_list.kts @@ -0,0 +1,5 @@ +val myList = listOf(42, 45, 1, 2, 5) + +if (45 in myList) { + println("Found!") +} diff --git a/lecture/05_Listen/listings/in_operator_string.kts b/lecture/05_Listen/listings/in_operator_string.kts new file mode 100644 index 0000000..7ae46ba --- /dev/null +++ b/lecture/05_Listen/listings/in_operator_string.kts @@ -0,0 +1,3 @@ +if ('F' in "TU Darmstadt") { + // ... +} diff --git a/lecture/05_Listen/listings/intervalSlice.py b/lecture/05_Listen/listings/intervalSlice.py deleted file mode 100644 index ec390cb..0000000 --- a/lecture/05_Listen/listings/intervalSlice.py +++ /dev/null @@ -1,7 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[1:4:2] -# sliced = ['Apfel', 'Käse'] - -sliced2 = liste[::3] -# sliced2 = ['Banane', 'Käse'] diff --git a/lecture/05_Listen/listings/intervalSlice_no_solution.py b/lecture/05_Listen/listings/intervalSlice_no_solution.py deleted file mode 100644 index 05357bd..0000000 --- a/lecture/05_Listen/listings/intervalSlice_no_solution.py +++ /dev/null @@ -1,6 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[1:4:2] - - -sliced2 = liste[::3] diff --git a/lecture/05_Listen/listings/list_iteration.kts b/lecture/05_Listen/listings/list_iteration.kts new file mode 100644 index 0000000..536dfd9 --- /dev/null +++ b/lecture/05_Listen/listings/list_iteration.kts @@ -0,0 +1,5 @@ +val myList = listOf(42, 45, 1, 2, 5) + +for (e in myList) { + println(e) +} diff --git a/lecture/05_Listen/listings/list_length.py b/lecture/05_Listen/listings/list_length.py deleted file mode 100644 index be74630..0000000 --- a/lecture/05_Listen/listings/list_length.py +++ /dev/null @@ -1,3 +0,0 @@ -meine_liste = [1, 2, 3, 4] -# Laenge ermitteln -laenge = len(meine_liste) # 4 diff --git a/lecture/05_Listen/listings/list_length_no_solution.py b/lecture/05_Listen/listings/list_length_no_solution.py deleted file mode 100644 index 02e7da3..0000000 --- a/lecture/05_Listen/listings/list_length_no_solution.py +++ /dev/null @@ -1,3 +0,0 @@ -meine_liste = [1, 2, 3, 4] -# Laenge ermitteln -laenge = len(meine_liste) diff --git a/lecture/05_Listen/listings/list_mutability.kts b/lecture/05_Listen/listings/list_mutability.kts new file mode 100644 index 0000000..fd81f31 --- /dev/null +++ b/lecture/05_Listen/listings/list_mutability.kts @@ -0,0 +1,4 @@ +// Read-Only +val normalList = listOf(1, 2, 3) +// Read-Write +val mutableList = mutableListOf(1, 2, 3) diff --git a/lecture/05_Listen/listings/list_pop.py b/lecture/05_Listen/listings/list_pop.py deleted file mode 100644 index 51629e6..0000000 --- a/lecture/05_Listen/listings/list_pop.py +++ /dev/null @@ -1,5 +0,0 @@ -meine_liste = [1, 2, 3, 4] -# Element an Index 0 entfernen und zurückgeben -weg = meine_liste.pop(0) -# [2,3,4] -# weg hat den Wert 1 diff --git a/lecture/05_Listen/listings/list_slice.kts b/lecture/05_Listen/listings/list_slice.kts new file mode 100644 index 0000000..b22cdac --- /dev/null +++ b/lecture/05_Listen/listings/list_slice.kts @@ -0,0 +1,4 @@ +val liste = listOf("Banane", "Apfel", "Ei", "Käse", "Zitrone") + +val sliced = liste.slice(2 until 4) +// sliced = ["Ei", "Käse"] diff --git a/lecture/05_Listen/listings/list_slice_list.kts b/lecture/05_Listen/listings/list_slice_list.kts new file mode 100644 index 0000000..d6721a3 --- /dev/null +++ b/lecture/05_Listen/listings/list_slice_list.kts @@ -0,0 +1,4 @@ +val liste = listOf("Banane", "Apfel", "Ei", "Käse", "Zitrone") + +val sliced = liste.slice(listOf(0, 2, 4)) +// sliced = ["Banane", "Ei", "Zitrone"] diff --git a/lecture/05_Listen/listings/list_slice_list_no_solution.kts b/lecture/05_Listen/listings/list_slice_list_no_solution.kts new file mode 100644 index 0000000..6647c6b --- /dev/null +++ b/lecture/05_Listen/listings/list_slice_list_no_solution.kts @@ -0,0 +1,3 @@ +val liste = listOf("Banane", "Apfel", "Ei", "Käse", "Zitrone") + +val sliced = liste.slice(listOf(0, 2, 4)) diff --git a/lecture/05_Listen/listings/list_slice_no_solution.kts b/lecture/05_Listen/listings/list_slice_no_solution.kts new file mode 100644 index 0000000..4fb883d --- /dev/null +++ b/lecture/05_Listen/listings/list_slice_no_solution.kts @@ -0,0 +1,3 @@ +val liste = listOf("Banane", "Apfel", "Ei", "Käse", "Zitrone") + +val sliced = liste.slice(2 until 4) diff --git a/lecture/05_Listen/listings/list_syntax.py b/lecture/05_Listen/listings/list_syntax.py deleted file mode 100644 index ba0a16b..0000000 --- a/lecture/05_Listen/listings/list_syntax.py +++ /dev/null @@ -1,13 +0,0 @@ -# Liste erstellen -meine_liste = [42, 56, 1, 2, 5] -leere_liste = [] - -# Liste lesen -# liest Element an Index 1 aus -zweites_element = meine_liste[1] # 56 -letztes_element = meine_liste[-1] # 5 - -# Element setzen -# setzt Element an Index 3 auf den Wert 9 -meine_liste[3] = 9 -# meine_liste ist jetzt: [42, 56, 1, 9, 5] diff --git a/lecture/05_Listen/listings/list_syntax_immutable_access.kts b/lecture/05_Listen/listings/list_syntax_immutable_access.kts index c93f7c1..ba24ba1 100644 --- a/lecture/05_Listen/listings/list_syntax_immutable_access.kts +++ b/lecture/05_Listen/listings/list_syntax_immutable_access.kts @@ -1,5 +1,7 @@ val myList = listOf(42, 56, 1, 2, 5) +val size = myList.size // 5 + val erstesElement = myList[0] // 42 val erstesElement = myList[4] // 5 val erstesElement = myList[myList.size - 1] // 5 diff --git a/lecture/05_Listen/listings/list_syntax_immutable_basic.kts b/lecture/05_Listen/listings/list_syntax_immutable_basic.kts index a15ff33..f7702f6 100644 --- a/lecture/05_Listen/listings/list_syntax_immutable_basic.kts +++ b/lecture/05_Listen/listings/list_syntax_immutable_basic.kts @@ -6,3 +6,5 @@ val leereListe1 = listOf() // Geht auch so val leereListe2 = emptyList() + +println(myList) // [42, 56, 1, 2, 5] diff --git a/lecture/05_Listen/listings/list_syntax_immutable_no_type.kts b/lecture/05_Listen/listings/list_syntax_immutable_no_type.kts index df61f69..db0ac69 100644 --- a/lecture/05_Listen/listings/list_syntax_immutable_no_type.kts +++ b/lecture/05_Listen/listings/list_syntax_immutable_no_type.kts @@ -6,3 +6,5 @@ val leereListe1 = listOf() // Geht auch so val leereListe2 = emptyList() + +println(myList) // [42, 56, 1, 2, 5] diff --git a/lecture/05_Listen/listings/listcopy1.kts b/lecture/05_Listen/listings/listcopy1.kts new file mode 100644 index 0000000..9ce3d5b --- /dev/null +++ b/lecture/05_Listen/listings/listcopy1.kts @@ -0,0 +1,8 @@ +val myList = mutableListOf(5, 7, 8, 42) +val kopie = myList + +println(myList[0]) // 5 +println(myList[1]) // 7 + +kopie[1] = 2 +println(myList[1]) // 2 diff --git a/lecture/05_Listen/listings/listcopy1.py b/lecture/05_Listen/listings/listcopy1.py deleted file mode 100644 index f507628..0000000 --- a/lecture/05_Listen/listings/listcopy1.py +++ /dev/null @@ -1,8 +0,0 @@ -meine_liste = [5, 7, 8, 42] -kopie = meine_liste - -print(meine_liste[0]) # 5 -print(meine_liste[1]) # 7 - -kopie[1] = 2 -print(meine_liste[1]) # 2 diff --git a/lecture/05_Listen/listings/listcopy2.kts b/lecture/05_Listen/listings/listcopy2.kts new file mode 100644 index 0000000..6755d27 --- /dev/null +++ b/lecture/05_Listen/listings/listcopy2.kts @@ -0,0 +1,9 @@ +val myList = mutableListOf(5, 7, 8, 42) +val kopie = myList + +println(myList[0]) // 5 +println(myList[1]) // 7 + +kopie[1] = 2 +println(myList[1]) // 7 +println(kopie[1]) // 2 diff --git a/lecture/05_Listen/listings/listcopy2.py b/lecture/05_Listen/listings/listcopy2.py deleted file mode 100644 index 96140d5..0000000 --- a/lecture/05_Listen/listings/listcopy2.py +++ /dev/null @@ -1,9 +0,0 @@ -meine_liste = [5, 7, 8, 42] -kopie = meine_liste.copy() - -print(meine_liste[0]) # 5 -print(meine_liste[1]) # 7 - -kopie[1] = 2 -print(meine_liste[1]) # 7 -print(kopie[1]) # 2 diff --git a/lecture/05_Listen/listings/out_of_bounds_code.kts b/lecture/05_Listen/listings/out_of_bounds_code.kts new file mode 100644 index 0000000..22f82c4 --- /dev/null +++ b/lecture/05_Listen/listings/out_of_bounds_code.kts @@ -0,0 +1,5 @@ +val myList = listOf(42, 45, 1, 2, 5) + +for (i in 0 until 6) { + println(myList[i]) +} diff --git a/lecture/05_Listen/listings/out_of_bounds_error.txt b/lecture/05_Listen/listings/out_of_bounds_error.txt new file mode 100644 index 0000000..9c4e7a2 --- /dev/null +++ b/lecture/05_Listen/listings/out_of_bounds_error.txt @@ -0,0 +1,3 @@ +java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 + at java.base/java.util.Arrays$ArrayList.get(Arrays.java:4165) + at Foo.(foo.kts:4) diff --git a/lecture/05_Listen/listings/partlySlice.py b/lecture/05_Listen/listings/partlySlice.py deleted file mode 100644 index 7043f3f..0000000 --- a/lecture/05_Listen/listings/partlySlice.py +++ /dev/null @@ -1,7 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[:4] -# sliced = ['Banane', 'Apfel', 'Ei', 'Käse'] - -sliced2 = liste[2:] -# sliced2 = ['Ei', 'Käse', 'Zitrone', 'Ingwer'] diff --git a/lecture/05_Listen/listings/partlySlice_no_solution.py b/lecture/05_Listen/listings/partlySlice_no_solution.py deleted file mode 100644 index bedff48..0000000 --- a/lecture/05_Listen/listings/partlySlice_no_solution.py +++ /dev/null @@ -1,6 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[:4] - - -sliced2 = liste[2:] diff --git a/lecture/05_Listen/listings/range_char_example b/lecture/05_Listen/listings/range_char_example new file mode 100644 index 0000000..e69de29 diff --git a/lecture/05_Listen/listings/range_char_example_minus.kts b/lecture/05_Listen/listings/range_char_example_minus.kts new file mode 100644 index 0000000..bb1968b --- /dev/null +++ b/lecture/05_Listen/listings/range_char_example_minus.kts @@ -0,0 +1,3 @@ +for (c in ('a' .. 'z') - ('e' .. 'm')) { + println(c) +} diff --git a/lecture/05_Listen/listings/range_char_example_plus.kts b/lecture/05_Listen/listings/range_char_example_plus.kts new file mode 100644 index 0000000..7397f70 --- /dev/null +++ b/lecture/05_Listen/listings/range_char_example_plus.kts @@ -0,0 +1,3 @@ +for (c in ('a' .. 'c') + ('x' .. 'z')) { + println(c) +} diff --git a/lecture/05_Listen/listings/simpleSlice.py b/lecture/05_Listen/listings/simpleSlice.py deleted file mode 100644 index 0ac888b..0000000 --- a/lecture/05_Listen/listings/simpleSlice.py +++ /dev/null @@ -1,4 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[2:4] -# sliced = ['Ei', 'Käse'] diff --git a/lecture/05_Listen/listings/simpleSlice_no_solution.py b/lecture/05_Listen/listings/simpleSlice_no_solution.py deleted file mode 100644 index 8b3cf09..0000000 --- a/lecture/05_Listen/listings/simpleSlice_no_solution.py +++ /dev/null @@ -1,3 +0,0 @@ -liste = ["Banane", "Apfel", "Ei", "Käse", "Zitrone", "Ingwer"] - -sliced = liste[2:4]