\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[czech]{babel}
\usepackage{palatino}
\usepackage{verbatim}
\usetheme{Warsaw}
\title{Programování 1: Seznamy}
\author[Martin Mareš]{Martin Mareš\\\texttt{mj@ucw.cz}}
\institute{Katedra Aplikované Matematiky\\MFF UK Praha}
\date{2019}
\begin{document}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamerfont{title page}{family=\rmfamily}

\begin{frame}
\titlepage
\end{frame}

\input ../slidemac.tex

% ----------------------------------------------------------------------

\begin{frame}{Seznamy a jejich indexování}

\py{%
cisla = [11, 22, 33, 44, 55]
}{%
}

\py{%
cisla[0]
}{%
11
}

\py{%
cisla[2]
}{%
33
}

\py{%
cisla[-1]
}{%
55
}

\py{%
len(cisla)
}{%
5
}

\end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Vnořené seznamy}

\py{%
matice = [[11, 12, 13], [21, 22, 23]]
}{%
}

\py{%
matice[0]
}{%
[11, 12, 13]
}

\py{%
matice[0][2]
}{%
13
}

\end{frame}

% ----------------------------------------------------------------------

% \begin{frame}{Řetězec se také chová jako seznam}
% 
% \py{%
% slovo = "mňau"\\
% len(slovo)
% }{%
% 4
% }
% 
% \py{%
% slovo[1]
% }{%
% 'ň'
% }
% 
% \py{%
% slova = ["mňau", "haf", "kvák"]\\
% slova[1]
% }{%
% 'haf'
% }
% 
% \end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Cyklus for}

\py{%
slova = ["mňau", "haf", "kvák"]\\
for x in slova:\.
\>print(x)
}{%
mňau\\
haf\\
kvák
}

\py{%
for x in range(1, 5):\.
\>print(x)
}{%
1\\
2\\
3\\
4
}

\py{%
for i in range(len(x)):\.
\>print(x[i])
}{%
}

\end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Operace se seznamy}

\py{%
slova = ["mňau", "haf", "kvák"]\\
slova.append("íá")\\
slova
}{%
['mňau', 'haf', 'kvák', 'íá']
}

\py{%
[1, 2] + [3, 4, 5]
}{%
[1, 2, 3, 4, 5]
}

\py{%
[0] * 5
}{%
[0, 0, 0, 0, 0]
}

\end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Chyták: proměnné na seznamy jen odkazují!}

\py{%
x = [1, 2, 3]\\
y = x\\
x[2] = 9\\
y
}{%
[1, 2, 9]
}

\py{%
m = [[0] * 4] * 3\\
m
}{%
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
}

\py{%
m[0][0] = 1\\
m
}{%
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
}

\end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Vypsání čísel pozpátku}

\verbatiminput{seznam-pozpatku.py}

\end{frame}

% ----------------------------------------------------------------------

\begin{frame}{Něco navíc o generátorech}

\py{%
range(5)
}{%
range(0, 5)
}

\py{%
list(range(5))
}{%
[0, 1, 2, 3, 4]
}

\py{%
list(range(0, 20, 3))
}{%
[0, 3, 6, 9, 12, 15, 18]
}

\py{%
list(range(10, 0))
}{%
[]
}

\py{%
list(range(10, 0, -1))
}{%
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
}

\end{frame}

% ----------------------------------------------------------------------

\end{document}
