|
| 1 | +\documentclass[11pt,a4]{article} |
| 2 | + |
| 3 | +\usepackage[margin=2cm]{geometry} |
| 4 | + |
| 5 | +\usepackage{multicol} |
| 6 | + |
| 7 | +\usepackage{amsmath} |
| 8 | +\usepackage{url} |
| 9 | + |
| 10 | +\usepackage{amsmath} |
| 11 | +\usepackage{url} |
| 12 | + |
| 13 | +\usepackage[utf8]{inputenc} |
| 14 | + |
| 15 | +% Default fixed font does not support bold face |
| 16 | +\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold |
| 17 | +\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10} % for normal |
| 18 | + |
| 19 | +% Custom colors |
| 20 | +\usepackage{color} |
| 21 | +\definecolor{deepblue}{rgb}{0,0,0.5} |
| 22 | +\definecolor{deepred}{rgb}{0.6,0,0} |
| 23 | +\definecolor{deepgreen}{rgb}{0,0.5,0} |
| 24 | + |
| 25 | +\usepackage{listings} |
| 26 | + |
| 27 | +% Python style for highlighting |
| 28 | +\newcommand\pythonstyle{\lstset{ |
| 29 | +language=Python, |
| 30 | +basicstyle=\ttm, |
| 31 | +otherkeywords={self}, % Add keywords here |
| 32 | +keywordstyle=\ttb\color{deepblue}, |
| 33 | +emph={MyClass,__init__}, % Custom highlighting |
| 34 | +emphstyle=\ttb\color{deepred}, % Custom highlighting style |
| 35 | +stringstyle=\color{deepgreen}, |
| 36 | +frame=tb, % Any extra options here |
| 37 | +showstringspaces=false % |
| 38 | +}} |
| 39 | + |
| 40 | + |
| 41 | +% Python environment |
| 42 | +\lstnewenvironment{python}[1][] |
| 43 | +{ |
| 44 | +\pythonstyle |
| 45 | +\lstset{#1} |
| 46 | +} |
| 47 | +{} |
| 48 | + |
| 49 | +% Python for external files |
| 50 | +\newcommand\pythonexternal[2][]{{ |
| 51 | +\pythonstyle |
| 52 | +\lstinputlisting[#1]{#2}}} |
| 53 | + |
| 54 | +% Python for inline |
| 55 | +\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}} |
| 56 | + |
| 57 | + |
| 58 | +\usepackage{collectbox} |
| 59 | + |
| 60 | +\newcommand{\mybox}[2]{$\quad$\fbox{ |
| 61 | +\begin{minipage}{#1cm} |
| 62 | +\hfill\vspace{#2cm} |
| 63 | +\end{minipage} |
| 64 | +}} |
| 65 | + |
| 66 | + |
| 67 | +\usepackage{fancyhdr} |
| 68 | +\pagestyle{fancy} |
| 69 | +\rhead{Programmazione 1 - Esercitazione 5} |
| 70 | + |
| 71 | +\include{book_header} |
| 72 | + |
| 73 | +\begin{document} |
| 74 | +\thispagestyle{empty} |
| 75 | +\hrule |
| 76 | +\begin{center} |
| 77 | + {\Large {\bf Programmazione 1 \hspace{3cm} $\quad \quad \quad$ Tutorato 3}} |
| 78 | +\end{center} |
| 79 | + |
| 80 | +\hrule |
| 81 | + |
| 82 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | +\section*{} |
| 84 | + |
| 85 | +\begin{enumerate} |
| 86 | + |
| 87 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 | +\item Scrivere due funzioni {\tt MapFR(x)} e {\tt FilterFR(x)} che implementano la $map$ e la $filter$ utilizzando la FoldRight. |
| 89 | + |
| 90 | +\begin{python} |
| 91 | +def FoldRight(P, As, v): |
| 92 | + if IsEmpty(As): |
| 93 | + return v |
| 94 | + return P(Head(As), FoldRight(P, Tail(As), v)) |
| 95 | +\end{python} |
| 96 | + |
| 97 | + |
| 98 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 99 | +\item Scrivere due funzioni {\tt MapFL(x)} e {\tt FilterFL(x)} che implementano la $map$ e la $filter$ utilizzando la FoldLeft. |
| 100 | + |
| 101 | +\begin{python} |
| 102 | +def FoldLeft(P, As, v): |
| 103 | + if IsEmpty(As): |
| 104 | + return v |
| 105 | + return FoldLeft(P,Tail(As),P(z,Head(As)) |
| 106 | +\end{python} |
| 107 | + |
| 108 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 109 | +\item Ricordando le strutture viste: |
| 110 | + |
| 111 | +\begin{multicols}{2} |
| 112 | +\small |
| 113 | +\fbox{\parbox[t][3.5cm][t]{7.4cm}{ |
| 114 | +\vspace{0.2cm} |
| 115 | +\textbf{def} $F(X_1, \dots, X_n):$\\ |
| 116 | +$\hspace*{0.5cm}$ \textbf{def} $Fiter(Y_1,...,Y_m):$\\ |
| 117 | +$\hspace*{1.0cm}$ \textbf{if} $Proposizione(Y_1,...,Y_m)==True:$\\ |
| 118 | +\vspace{0.2cm} |
| 119 | +$\hspace*{1.5cm}$ \textbf{return} $Y_1,...,Y_{m'}$ $(m'\le m)$\\ |
| 120 | +\vspace{0.2cm} |
| 121 | +$\hspace*{1.0cm}$ \textbf{return} $Fiter(\tilde{Y_1},...,\tilde{Y_m})$\\ |
| 122 | +$\hspace*{0.5cm}$ $\textbf{return}$ $Fiter(\tilde{X_1},...,\tilde{X_m})$\\}} |
| 123 | +\columnbreak |
| 124 | + |
| 125 | +\fbox{\parbox[t][3.5cm][t]{7.4cm}{ |
| 126 | +\vspace{0.2cm} |
| 127 | +\textbf{def} $F(X_1, \dots, X_n):$\\ |
| 128 | +$\hspace*{0.5cm}$ $Y_i=\tilde{X_i}$ per $i=0,\dots,m$\\ |
| 129 | +$\hspace*{0.5cm}$ (le $\tilde{X_i}$ sono costruite a partire dalle $X_i$)\\ |
| 130 | +$\hspace*{0.5cm}$ \textbf{while} $Proposizione(Y_1,...,Y_m)==False:$\\ |
| 131 | +$\hspace*{1.4cm}$ $Y_i=\tilde{Y_i}$ per $i=0,\dots,m$\\ |
| 132 | +\\ |
| 133 | +$\hspace*{0.5cm}$ \textbf{return} $Y_1,...,Y_{m'}$}} |
| 134 | + |
| 135 | +\end{multicols} |
| 136 | + |
| 137 | + |
| 138 | +Si scrivano in entrambi i modi le funzioni seguenti: |
| 139 | + |
| 140 | +\begin{enumerate} |
| 141 | +\item {\tt Fib(n)}: prende in input un numero intero $n$ e restituisce l'n-esimo elemento della successione di Fibonacci. |
| 142 | +\item {\tt Fn(n)}: prende in input un numero intero $n$ e restituisce l'n-esimo elemento della successione $Fn$ definita ricorsivamente: |
| 143 | +\begin{equation*} |
| 144 | +\mbox{Fn}(x) = \left\{ \begin{array}{ll} |
| 145 | + n & \mbox{ se } n<3, \\ |
| 146 | + f(n-1)f(n-2)+nf(n-3) & \mbox{ se } n\ge3. |
| 147 | +\end{array} \right. |
| 148 | +\end{equation*} |
| 149 | +\item {\tt Equal(As,Bs)}: prende in input due liste e restituisce {True} se sono uguali, {False} altrimenti. |
| 150 | +\item {\tt PrimiPrimi(n)}: prende in input un numero intero $n$ e restituisce una lista dei primi n numeri primi. |
| 151 | +\end{enumerate} |
| 152 | +Dando una stima in termini di O-grande della complessità dell'algoritmo. |
| 153 | + |
| 154 | +\end{enumerate} |
| 155 | + |
| 156 | +\end{document} |
0 commit comments