Commit 9e112356 authored by Raffaele Grosso's avatar Raffaele Grosso
Browse files

Improve some Python exercise

parent b2e6a0e5
\documentclass[10pt,a4paper]{letter}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
%\usepackage{amsmath}
\usepackage{stix}
\usepackage{enumitem}
\usepackage[margin=4.2cm]{geometry}
\usepackage[utf8]{inputenc}
......@@ -84,11 +85,11 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\Hint{Use a \textit{list comprehension}.}
\item Write a program which returns in reversed order the words of a sentence received as input.
\Hint{Use the ``\textit{extended slice syntax}'' \texttt{[::-1]}. Alternatively use the \texttt{reverse} function of list objects.}
\Example{``\textit{I like this color}'' should give ``\textit{color this like I}''.}
\Example{``\texttt{I like this color}'' should give ``\texttt{color this like I}''.}
\item \label{1} Write a program which returns in alphabetical order the words of a sentence received as input.
\Example{The previous example should give ``\textit{I color like this}''.}
\Example{The previous example should give ``\texttt{I color like this}''.}
\item \label{2} Like in the previous exercise (\ref{Lists}.\ref{1}) but case insensitive. \Hint{You can pass the \texttt{str.lower} function as ``\texttt{key=}'' argument of \texttt{sort} or \texttt{sorted}.}
\Example{The previous example should give ``\textit{color I like this}''.}
\Example{The previous example should give ``\texttt{color I like this}''.}
\end{enumerate}
\Chapter{Dictionaries}
......@@ -96,8 +97,7 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\item Make sure you can:
\begin{itemize}
\item instantiate a dictionary (with \texttt{\{\}} and with \texttt{dict()})
\item add an entry (key-value pair) to a dictionary
\item insert/remove an entry
\item add/remove an entry (key-value pair) to/from a dictionary
\item get the value associated to a given key
\item check if the dictionary contains the pair with a given key
\item iterate over the elements of the dictionary
......@@ -107,7 +107,7 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\Chapter{Conditions and loops}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Make sure you can correctly use \texttt{if-else}-statements and \texttt{for}- and \texttt{while}-loops, in particular the construct \texttt{for i in} + \texttt{range()} or + iterable.
\item Make sure you can correctly use \texttt{if-else}-statements and \texttt{for}- and \texttt{while}-loops, in particular the construct ``\texttt{for i in}'' plus \texttt{range()} or plus an iterable.
\item List the arguments passed to your script.
\Hint{Using the \texttt{sys} module, \texttt{sys.argv} is a list containing the arguments.}
\item Write a ``guess the number'' game: the user guesses an integer between 1 and 10 until (s)he enters \texttt{exit} or until (s)he gets the right number.
......@@ -123,18 +123,25 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\Chapter{Functions}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a program which, by means of a function, prints out a sketched chessboard: the first, last and all odd lines are a sequence of dashes while the even lines are a sequence of spaced vertical bars, like in the sketch below.\\
\begin{verbatim} ----------------------------------------
| | | | | | | | | | |
----------------------------------------
| | | | | | | | | | |
----------------------------------------
\end{verbatim}
\end{enumerate}
Lambda functions:
\begin{enumerate}[label=\textbf{\alph*)}]
\item Like in exercise \ref{Lists}.\ref{2} but this time you are supposed to pass a lambda function as ``\texttt{key=}'' argument of \texttt{sort} or \texttt{sorted}, using the \texttt{lower} function of a string object.
\\ {\color{Blue} Example:} ``\textit{I like this color}'' should give ``\textit{color I like this}''
\item
\begin{minipage}[t]{0.7\textwidth}
Write a program which prints out a chessboard (like in the figure on the right). Define a function \texttt{odd\_row()} and a function \texttt{even\_row} returning a row starting with a white or black square respectively and in the main part of your script call \texttt{odd\_row()} for all odd lines (first and last included) and \texttt{even\_row} for all the lines in between.
\Hint{Let the line be a string composed of the Unicode characters: \texttt{2B1B} for a black square and \texttt{2B1C} for a white square.}
\end{minipage}
\begin{minipage}[t]{0.3\textwidth}
$\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare$\\
$\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare$\\
$\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare$\\
$\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare$\\
$\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare$\\
$\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare$\\
$\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare$\\
$\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare$
\end{minipage}
\item Write a docstring for both of this functions and verify it gets called when it should (in an interactive Python shell) or call it explicitly with \texttt{.\_\_doc\_\_}
\item Like in exercise \ref{Lists}.\ref{2} but this time you are supposed to pass a \textit{lambda function} as ``\texttt{key=}'' argument of \texttt{sort} or \texttt{sorted}, using the \texttt{lower} function of a string object.
\Example{``\texttt{I like this color}'' should give ``\texttt{color I like this}''}
\item Make a minimal example showing the effect of defining a variable inside a function with or without the keyword \texttt{global}.
\end{enumerate}
......
Markdown is supported
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