\documentclass{foils}

\MyLogo{}
\usepackage{color}
\usepackage[landscape,a4paper,hscale=0.9,vscale=0.9]{geometry}
\usepackage{graphicx}
\usepackage{listings}

\newcommand{\Red}[1]{\textcolor{red}{#1}}
\newcommand{\Green}[1]{\textcolor{green}{#1}}
\newcommand{\Blue}[1]{\textcolor{blue}{#1}}

\renewcommand\normalcolor{\color{blue}}

\definecolor{mauve}{rgb}{0.58,0,0.82}


\color{black}
\pagecolor{white}



\usepackage{url}

\begin{document} 
\lstset{language=Python,
        basicstyle=\small\ttfamily,
        keywordstyle=\bfseries\color{green},
        identifierstyle=\color{blue},
        stringstyle=\color{mauve},
        frame=single,
        showspaces=false,
        showstringspaces=false,
}

\author{Gertjan van Noord}
\title{\Blue{Coding for Humanities -- Python}}

\maketitle

\foilhead{Overview}

\begin{itemize}
\item Exercises
\item Lists
\end{itemize}

\foilhead{1. Every word on a single line}

\begin{lstlisting}
for line in sys.stdin:
    line=line.rstrip()
    word=''
    for char in line:
        if (char == ' '):
            print(word)
            word=''
        else:
            word=word+char
    print(word)
\end{lstlisting}


\foilhead{2. Only lines with e the only vowel}
\begin{lstlisting}
for line in sys.stdin:
    line=line.rstrip()
    if ( 'a' not in line and
         'i' not in line and
         'o' not in line and
         'u' not in line ):
        print(line)
\end{lstlisting}

\foilhead{2. Only lines with e the only vowel (for Erwin)}
\begin{lstlisting}
for line in sys.stdin:
    line=line.rstrip()
    if ( 'a' not in line and
         'i' not in line and
         'o' not in line and
         'u' not in line and
         'e' in line):
        print(line)
\end{lstlisting}


\foilhead{3. Only lines with identical first and last}
\begin{lstlisting}
for line in sys.stdin:
    line=line.rstrip()
    if line[0] == line[-1]:
        print(line)
\end{lstlisting}

\foilhead{4. Lines with neighbouring vowels}
\begin{lstlisting}
for line in sys.stdin:
    line=line.rstrip()
    prevchar='x'
    for char in line:
        if char in "aeiou" and prevchar in "aeiou":
            print(line)
            break     # leave the loop, we have a hit
                      # therefore no point to continue
        prevchar=char
\end{lstlisting}

\foilhead{5. Number of vowels, cons, other}
\begin{lstlisting}
for line in sys.stdin:
    line = line.rstrip()
    cs=vs=os=0
    for ch in line:
        if ch in "aeiou":
            vs=vs+1
        elif ch in "bcdfghjklmnpqrstvwxyz":
            cs=cs+1
        else:
            os=os+1
    print(vs,cs,os,line)
\end{lstlisting}

\foilhead{6. Lines with all vowels}
\begin{lstlisting}
for line in sys.stdin:
    line = line.rstrip()
    if ( "a" in line and
         "e" in line and
         "i" in line and
         "o" in line and
         "u" in line) :
        print(line)
\end{lstlisting}



\foilhead{7. Shortest word with all vowels}
\begin{verbatim}
cat /net/corpora/Eindhoven/words.txt | sort -u | 
    python3 p26.py | python3 p03.py | sort -nr | tail -n1
8 autopsie
\end{verbatim}

\foilhead{8. Longest consonant cluster}

This prints each line with the size of the longest cluster:

\begin{lstlisting}
for line in sys.stdin:
    line = line.rstrip()
    max=0
    cluster=0
    for ch in line:
        if ch in "bcdfghjklmnpqrstvwxz":
            cluster=cluster+1
        else:
            if cluster>max:
                max=cluster
            cluster=0
    print(max,line)
\end{lstlisting}

\foilhead{Longest consonant cluster}

for fun, I removed 'y' from the list of consonants!

\begin{verbatim}
cat /net/corpora/Eindhoven/words.txt | 
    python3 p28.py |sort -n |tail
6 voortschrijdt
6 vrachtschip
6 zendingsscholen
6 zwartschrift
7 herfstschemer
7 machtsstrijd
7 machtsstructuur
7 tekstschrijver
\end{verbatim}

% \foilhead{Opperlandse Taal- en Letterkunde}


% \includegraphics[scale=.8]{opperlans.png}

% \foilhead{Battus}

% \begin{itemize}
% \item Hugo Brandt Corstius (1935-2014)
% \item Battus, Piet Grijs, Stoker, Jan Eter, Raoul Chapkis, \dots
% \item First computational linguist in NL
% \item Author of very many columns, essays, \dots
% \item Father of Jelle and Aaf
% \item Winner of P.C. Hooftaward, which was never awarded because 
% "het kwetsen tot instrument had verheven", according to the government
% \end{itemize}

\foilhead{Lists}

In Python, strings are sequences of characters.

Lists are sequences too.

The elements can be anything: numbers, strings, lists, anything
\vspace{2ex}


\begin{lstlisting}
>>> list1 = [1,3,5]
>>> list2 = [2,4,6]
>>> list1 + list2
[1, 3, 5, 2, 4, 6]
>>> list3 = [list2,list2]
>>> list3
[[2, 4, 6], [2, 4, 6]]
\end{lstlisting}


\foilhead{Lists}

We can refer to elements of a list with indexing and slices:
\vspace{2ex}


\begin{lstlisting}
>>> simplelist = [1,3,5]
>>> simplelist[1] * simplelist[2]
15
>>> simplelist[:2]
[1, 3]
\end{lstlisting}


\foilhead{Lists}

The empty list is written \texttt{[]}
\vspace{2ex}

\begin{lstlisting}
>>> l1 = ['blue','red','green']
>>> for el in l1:
...     print(el)
... 
blue
red
green
>>> if 'red' in l1:
...     print("YES!")
... 
YES!
\end{lstlisting}


\foilhead{Lists}

\begin{itemize}
\item indexing
\item iteration
\item \texttt{if var in list}
\item append
\item overwriting (``item assignment'')
\end{itemize}

\foilhead{append}

\begin{lstlisting}
>>> l1 = [1,2,3]
>>> l2 = [3,4,5]
>>> l1 + l2
[1, 2, 3, 3, 4, 5]
>>> l1 = []
>>> l2 = [3]
>>> l1 + l2
[3]
\end{lstlisting}

\foilhead{append}

\begin{lstlisting}
>>> list4 = ['a','b']
>>> list4.append('c')
>>> list4
['a', 'b', 'c']
>>> list4 + 'd'
Traceback (most recent call last):
  File ``<stdin>'', line 1, in <module>
TypeError: can only concatenate list (not ``str'') to list
>>> list4 + ['d']
['a', 'b', 'c', 'd']
\end{lstlisting}

\foilhead{Item assignment: for lists, not for strings!}

\begin{lstlisting}
>>> x = "johnmy"
>>> x[4] = 'n'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

\end{lstlisting}

\foilhead{Item assigment: ok for lists!}

\begin{lstlisting}
>>> x = [1,3,5,8,9]
>>> x[3]=7
>>> x
[1, 3, 5, 7, 9]
\end{lstlisting}

\foilhead{Beware}

\begin{lstlisting}
>>> x = [1,2,3]
>>> y = x
>>> y
[1, 2, 3]
>>> y[2]=4
>>> y
[1, 2, 4]
>>> x
[1, 2, 4]
\end{lstlisting}



\foilhead{split}

The split() method is used to break a string into a list of strings

\begin{lstlisting}
>>> sen = "123-123-123-123"
>>> l2 = sen.split('-')
>>> l2
['123', '123', '123', '123']
\end{lstlisting}


\foilhead{split}

If the split method does not have an argument, it will split on white space

\begin{lstlisting}
>>> sen1 = "His ignorance is encyclopedic"
>>> sen1.split()
['His', 'ignorance', 'is', 'encyclopedic']
>>> sen2 = "This book fills a much-needed gap." 
>>> sen2.split()
['This', 'book', 'fills', 'a', 'much-needed', 'gap.']
\end{lstlisting}

\foilhead{Words on separate lines}

\begin{lstlisting}
for line in sys.stdin:
    for word in line.split():
        print(word)
\end{lstlisting}



\foilhead{Count words}
\begin{lstlisting}
for line in sys.stdin:
    words = line.split()
    print(len(words),line)
\end{lstlisting}

\foilhead{Lists of lists \dots}

\begin{lstlisting}
>>> m = [[1,2,3], [4,5,6]]
>>> m[1][2]
6
>>> l1 = [1,3,5]
>>> l2 = [2,4,6]
>>> m = []
>>> m + [l1]
[[1, 3, 5]]
>>> m = m + [l1]
>>> m = m + [l2]
>>> m
[[1, 3, 5], [2, 4, 6]]
\end{lstlisting}

\foilhead{Mystery program (1)}

\begin{lstlisting}
for line in sys.stdin:
    words = line.split()
    for word in words:
        if len(word) > 4:
            print(word,end=' ')
    print()
\end{lstlisting}




\foilhead{Mystery program (2)}

\begin{lstlisting}
m = []
for line in sys.stdin:
    line = line.rstrip()
    string_values = line.split(',')
    l = []
    for v in string_values:
        l = l + [int(v)]
    m = m + [l] 

print(m)   
\end{lstlisting}

\foilhead{Your turn, again}

\begin{enumerate}
\item Write a program which takes a text as input and which prints 
a sequence of words on a single line where each word is the last word
of a line (the last word of line 1, followed by the last word of line 2, etc).
\begin{verbatim}
python3 p31.py < in.txt
proof roof ya
\end{verbatim}

\item Write a program which takes a text as input, and 
which prints each word on a separate line if that word 
consists of a consonant, followed by a vowel, followed by a consonent.
\begin{verbatim}
python3 p32.py < in.txt
was
but
saw
her
Her
\end{verbatim}


\item Write a program which takes lines of five numbers as input, and 
which prints the sum of each
column. For example, if our matrix mat0.txt looks as follows:
\begin{verbatim}
4 6 10 10 8
3 5 11 21 3
5 1 3 3 4
\end{verbatim}
Then:
\begin{verbatim}
python3 p33.py < mat0.txt
12 12 24 34 15
\end{verbatim}


\item Write a program which takes a text as input, and which prints each word on a separate line
if that word is preceded by a determiner. The determiners are the words \texttt{the, a, an}.
\begin{verbatim}
python3 p34.py < in.txt
roof
moonlight
\end{verbatim}

\item \textbf{Difficult}. Write a program which takes a text as input, and which prints longest sequences of words which start with
an uppercase letter. For the following text:

\begin{verbatim}
Feyenoord heeft ook zijn derde duel  
in de Champions League verloren . 
Na de kansloze excercities tegen 
Manchester City en Napoli ging de ploeg ditmaal in
de eigen Kuip strijdend ten onder tegen
Sjachtar Donetsk : 1-2 .
\end{verbatim}

Your program should output:
\begin{verbatim}
Feyenoord
Champions League
Na
Manchester City
Napoli
Kuip
Sjachtar Donetsk
\end{verbatim}

\end{enumerate}


\end{document}
