Changes made during REC Chennai workshop.
--- a/day1/day1quiz1.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/day1quiz1.tex Tue Jan 12 19:03:34 2010 +0530
@@ -58,4 +58,162 @@
\end{itemize}
\end{frame}
+\begin{frame}[fragile]
+\frametitle{\incqno }
+Draw (roughly) the plot obtained by the following:
+\begin{lstlisting}
+In []: x = linspace(0, 2*pi, 3)
+In []: plot(x, sin(x))
+\end{lstlisting}
+\end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% Describe the plot produced by the following:
+%% \begin{lstlisting}
+%% In []: x = linspace(0, 2*pi, 50)
+%% In []: plot(x, cos(x), 'go')
+%% \end{lstlisting}
+%% \end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+How will you plot the previous graph with line width set to 3? How will
+you set the $x$ and $y$ labels of the plot?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+How will you set the x and y axis limits so that the region of interest
+is in the rectangle $(0, -1.5)$ (left bottom coordinate) and $(2\pi,
+1.5)$ (right top coordinate)?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+What ipython magic command do you use to obtain the lines of code you have already typed in the interpreter? What command do you use to save them?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+The following code snippet has an error/bug:
+\begin{lstlisting}
+In []: y = linspace(0, 2*pi, 50)
+In []: plot(y, sin(y))
+In []: clf()
+In []: plot(y, cos(y))
+In []: legend(['sin(y)', 'cos(y)'])
+\end{lstlisting}
+What is the error? How do you fix it?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+ \begin{lstlisting}
+ In []: a = [1, 2, 5, 9]
+ In []: a[0:-1]
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ How do you combine two lists \emph{a} and \emph{b} to produce one list?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+ \begin{lstlisting}
+ In []: a = [1, 2, 5, 9]
+ \end{lstlisting}
+ How do you add the value 10 to the end of this list?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+Write the code to read a file \texttt{data.txt} and print each line of it?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+What would be the result of the following code snippet:
+\begin{lstlisting}
+In []: x = linspace(0, 10, 50)
+In []: y = linspace(50, 100, 100)
+In []: plot(x, y)
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+The following code snippet has an error/bug:
+\begin{lstlisting}
+In []: l = [0.1, 0.2, 0.3, 0.4]
+In []: t = [0.69, 0.90, 1.19, 1.30]
+In []: tsq = []
+In []: for time in t:
+ ....: tsq.append(time*time)
+ ....: plot(l, tsq)
+\end{lstlisting}
+What is the error? How do you fix it?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ A sample line from a Comma Separated Values (CSV) file:\\
+ \vspace*{0.2in}
+ \emph{Rossum, Guido, 42, 56, 34, 54}\\
+ \vspace*{0.2in}
+ What code would you use to separate the line into fields?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+ \begin{lstlisting}
+ In []: a = [1, 2, 5, 9]
+ \end{lstlisting}
+ How do you find the length of this list?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+ \begin{lstlisting}
+ In [1]: d = {
+ 'a': 1,
+ 'b': 2
+ }
+ In [2]: print d['c']
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: sc = {'A': 10, 'B': 20,
+ 'C': 70}
+\end{lstlisting}
+Given the above dictionary, what command will you give to plot a
+pie-chart?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: marks = [10, 20, 30, 50, 55,
+ 75, 83]
+\end{lstlisting}
+Given the above marks, how will you calculate the \alert{mean} and
+\alert{standard deviation}?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: marks = [10, 20, 30, 50, 55,
+ 75, 83]
+\end{lstlisting}
+How will you convert the list \texttt{marks} to an \alert{array}?
+\end{frame}
+
\end{document}
--- a/day1/day1quiz2.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/day1quiz2.tex Tue Jan 12 19:03:34 2010 +0530
@@ -61,6 +61,232 @@
\end{itemize}
\end{frame}
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: a = array([[1, 2],
+ [3, 4]])
+In []: a[1,0] = 0
+\end{lstlisting}
+What is the resulting array?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+ In []: x = array(([1,2,3,4],
+ [2,3,4,5]))
+ In []: x[-2][-3] = 4
+ In []: print x
+\end{lstlisting}
+What will be printed?
+\end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% \begin{lstlisting}
+%% In []: x = array([[1,2,3,4],
+%% [3,4,2,5]])
+%% \end{lstlisting}
+%% What is the \lstinline+shape+ of this array?
+%% \end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+ In []: x = array([[1,2,3,4]])
+\end{lstlisting}
+How to change \lstinline+x+ to \lstinline+array([[1,2,0,4]])+?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+ In []: x = array([[1,2,3,4],
+ [3,4,2,5]])
+\end{lstlisting}
+How do you get the following slice of \lstinline+x+?
+\begin{lstlisting}
+array([[2,3],
+ [4,2]])
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+ In []: x = array([[9,18,27],
+ [30,60,90],
+ [14,7,1]])
+\end{lstlisting}
+What is the output of \lstinline+x[::3,::3]+
+\end{frame}
+
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: a = array([[1, 2],
+ [3, 4]])
+\end{lstlisting}
+How do you get the transpose of this array?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
+In []: a = array([[1, 2],
+ [3, 4]])
+In []: b = array([[1, 1],
+ [2, 2]])
+In []: a*b
+\end{lstlisting}
+What does this produce?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+What command do you use to find the inverse of a matrix and its
+eigenvalues?
+\end{frame}
+
+%% \begin{frame}
+%% \frametitle{\incqno }
+%% The file \lstinline+datafile.txt+ contains 3 columns of data. What
+%% command will you use to read the entire data file into an array?
+%% \end{frame}
+
+%% \begin{frame}
+%% \frametitle{\incqno }
+%% If the contents of the file \lstinline+datafile.txt+ is read into an
+%% $N\times3$ array called \lstinline+data+, how would you obtain the third
+%% column of this data?
+%% \end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+Given a 4x4 matrix \texttt{A} and a 4-vector \texttt{b}, what command do
+you use to solve for the equation \\
+\texttt{Ax = b}?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+What command will you use if you wish to integrate a system of ODEs?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+How do you calculate the roots of the polynomial, $y = 1 + 6x + 8x^2 +
+x^3$?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+Two arrays \lstinline+a+ and \lstinline+b+ are numerically almost equal, what command
+do you use to check if this is true?
+\end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% \begin{lstlisting}
+%% In []: x = arange(0, 1, 0.25)
+%% In []: print x
+%% \end{lstlisting}
+%% What will be printed?
+%% \end{frame}
+
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% \begin{lstlisting}
+%% from scipy.integrate import quad
+%% def f(x):
+%% res = x*cos(x)
+%% quad(f, 0, 1)
+%% \end{lstlisting}
+%% What changes will you make to the above code to make it work?
+%% \end{frame}
+
+%% \begin{frame}
+%% \frametitle{\incqno }
+%% What two commands will you use to create and evaluate a spline given
+%% some data?
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% What would be the result?
+%% \begin{lstlisting}
+%% In []: x
+%% array([[0, 1, 2],
+%% [3, 4, 5],
+%% [6, 7, 8]])
+%% In []: x[::-1,:]
+%% \end{lstlisting}
+%% Hint:
+%% \begin{lstlisting}
+%% In []: x = arange(9)
+%% In []: x[::-1]
+%% array([8, 7, 6, 5, 4, 3, 2, 1, 0])
+%% \end{lstlisting}
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% What would be the result?
+%% \begin{lstlisting}
+%% In []: y = arange(3)
+%% In []: x = linspace(0,3,3)
+%% In []: x-y
+%% \end{lstlisting}
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% \begin{lstlisting}
+%% In []: x
+%% array([[ 0, 1, 2, 3],
+%% [ 4, 5, 6, 7],
+%% [ 8, 9, 10, 11],
+%% [12, 13, 14, 15]])
+%% \end{lstlisting}
+%% How will you get the following \lstinline+x+?
+%% \begin{lstlisting}
+%% array([[ 5, 7],
+%% [ 9, 11]])
+%% \end{lstlisting}
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% What would be the output?
+%% \begin{lstlisting}
+%% In []: y = arange(4)
+%% In []: x = array(([1,2,3,2],[1,3,6,0]))
+%% In []: x + y
+%% \end{lstlisting}
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% \begin{lstlisting}
+%% In []: line = plot(x, sin(x))
+%% \end{lstlisting}
+%% Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2.
+%% \end{frame}
+
+%% \begin{frame}[fragile]
+%% \frametitle{\incqno }
+%% What would be the output?
+%% \begin{lstlisting}
+%% In []: x = arange(9)
+%% In []: y = arange(9.)
+%% In []: x == y
+%% \end{lstlisting}
+%% \end{frame}
+
+
\end{document}
--- a/day1/exercise/aliquot.py Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/exercise/aliquot.py Tue Jan 12 19:03:34 2010 +0530
@@ -1,18 +1,13 @@
-def is_perfect_square(n):
- i = 1
- while i * i < n:
- i += 1
- return i * i == n, i
def aliquot(n):
sum = 1
i = 2
- is_ps, root = is_perfect_square(n)
- while i < root:
+ while i * i < n:
if n % i == 0:
sum += i + (n / i)
i += 1
+ if n % i == 0: sum += i
return sum
n = int(raw_input('Enter a number? '))
--- a/day1/exercise/collatz.py Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/exercise/collatz.py Tue Jan 12 19:03:34 2010 +0530
@@ -1,7 +1,10 @@
-a = -1
-while a > 1:
+a = int( raw_input( 'Enter number: ') )
+while a != 4:
print a
- if a % 2:
+ if a % 2 == 1:
a = a * 3 + 1
else:
a /= 2
+print 4
+print 2
+print 1
--- a/day1/exercise/datestring.py Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/exercise/datestring.py Tue Jan 12 19:03:34 2010 +0530
@@ -1,23 +1,15 @@
-def get_date_from_str(date_str):
- month2mm = {
- 'January': 1,
- 'February': 2,
- 'March': 3,
- 'April': 4,
- 'May': 5,
- 'June': 6,
- 'July': 7,
- 'August': 8,
- 'September': 9,
- 'October': 10,
- 'November': 11,
- 'December': 12,
- }
+month2mm = { 'JAN': 1, 'FEB': 2, 'MAR': 3, 'APR': 4, 'MAY': 5,
+'JUN': 6, 'JUL': 7, 'AUG': 8, 'SEP': 9, 'OCT': 10, 'NOV': 11,
+'DEC': 12 }
- dd, month, yyyy = date_str.split()
+COMMA = ','
+SPACE = ' '
+date_str = raw_input('Enter a date string? ')
+date_str = date_str.replace( COMMA, SPACE)
+d, m, y = date_str.split()
+dd = int( d )
+mon = m[:3].upper()
+mm = month2mm[mon]
+yyyy = int( y )
- mm = month2mm[month]
- return int(yyyy), int(dd.strip(',')), mm
-
-date_str = raw_input('Enter a date string? ')
-print get_date_from_str(date_str)
+print dd,mm, yyyy
--- a/day1/exercise/pytriads.py Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/exercise/pytriads.py Tue Jan 12 19:03:34 2010 +0530
@@ -10,12 +10,8 @@
else:
return gcd(b, a%b)
-a = 3
-while a < 100:
- b = a + 1
- while b < 100:
- is_ps, c = is_perfect_square((a * a) + (b * b))
- if is_ps and gcd(a, b) == 1:
- print a, b, c
- b += 1
- a += 1
+for a in range(3, 501):
+ for b in range( a+1, 501, 2):
+ if gcd( a, b ) == 1:
+ is_ps, c = is_perfect_square((a * a) + (b * b))
+ if is_ps: print a, b, c
--- a/day1/session1.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/session1.tex Tue Jan 12 19:03:34 2010 +0530
@@ -562,7 +562,7 @@
What ipython magic command do you use to obtain the lines of code you have already typed in the interpreter? What command do you use to save them?
\end{frame}
-\begin{frame}
+\begin{frame}[fragile]
\frametitle{\incqno }
The following code snippet has an error/bug:
\begin{lstlisting}
--- a/day1/session4.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day1/session4.tex Tue Jan 12 19:03:34 2010 +0530
@@ -646,7 +646,7 @@
\begin{lstlisting}
In []: x = array([[1,2,3,4]])
\end{lstlisting}
-How to \lstinline+x+ to \lstinline+array([[1,2,0,4]])+?
+How to change \lstinline+x+ to \lstinline+array([[1,2,0,4]])+?
\end{frame}
\begin{frame}[fragile]
@@ -665,6 +665,17 @@
\begin{frame}[fragile]
\frametitle{\incqno }
\begin{lstlisting}
+ In []: x = array([[9,18,27],
+ [30,60,90],
+ [14,7,1]])
+\end{lstlisting}
+What is the output of \lstinline+x[::3,::3]+
+\end{frame}
+
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+\begin{lstlisting}
In []: a = array([[1, 2],
[3, 4]])
\end{lstlisting}
--- a/day2/cheatsheet3.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day2/cheatsheet3.tex Tue Jan 12 19:03:34 2010 +0530
@@ -105,7 +105,7 @@
\section{Self contained python script}
Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them.
\begin{lstlisting}
-from pylab import linspace, pi, sin
+from scipy import linspace, pi, sin
from pylab import plot, legend, annotate
from pylab import xlim, ylim
--- a/day2/day2quiz.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day2/day2quiz.tex Tue Jan 12 19:03:34 2010 +0530
@@ -60,5 +60,127 @@
\end{itemize}
\end{frame}
+\begin{frame}
+\frametitle{\incqno }
+ What is the largest integer value that can be represented natively by Python?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ What is the result of 17.0 / 2?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ Which of the following is not a type in Python?
+ \begin{enumerate}
+ \item int
+ \item float
+ \item char
+ \item string
+ \end{enumerate}
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+How do you create a complex number with real part 2 and imaginary part
+0.5.
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ What is the difference between \kwrd{print} \emph{x} and \kwrd{print} \emph{x,} ?
+\end{frame}
+
+\begin{frame}
+\frametitle{\incqno }
+ What does '*' * 40 produce?
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\incqno }
+ What is the output of:
+ \begin{lstlisting}
+In []: ', '.join(['a', 'b', 'c'])
+ \end{lstlisting}
+\end{frame}
+
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: 47 % 3
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}
+ \frametitle{\incqno}
+ How do you find the presence of an element \emph{x} in the list \emph{a}?
+\end{frame}
+
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: set([1, 2, 8, 2, 13, 8, 9])
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: a = {'a': 1, 'b': 2}
+In []: a['a'] = 10
+pIn []: print a
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: for i in range(3, 10, 2):
+ ...: print i
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: a = [1, 2, 3]
+In []: a.extend([5, 6])
+ \end{lstlisting}
+ What is the value of a?
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+In []: a = (1, 2, 3)
+In []: a[1] = 10
+ \end{lstlisting}
+ What is the result?
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{\incqno}
+ \begin{lstlisting}
+def func(x, y=10):
+ print x+1, y+10
+
+func(1)
+
+ \end{lstlisting}
+ What is the output?
+\end{frame}
+
+\begin{frame}
+ \frametitle{\incqno}
+ How many items can a function return?
+\end{frame}
+
\end{document}
--- a/day2/session3.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day2/session3.tex Tue Jan 12 19:03:34 2010 +0530
@@ -250,6 +250,23 @@
\begin{frame}[fragile]
\frametitle{Remedy \ldots}
\begin{lstlisting}
+from scipy import *
+ \end{lstlisting}
+\alert{Now run python four\_plot.py again}
+\end{frame}
+
+\begin{frame}[fragile]
+ \begin{lstlisting}
+Traceback (most recent call last):
+ File "four_plot.py", line 1, in <module>
+ x = plot(x, x, 'b')
+NameError: name 'plot' is not defined
+ \end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+ \frametitle{Remedy \ldots}
+ \begin{lstlisting}
from pylab import *
\end{lstlisting}
\alert{Now run python four\_plot.py again!!}
@@ -261,8 +278,8 @@
\item The \kwrd{import} keyword ``loads'' a module
\item One can also use:
\begin{lstlisting}
-In []: from pylab import *
-In []: from pylab import linspace
+In []: from scipy import *
+In []: from scipy import linspace
\end{lstlisting}
\item What is the difference?
\item \alert{Use the former only in interactive mode}
@@ -282,7 +299,7 @@
\frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
\small
\begin{lstlisting}
-from pylab import linspace, pi, sin
+from scipy import linspace, pi, sin
from pylab import plot, legend, annotate
from pylab import xlim, ylim
@@ -302,17 +319,18 @@
\frametitle{\texttt{from} \ldots \texttt{import} - conventional way!}
\small
\begin{lstlisting}
+import scipy
import pylab
-x = pylab.linspace(-5*pylab.pi, 5*pylab.pi, 500)
+x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
pylab.plot(x, x, 'b')
pylab.plot(x, -x, 'b')
-pylab.plot(x, pylab.sin(x), 'g', linewidth=2)
-pylab.plot(x, x*pylab.sin(x), 'r', linewidth=3)
+pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
+pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
pylab.annotate('origin', xy = (0, 0))
-pylab.xlim(-5*pylab.pi, 5*pylab.pi)
-pylab.ylim(-5*pylab.pi, 5*pylab.pi)
+pylab.xlim(-5*scipy.pi, 5*scipy.pi)
+pylab.ylim(-5*scipy.pi, 5*scipy.pi)
\end{lstlisting}
\end{frame}
--- a/day2/session5.tex Sun Jan 10 23:09:00 2010 +0530
+++ b/day2/session5.tex Tue Jan 12 19:03:34 2010 +0530
@@ -138,9 +138,17 @@
\inctime{10}
\end{frame}
-\begin{frame}
+
+\begin{frame}
\frametitle{Problem 3}
- Count frequencies of words in a file.
+Given a list of words, find all the anagrams in the list.
+Solve the problem without using dictionaries.
+
+\end{frame}
+
+\begin{frame}
+ \frametitle{Problem 4}
+ Count frequencies of words in a file named 'holmes.txt'.
\inctime{25}
\end{frame}