summaryrefslogtreecommitdiff
path: root/day1
diff options
context:
space:
mode:
authorMadhusudan.C.S2009-10-09 12:59:55 +0530
committerMadhusudan.C.S2009-10-09 12:59:55 +0530
commitd1bcdc4243d67495c1e23fdff1b528843472ac4d (patch)
treeef3d4f9df2ceb21bd708acf5c953f176f619eb24 /day1
parent5e3fb1f1e3c2bf7cad7160e020c8ad782fc224a6 (diff)
downloadworkshops-d1bcdc4243d67495c1e23fdff1b528843472ac4d.tar.gz
workshops-d1bcdc4243d67495c1e23fdff1b528843472ac4d.tar.bz2
workshops-d1bcdc4243d67495c1e23fdff1b528843472ac4d.zip
Final set of changes.
Diffstat (limited to 'day1')
-rw-r--r--day1/DebugginDiagram.pngbin9416 -> 8560 bytes
-rwxr-xr-xday1/Session-2.tex50
-rwxr-xr-xday1/Session-4.tex65
-rw-r--r--day1/debug_exercise.py16
-rw-r--r--day1/exercise/kwfreq.py4
5 files changed, 93 insertions, 42 deletions
diff --git a/day1/DebugginDiagram.png b/day1/DebugginDiagram.png
index 530d1a0..41af377 100644
--- a/day1/DebugginDiagram.png
+++ b/day1/DebugginDiagram.png
Binary files differ
diff --git a/day1/Session-2.tex b/day1/Session-2.tex
index 5c597d4..430f5b7 100755
--- a/day1/Session-2.tex
+++ b/day1/Session-2.tex
@@ -110,9 +110,9 @@
\titlepage
\end{frame}
-\section{Functions and basic data structures}
+\section{Control Flow}
-\subsection{Exercises on Control flow}
+\subsection{Exercises}
\begin{frame}
\frametitle{Problem set 1}
\begin{itemize}
@@ -123,7 +123,10 @@
\begin{frame}{Problem 1.1}
Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers $abc$ that have the property $abc = a^3 + b^3 + c^3$\\
+\begin{block}
+{Information...}
These are called $Armstrong$ numbers.
+\end{block}
\end{frame}
\begin{frame}{Problem 1.2 - Collatz sequence}
@@ -155,7 +158,8 @@ When can your code fail?
% TIME: 20 m, running 20m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{Functions}
+\section{Functions}
+\subsection{Defining}
\begin{frame}[fragile]
\frametitle{Functions: examples}
\begin{lstlisting}
@@ -229,10 +233,11 @@ def what( n, x ):
\end{lstlisting}
\end{frame}
+\subsection{Built-in functions}
\begin{frame}
{Before writing a function}
\begin{itemize}
- \item Builtin functions for various and sundry
+ \item Variety of builtin functions are available
\item \typ{abs, any, all, len, max, min}
\item \typ{pow, range, sum, type}
\item Refer here:
@@ -244,32 +249,29 @@ def what( n, x ):
% TIME: 10 m, running 30m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\begin{frame}{Problem set 2}
- The focus is on writing functions and calling them.
-\end{frame}
-
-\begin{frame}{Problem 2.1}
+\subsection{Exercises}
+\begin{frame}{Problem set 2: Problem 2.1}
Write a function to return the gcd of two numbers.
\end{frame}
\begin{frame}{Problem 2.2}
-A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and should not be displayed by our program. \\
-Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100
+Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
+A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive.
\end{frame}
\begin{frame}{Problem 2.3}
- Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\\For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).
+ Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).}
\end{frame}
\begin{frame}{Problem 2.4}
- The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
+ The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
Write a function that returns the aliquot number of a given number.
\end{frame}
\begin{frame}{Problem 2.5}
A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
Example: \texttt{220, 284}\\
- Write a program that prints all five digit amicable pairs.
+ Write a program that prints all four digit amicable pairs.
\inctime{25}
\end{frame}
@@ -277,8 +279,9 @@ Write a program to print primitive pythagorean triads. The program should genera
% TIME: 25 m, running 55m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\subsection{Lists}
+\section{Lists}
+\subsection{Manipulating}
\begin{frame}[fragile]
\frametitle{List creation and indexing}
\begin{lstlisting}
@@ -286,8 +289,8 @@ Write a program to print primitive pythagorean triads. The program should genera
>>> a = [1, 2, 3, 4] # More useful.
>>> len(a)
4
->>> a[0] + a[1] + a[2] + a[-1]
-10
+>>> a[0] + a[1] + a[-1]
+7
\end{lstlisting}
\begin{itemize}
\item Indices start with ?
@@ -366,6 +369,7 @@ What do you think the last one will do?
% TIME: 10 m, running 65m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\subsection{Methods}
\begin{frame}[fragile]
\frametitle{List methods}
\begin{lstlisting}
@@ -403,6 +407,7 @@ True
% TIME: 5 m, running 70m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\section{Tuples}
\begin{frame}[fragile]
\frametitle{Tuples: immutable}
\begin{lstlisting}
@@ -412,7 +417,8 @@ True
>>> t[0] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
-TypeError: object does not support item assignment
+TypeError: object does not support
+item assignment
\end{lstlisting}
\begin{itemize}
\item Multiple return values are actually a tuple.
@@ -425,6 +431,7 @@ TypeError: object does not support item assignment
% TIME: 5 m, running 75m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\section{for and range()}
\begin{frame}[fragile]
\frametitle{\typ{range()} function}
\begin{lstlisting}
@@ -494,12 +501,13 @@ Try: \typ{print enumerate(a)}
\begin{frame}
\frametitle{What did we learn?}
\begin{itemize}
- \item Defining functions and calling them
- \item Lists: Creating, Indexing, Slicing and List methods
+ \item Control flow in action
+ \item Functions
+ \item Manipulating Lists
\item Tuples
\item range() function
\item for loops
- \item iterating lists with for, for...range()
+ \item for...range() idiom
\end{itemize}
\end{frame}
\end{document}
diff --git a/day1/Session-4.tex b/day1/Session-4.tex
index 8dd7150..c80d566 100755
--- a/day1/Session-4.tex
+++ b/day1/Session-4.tex
@@ -112,7 +112,7 @@
\titlepage
\end{frame}
-\section{Advanced Data structures, Functions and Debugging}
+\section{Advanced Data structures}
\subsection{Dictionary}
\begin{frame}{Dictionary}
@@ -120,7 +120,7 @@
\item lists and tuples index: 0 \ldots n
\item dictionaries index using strings
\item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
- \item \typ{d[``Terminator'']\\``I'll be back''}
+ \item \typ{d[``Terminator''] => ``I'll be back''}
\item aka associative array, key-value pair, hashmap, hashtable \ldots
\item what can be keys?
\end{itemize}
@@ -131,7 +131,7 @@
\item \alert{Unordered}
\begin{block}{Standard usage}
for key in dict:\\
- <use> dict[key] \# => value
+ \ \ \ \ print dict[key]
\end{block}
\item \typ{d.keys()} returns a list
\item can we have duplicate keys?
@@ -189,7 +189,6 @@ False
\inctime{5}
\end{frame}
-
\begin{frame}
\frametitle{Problem set 6.2}
\begin{description}
@@ -199,7 +198,8 @@ False
\inctime{10}
\end{frame}
-\subsection{Functions Reloaded!}
+
+\section{Functions Reloaded!}
\begin{frame}[fragile]
\frametitle{Advanced functions}
\begin{itemize}
@@ -211,6 +211,7 @@ False
\end{itemize}
\end{frame}
+\subsection{Default arguments}
\begin{frame}[fragile]
\frametitle{Functions: default arguments}
\small
@@ -230,6 +231,7 @@ ask_ok('?', '[Y/N]')
\end{lstlisting}
\end{frame}
+\subsection{Keyword arguments}
\begin{frame}[fragile]
\frametitle{Functions: keyword arguments}
\small
@@ -251,7 +253,7 @@ ask_ok(complaint='[y/n]', prompt='?')
\inctime{15}
\end{frame}
-\subsection{Functional programming}
+\section{Functional programming}
\begin{frame}[fragile]
\frametitle{Functional programming}
\begin{itemize}
@@ -263,6 +265,7 @@ ask_ok(complaint='[y/n]', prompt='?')
\end{itemize}
\end{frame}
+\subsection{List comprehensions}
\begin{frame}[fragile]
\frametitle{List Comprehensions}
Lets say we want to squares of all the numbers from 1 to 100
@@ -294,11 +297,22 @@ squares = [i*i for i in range(1, 100)
if i % 10 in [1, 2, 5, 7]]
\end{lstlisting}
Which is more readable?
-\inctime{15}
\end{frame}
+\begin{frame}[fragile]
+ \frametitle{map() function}
+ map() function accomplishes the same as list comprehensions
+ \begin{lstlisting}
+>>> def square(x): return x*x
+...
+>>> map(square, range(1, 100))
+[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
+ \end{lstlisting}
+\inctime{15}
+\end{frame}
-\subsection{Debugging}
+\section{Debugging}
+\subsection{Errors and Exceptions}
\begin{frame}[fragile]
\frametitle{Errors}
\begin{lstlisting}
@@ -340,6 +354,7 @@ or modulo by zero
\end{lstlisting}
\end{frame}
+\subsection{Strategy}
\begin{frame}[fragile]
\frametitle{Debugging effectively}
\begin{itemize}
@@ -352,7 +367,7 @@ or modulo by zero
\begin{frame}[fragile]
\frametitle{Debugging effectively}
\begin{itemize}
- \item Using \typ{\%debug} and \typ{\%pdb} in IPython
+ \item Using \typ{\%debug} in IPython
\end{itemize}
\end{frame}
@@ -364,33 +379,51 @@ In [1]: import mymodule
In [2]: mymodule.test()
---------------------------------------------
NameError Traceback (most recent call last)
-/media/python/iitb/workshops/day1/<ipython console> in <module>()
-/media/python/iitb/workshops/day1/mymodule.py in test()
+<ipython console> in <module>()
+mymodule.py in test()
1 def test():
----> 2 print spam
NameError: global name 'spam' is not defined
+
In [3]: %debug
-> /media/python/iitb/workshops/day1/mymodule.py(2)test()
+> mymodule.py(2)test()
0 print spam
ipdb>
\end{lstlisting}
\inctime{15}
\end{frame}
+\subsection{Exercise}
\begin{frame}[fragile]
\frametitle{Debugging: Exercise}
+\small
+\begin{lstlisting}
+import keyword
+f = open('/path/to/file')
+
+freq = {}
+for line in f:
+ words = line.split()
+ for word in words:
+ key = word.strip(',.!;?()[]: ')
+ if keyword.iskeyword(key):
+ value = freq[key]
+ freq[key] = value + 1
+
+print freq
+\end{lstlisting}
\inctime{10}
\end{frame}
\begin{frame}
\frametitle{What did we learn?}
\begin{itemize}
- \item Creating and using Dictionaries
- \item Creating and using Sets
- \item Advances Functions: default arguments, keyword arguments
+ \item Dictionaries
+ \item Sets
+ \item Default and keyword arguments
\item Functional Programming, list comprehensions
\item Errors and Exceptions in Python
- \item Debugging: \%pdb and \%debug in IPython
+ \item Debugging: \%debug in IPython
\end{itemize}
\end{frame}
\end{document}
diff --git a/day1/debug_exercise.py b/day1/debug_exercise.py
index 8b0ff22..b8ae313 100644
--- a/day1/debug_exercise.py
+++ b/day1/debug_exercise.py
@@ -1,3 +1,13 @@
-def no_bug():
- for i in range(10):
- if
+import keyword
+f = open('/path/to/file')
+
+freq = {}
+for line in f:
+ words = line.split()
+ for word in words:
+ key = word.strip(',.!;?()[]: ')
+ if keyword.iskeyword(key):
+ value = freq[key]
+ freq[key] = value + 1
+
+print freq
diff --git a/day1/exercise/kwfreq.py b/day1/exercise/kwfreq.py
index 32a8765..0da7ff3 100644
--- a/day1/exercise/kwfreq.py
+++ b/day1/exercise/kwfreq.py
@@ -1,5 +1,5 @@
import keyword
-f = open('/home/madhu/pyprogs/pytriads.py')
+f = open('/path/to/file')
freq = {}
for line in f:
@@ -7,7 +7,7 @@ for line in f:
for word in words:
key = word.strip(',.!;?()[]: ')
if keyword.iskeyword(key):
- value = freq.get(key, 1)
+ value = freq[key]
freq[key] = value + 1
print freq