\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 So far: our ``data'' are strings and numbers
\item conversion between strings and numbers
\begin{itemize}
\item \texttt{str()}
\item \texttt{float()}
\item \texttt{int()}
\item ps, the use of \texttt{eval()} is forbidden
\end{itemize}
\item More things to do with strings
\item If
% \item Lists
\end{itemize}

\foilhead{Strings}

\begin{itemize}
\item Is what we get from the outside world
\item Using \texttt{input()} or \texttt{for line in sys.stdin}
\item Is what we give back to the outside world: \texttt{print()}
\end{itemize}

\foilhead{Strings}

\begin{itemize}
\item A string is a sequence of characters
\item \texttt{len()} returns the length of that sequence
\item We can also refer to these characters by means of \textit{indexing}
\item Characters are numbered starting with 0
\end{itemize}

\foilhead{String Indexing}

\begin{lstlisting}
>>> location = "Groningen"
>>> location[0]
'G'
>>> location[3]
'n'
>>> location[7]
'e'
>>> location[121]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> 
\end{lstlisting}

\foilhead{String Indexing}

\begin{lstlisting}
>>> location = "FC " + location
>>> location
'FC Groningen'
>>> location[3]
'G'
>>> location[2]
' '
>>> location[-1]
'n'
\end{lstlisting}

\foilhead{String Indexing}

\begin{lstlisting}
>>> i = 3
>>> i = i+2
>>> location = "Leeuwarden"
>>> location[i+1]
\end{lstlisting}

\foilhead{String Indexing}

\begin{lstlisting}
>>> i = 3
>>> i = i+2
>>> location = "Leeuwarden"
>>> location[i+1]
'r'
>>> location[-3]
\end{lstlisting}

\foilhead{String Indexing}

\begin{lstlisting}
>>> i = 3
>>> i = i+2
>>> location = "Leeuwarden"
>>> location[i+1]
'r'
>>> location[-3]
'd'
\end{lstlisting}

\foilhead{String slices}

\begin{itemize}
\item select a slice from a string
\item by specifying begin and end position
\item \texttt{location[4:6]}
\item from character with index 4, to (but not including) character with index 6
\end{itemize}


\foilhead{String slices}

\begin{lstlisting}
>>> location = "Leeuwarden"
>>> location[4:6]
'wa'
>>> location[:6]
'Leeuwa'
>>> location[4:]
'warden'
>>> location[4:-1]
\end{lstlisting}


\foilhead{String slices}

\begin{lstlisting}
>>> location = "Leeuwarden"
>>> location[4:6]
'wa'
>>> location[:6]
'Leeuwa'
>>> location[4:]
'warden'
>>> location[4:-1]
'warde'
\end{lstlisting}

\foilhead{Iterating over strings}

\begin{lstlisting}
>>> location = "Leeuwarden"
>>> for ch in location:
...     print(ch+ch)
... 
LL
ee
ee
uu
ww
aa
rr
dd
ee
nn
\end{lstlisting}


\foilhead{Iterating over strings}

\begin{lstlisting}
>>> location = "Leeuwarden"
>>> for ch in location[4:7]:
...     print(ch)
... 
w
a
r
\end{lstlisting}

\foilhead{Iterating over strings}

\begin{lstlisting}
>>> location = "Leeuwarden"
>>> for ch in location[4:7]:
...     print(ch,end=' ')
... 
w a r >>>
\end{lstlisting}


\foilhead{Please try this yourself (1)}

Write a program that reads lines of input (line by line)
and for each line it prints the first character and the last character 
of that line.  For instance, if our input file \texttt{in.txt} 
consists of
\begin{verbatim}
Your faith was strong but you needed proof
You saw her bathing on the roof
Her beauty and the moonlight overthrew ya
\end{verbatim}
We should then be able to get the following, assuming your
program is called "hal.py":
\begin{verbatim}
python3 hal.py < in.txt
Yf
Yf
Ha
\end{verbatim}




\foilhead{Please try this yourself (2)}

Write a program that reads lines of input (line by line)
and after all lines have been read, it prints the
first character of each line as a single word. 

For instance, assuming your program is called "acro.py":
\begin{verbatim}
python3 acro.py < in.txt
YYH
\end{verbatim}

Can you solve this exercise by executing the print statement
only once?


\foilhead{acro.py}

\begin{lstlisting}
#!/usr/bin/python3

import sys

