Commit dc2e457e authored by Raffaele Grosso's avatar Raffaele Grosso
Browse files

Add a couple of Python exercises

parent 42eefa06
......@@ -21,6 +21,7 @@
\newcounter{ChapterCounter}
\newcommand{\Chapter}[1]{\refstepcounter{ChapterCounter}\textbf{\theChapterCounter. #1 \label{#1}}}
\renewcommand\labelitemi{-}
\begin{document}
......@@ -29,13 +30,39 @@
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a Hello World program, that is a program that just writes: \texttt{"Hello World!"}
\item Write a program which asks the user its name and birth year and then writes the following text: \texttt{"Hi NAME, you will become X years old this year."}.
\HintN{1}{The \texttt{input} and \texttt{raw\_input} functions take as argument a string to be prompted to the user and return the user's input. These two functions differ between Python version (2 or 3). Mind the type of the returned object, casting it might be necessary.}
\HintN{1}{The \texttt{input} and \texttt{raw\_input} functions take as argument a string to be prompted to the user and return the user's input. These two functions differ between Python version 2 or 3. Mind the type of the returned object, casting it in case.}
\HintN{2}{To get the current year and avoid to hardcode it you should use the appropriate function from the \texttt{datetime} module, therefore \texttt{import} it.}
\end{enumerate}
\Chapter{Strings}
\begin{enumerate}[label=\textbf{\alph*)}]
\item In an interactive Python shell make sure you can:
\begin{itemize}
\item concatenate two or more strings into a single one
\item ``multiply'' a string \texttt{n} times
\item make a string upper (lower) case
\item find a substring inside a string
\item extract from a string a character at a given position
\item \textit{slice} a string, i.e. extract part of the string, from position \texttt{m} to position \texttt{n}
\end{itemize}
\end{enumerate}
\textbf{Data structures}\\
Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s, \texttt{set}s, \texttt{dict}ionaries.
\Chapter{Lists}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Since in Python a string is a list of chars, you already know something about lists. Make sure you also can:
\begin{itemize}
\item append an element to a list
\item add the elements of a list to another list
\item insert/remove an element at a given position
\item return the last element removing it from the list
\item sort and revert the order of elements in the list
\end{itemize}
By means of the methods above instantiate a list and use it as a stack (LIFO $\Leftrightarrow$ Last In First Out) or as a queue (FIFO $\Leftrightarrow$ Last In First Out)).
%\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a program that fills a list with ten integers taken as input from the terminal and returns only the even ones.
\Hint{Use a \textit{list comprehension}.}
\item Write a program which returns in reversed order the words of a sentence received as input.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment