From 7082f840a48d17e1aa262b2fd504f8132b1986e4 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Wed, 8 Feb 2017 22:04:31 +0530 Subject: Fix several mistakes and update practice_ds. --- basic_python/core_data_structures.tex | 22 +++--- basic_python/practice_control_flow.tex | 7 +- basic_python/practice_ds.tex | 118 +++++++++++++++++++++++++-------- 3 files changed, 109 insertions(+), 38 deletions(-) (limited to 'basic_python') diff --git a/basic_python/core_data_structures.tex b/basic_python/core_data_structures.tex index 9de0954..7bfbb38 100644 --- a/basic_python/core_data_structures.tex +++ b/basic_python/core_data_structures.tex @@ -99,9 +99,9 @@ In []: num.index(2) Out[]: 2 In []: num.pop() -9 +4 In []: num -[1, 2, 2, 3, 3, 4, 8] +[9, 8, 2, 3, 1, 2, 3] \end{lstlisting} \end{frame} @@ -375,22 +375,22 @@ Out[]: \item Adding a new key-value pair \end{itemize} \begin{lstlisting} - In[]: extensions['c++'] = 'C++ code' - In[]: extensions + In[]: d['c++'] = 'C++ code' + In[]: d \end{lstlisting} \begin{itemize} \item Deleting a key-value pair \end{itemize} \begin{lstlisting} - In[]: del extension['java'] - In[]: extensions + In[]: del d['java'] + In[]: d \end{lstlisting} \begin{itemize} \item Assigning to existing key, modifies the value \end{itemize} \begin{lstlisting} - In[]: extensions['cpp'] = 'C++ source code' - In[]: extensions + In[]: d['cpp'] = 'C++ source code' + In[]: d \end{lstlisting} \end{frame} @@ -405,7 +405,7 @@ In []: d.get('junk') # No error! In []: d.update({'bin': 'Binary file', .....: 'exe': 'executable'}) -In []: d.clear()) +In []: d.clear() \end{lstlisting} \end{frame} @@ -429,6 +429,10 @@ Out[]: False \begin{frame}[fragile] \frametitle{Dictionaries: iteration} \begin{lstlisting} +In []: for v in d: +.....: print(v) +.....: + In []: for v in d.values(): .....: print(v) .....: diff --git a/basic_python/practice_control_flow.tex b/basic_python/practice_control_flow.tex index 5ce053f..4fa966b 100644 --- a/basic_python/practice_control_flow.tex +++ b/basic_python/practice_control_flow.tex @@ -138,6 +138,7 @@ from __future__ import print_function \item Use a while loop to generate the numbers from 1 to 10 \item If a number divisible by 3 is found, print *** \item If a number divisible by 5 is found, print ***** + \item Otherwise just print the number \end{enumerate} \end{frame} @@ -295,7 +296,7 @@ from __future__ import print_function \begin{lstlisting} from numpy import linspace n = int(input()) - data = linspace(0, 1, n) + data = linspace(1, 2, n) for x in data: print(x) \end{lstlisting} @@ -305,7 +306,7 @@ from __future__ import print_function \begin{frame}[plain,fragile] \frametitle{\typ{for} loop over list 2} \begin{enumerate} - \item Ask the user for an list of fruits separated by a space + \item Ask the user for an list of fruits separated by a comma \item Print each fruit in a separate line \end{enumerate} For example let us say user enters \typ{apple, pear}, print @@ -365,7 +366,7 @@ from __future__ import print_function \begin{frame}[fragile,plain] \frametitle{Solution} \begin{lstlisting} - n = input() + n = int(input()) for i in range(n): for j in range(n): print(i+j, end=' ') diff --git a/basic_python/practice_ds.tex b/basic_python/practice_ds.tex index 7931310..9f30319 100644 --- a/basic_python/practice_ds.tex +++ b/basic_python/practice_ds.tex @@ -251,6 +251,90 @@ In []: for x, y in data: \end{lstlisting} \end{frame} +\begin{frame}[fragile,plain,fragile] + \frametitle{Exercise: simple dictionaries} + \begin{enumerate} + \item Ask the user for a list of integers separated by spaces + \item For each integer, store the string version as the key and the square of + the integer value as the value in a dictionary. + \item Print the resulting dictionary + \end{enumerate} +For example if the user enters "1 3 5", print: +\begin{lstlisting} +{'1': 1, '3': 9, '5': 25} +\end{lstlisting} +Hint: simply print the resulting dictionary +\end{frame} + +\begin{frame}[fragile,plain] +\frametitle{Solution} +\begin{lstlisting} +text = input() +d = {} +for item in text.split(): + x = int(item) + d[item] = x*x +print(d) +\end{lstlisting} +\end{frame} + + +\begin{frame}[fragile,plain,fragile] + \frametitle{Exercise: mapping using dicts} + \begin{enumerate} + \item Create a mapping from 3 character month name to month number + \item Hint: for example \typ{\{'jan': 1, 'dec': 12\}} + \item Ask the user for a 3 character month code lower/upper mixed + \item Print the month number corresponding to the month the user entered. + \end{enumerate} +For example if the user enters "Jul", print: +\begin{lstlisting} +7 +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile,plain] +\frametitle{Solution} +\begin{lstlisting} +months = ('jan feb mar apr may jun jul ' + + 'aug sep oct nov dec').split() +month2mm = {} +for i in range(1, len(months)+1): + month2mm[months[i]] = i + +text = input() +mon = text[:3].lower() +print(month2mm[mon]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile,plain] +\frametitle{Aside: using \typ{enumerate} example} +\begin{lstlisting} +for i, char in enumerate('hello'): + print(i, char) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile,plain] +\frametitle{Aside: using \typ{enumerate}} +\begin{lstlisting} +months = ('jan feb mar apr may jun jul ' + + 'aug sep oct nov dec').split() +month2mm = {} +for i, month in enumerate(months): + month2mm[month] = i+1 + +# Is easier/nicer than this: + +for i in range(1, len(months)+1): + month2mm[months[i]] = i +\end{lstlisting} + +\end{frame} + + + \begin{frame}[plain,fragile] \frametitle{Exercise: dictionaries} \begin{enumerate} @@ -285,31 +369,6 @@ In []: for x, y in data: \end{lstlisting} \end{frame} -\begin{frame}[plain] - \frametitle{Problem: Day of the Week} - \begin{block}{Problem} - Given a list, \texttt{week}, containing names of the days of the - week and a string \texttt{s}, check if the string is a day of the - week. We should be able to check for any of the forms like, - \emph{sat, Sat, SAT} - \end{block} - \begin{itemize} - \item Ask the user for a string of length 3 - \item Check if the string is a day of the week - \item Print True or False - \end{itemize} -\end{frame} - -\begin{frame}[fragile, plain] - \frametitle{Solution} - \small - \begin{lstlisting} -week = 'mon tue wed thu fri sat sun'.split() -s = input().lower() -print(s in week) -\end{lstlisting} -\end{frame} - \begin{frame}[plain] {Problem: datestring to date tuple} @@ -329,7 +388,7 @@ print(s in week) months = ('jan feb mar apr may jun jul ' + 'aug sep oct nov dec').split() month2mm = {} -for i in range(len(months)): +for i in range(1, len(months)+1): month2mm[months[i]] = i date = input() @@ -342,4 +401,11 @@ mm = month2mm[mon] print((yyyy, mm, dd)) \end{lstlisting} \end{frame} + +\begin{frame} + \centering + \Huge + + That's all folks! +\end{frame} \end{document} -- cgit