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

Complete solutions of Python exercises in notebook

parent 2db3e07f
This diff is collapsed.
......@@ -160,27 +160,28 @@ $\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblks
$\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare$\\
$\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare\lgblksquare\lgwhtsquare$
\end{minipage}
\sitem 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\_\_}
\sitem Write a \textit{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\_\_}
\sitem 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}''}
\sitem Make a minimal example showing the effect of defining a variable inside a function with or without the keyword \texttt{global}.
\sitem Write a function taking one positional argument plus unnamed and named arguments and call it with three unnamed and two named arguments.
\Hint{Use the standard syntax \lstinline{f(a, *args, **kwargs)} and inside the function access the unnamed arguments from the list \texttt{args} and the named ones from the dictionary \texttt{kwargs}.}
\end{enumerate}
\Chapter{Modules}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Create a module (a file named \texttt{a\_sort.py}) containing the two functions \texttt{sort\_alpha} and \texttt{i\_sort\_alpha} taking as argument a string and returning the corresponding list of component words sorted alfabetically (case insensitive in the case of the second function).
\item Create a module for alphabetical sorting (a file named \texttt{alphasort.py}) containing the two functions \texttt{sort} and \texttt{isort}: they take as argument a string, preferably made up of many words, and return a list of words alphabetically sorted, case insensitive in the case of the \texttt{isort} function (use the code from your own solutions of exercises \ref{Lists}.\ref{1} and \ref{Lists}.\ref{2}).
\item From a script or an interactive shell import the module in the following ways:
\begin{enumerate}[label=\textbf{\roman*)}]
\item \texttt{import a\_sort}
\item \texttt{import a\_sort as srt}
\item \texttt{from a\_sort import *}
\item \texttt{import alphasort}
\item \texttt{import alphasort as srt}
\item \texttt{from alphasort import *}
\end{enumerate}
and make use of the two functions.
\sitem Complete the module with a \texttt{if \_\_name\_\_ == "\_\_main\_\_":} block so that the functions are executed when executing the module but not when loading it.
\item Use the \texttt{dir} function to inspect the name space of the imported module.
\sitem Write and use a package, that is a tree of modules inside a common directory where a \texttt{\_\_init\_\_.py} is provided.
\Note{The \lstinline{__init__.py} file is supposed to define which modules are to be exported (the so called \textit{API} of the package) and which not (the internal modules).}
% \sitem Reshape the module above into a package (modules inside a common directory) and use the \lstinline{__init__.py} file to define which modules are to be exported (the so called \textit{API} of the package) and which not (the internal modules).
\end{enumerate}
......@@ -202,13 +203,16 @@ Write a small Python script -- \texttt{inverse.py} -- (or a function -- \texttt{
\Chapter{Classes}
\begin{enumerate}[label=\textbf{\alph*)}]
\sitem Write a class to implement a Fibonacci iterator: assuming your class is called \texttt{fib\_iter}, this is its expected behaviour:
\sitem Write a class to implement a Fibonacci iterator. Assuming your class is called \texttt{fib\_iter}, the following \texttt{for}-loop:
\begin{lstlisting}
for i in fib_iter(500):
print(i, end=' ')
for i in fib_iter(500):
print(i, end=' ')
\end{lstlisting}
will return: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
\\{\color{Blue}Theory: }To conform to the paradigm of the Python iterator, the class needs
should return all Fibonacci numbers up to \texttt{500}:
\begin{lstlisting}
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
\end{lstlisting}
{\color{Blue}Theory: }To conform to the paradigm of the Python iterator, the class needs
to implement an \texttt{\_\_iter\_\_} method (usully returning \texttt{self}) and a
\texttt{\_\_next\_\_} method, which defines what is returned at each iteration and when
the loop should be exited by raising a \texttt{StopIteration} exception. The
......@@ -216,7 +220,7 @@ Write a small Python script -- \texttt{inverse.py} -- (or a function -- \texttt{
and then calles the \texttt{\_\_next\_\_} method at each iteration.
\sitem Instantiate an object of \texttt{fib\_iter} and display the value of its members after
manually calling \texttt{object.\_\_next\_\_()}.
\sitem Add a \textit{docstring} to the class and have it desplayed (use \texttt{fib\_iter.\_\_doc\_\_}).
\sitem Add a \textit{docstring} to the class and its methods.
\sitem Similiarly, you can create your own collection class by following the collection protocol.
Create a class representing a collection of files in a directory in form of a dictionary, where keys are the filenames of the files contained in the directory and values are the corresponding contents.\\
%http://zesty.ca/bc/explore-11.html
......@@ -236,11 +240,13 @@ For the exercises below, for example, you are supposed to use the \texttt{matplo
\Chapter{Plots and Histograms}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Plot the functions $sin(x)$ and $cos(x)$ in the range $[0,4]$ .
\Hint{Use the \texttt{sin} and \texttt{cos} functions for the $y$-axis and the \texttt{arange} function for the $x$-axis from the \texttt{numpy} package.}
\sitem Plot the function $f(x) = 1 + sin(3x)$ in the range $[0,4]$ labelling the axis and adding a grid.
\Hint{Use the \texttt{pyplot.subplots} function from the module \texttt{matplotlib} and \texttt{sin} from \texttt{numpy}.}
\Hint{Use the \texttt{pyplot.subplots} function from the package \texttt{matplotlib} and \texttt{sin} from \texttt{numpy}.}
%https://matplotlib.org/gallery/lines_bars_and_markers/simple_plot.html#sphx-glr-gallery-lines-bars-and-markers-simple-plot-py
\item Plot an histogram with the distribution of births per month from exercise \ref{Dictionaries}.\ref{3}.
\Hint{Use the \texttt{pyplot.hist} function from the module \texttt{matplotlib}.}
\sitem Plot an histogram with the distribution of births per month from exercise \ref{Dictionaries}.\ref{3}.
\Hint{Use the \texttt{pyplot.hist} function from the module \texttt{matplotlib}. The bins argument is a list of bin edges, included first and last, thus thirteen edges for twelve bins.}
%https://matplotlib.org/gallery/statistics/histogram_features.html
\sitem Histogram a randomly generated distribution around a gaussian and add a best fit line.
\Hint{Use the \texttt{np.random.randn} to generate the distribution.}
......
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