def main():
    acro=""
    for line in sys.stdin:
        line = line.rstrip()
        acro = acro+line[0]
    print(acro)

main()
\end{lstlisting}

\foilhead{What if}

\begin{itemize}
\item What if we encounter an empty line in the input for
acro.py?
\end{itemize}

\foilhead{What if}

\begin{itemize}
\item What if we encounter an empty line in the input for
acro.py?

\item
\begin{verbatim}
python3 acro.py
alskdjf
alsdfjk

Traceback (most recent call last):
  File "acro.py", line 14, in <module>
    main()
  File "acro.py", line 9, in main
    acro = acro+line[0]
IndexError: string index out of range
\end{verbatim}

\end{itemize}

\foilhead{What if}

\begin{itemize}
\item Conditional execution of code
\end{itemize}


\foilhead{acro.py}

\begin{lstlisting}
#!/usr/bin/python3

import sys

def main():
    acro=""
    for line in sys.stdin:
        line = line.rstrip()
        if len(line) > 0:
            acro = acro+line[0]
    print(acro)

main()
\end{lstlisting}

\foilhead{What if}

Such expressions evaluate to True or False.

Typical expressions:

\begin{itemize}
\item \texttt{x > y}
\item \texttt{x < y}
\item \texttt{x == y}
\item Expr1 or Expr2
\item not Expr
\item Expr1 and Expr2
\end{itemize}

\foilhead{What if}

Such expressions evaluate to True or False.

Typical expressions:

\begin{itemize}
\item Check if a character occurs in a string:
\begin{lstlisting}
if ch in "abc":
    print(ch)
\end{lstlisting}
\item The number 0 is interpreted as False, other numbers are
interpreted as True
\item The empty string is interpreted as False, other strings are interpreted as True
\end{itemize}


\foilhead{acro.py}

\begin{lstlisting}
#!/usr/bin/python3

import sys

def main():
    acro=""
    for line in sys.stdin:
        line = line.rstrip()
        if line:
            acro = acro+line[0]
    print(acro)

main()
\end{lstlisting}

\foilhead{What else}

\begin{lstlisting}
#!/usr/bin/python3

import sys

def main():
    acro=""
    for line in sys.stdin:
        line = line.rstrip()
        if line:
            acro = acro+line[0]
        else:
            print("Warning: empty line!")
    print(acro)

main()
\end{lstlisting}

\foilhead{What if, else-if, else}

\begin{lstlisting}
if ch in "aeiou":
    print("vowel!")
elif ch in "bcdfghjklmnpqrstvwxyz":
    print("consonant!")
elif ch in "0123456789":
    print("digit")
else:
    print("some other character")
\end{lstlisting}


\foilhead{Mystery program (1)}

\begin{lstlisting}
for line in sys.stdin:
    line = line.rstrip()
    nline=""
    for ch in line:
        if not ch in "aeiou":
            nline=nline+ch
    print(nline)
\end{lstlisting}



\foilhead{Mystery program (2)}

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

\foilhead{Your Turn!}

\begin{enumerate}

\item
Write a program which puts every word on a separate line, assuming
that words are separated by white space in the input.

\item
Write a program which works line by line, and which prints every
line in which the letter "e" is the only occurring vowel. For 
the purpose of this exercise, the only vowels we consider are
a, e, i, o, u.

\item
Write a program which works line by line, and which prints every
line in which the first character equals the final character of
the line.

\item
Write a program which works line by line, and which prints every
line in which two vowels are placed directly next to each other.

\clearpage

\item 
Write a program which works line by line, and which prints every
line preceded by the number of vowels, the number of consonants, and
the number of other characters in the input. Example:

\begin{verbatim}
python3 cv.py < in.txt 
14 20 8 Your faith was strong but you needed proof
10 14 7 You saw her bathing on the roof
13 21 7 Her beauty and the moonlight overthrew ya
\end{verbatim}

\item 
Write a program which works line by line, and which prints every line
in which each of the vowels a, e, i, o, u occurs

\item
Consider the file \texttt{/net/corpora/Eindhoven/words.txt}. It contains words from the
Dutch Eindhoven corpus. We can use the previous program to find words which contain
each of the five vowels. We can combine this with one of the programs of last week
and with the UNIX sort command to find the shortest word which contains all vowels.
Which word is that? And which is the longest?

\item
Can you write a program which finds the word in \texttt{/net/corpora/Eindhoven/words.txt} which
contains the longest sequence of consonants? 

\end{enumerate}

\end{document}
