summaryrefslogtreecommitdiff
path: root/day1
diff options
context:
space:
mode:
Diffstat (limited to 'day1')
-rw-r--r--day1/day1quiz1.tex158
-rw-r--r--day1/day1quiz2.tex226
-rw-r--r--day1/exercise/aliquot.py9
-rw-r--r--day1/exercise/collatz.py9
-rw-r--r--day1/exercise/datestring.py34
-rw-r--r--day1/exercise/pytriads.py14
-rw-r--r--day1/session1.tex2
-rw-r--r--day1/session4.tex13
8 files changed, 423 insertions, 42 deletions
diff --git a/day1/day1quiz1.tex b/day1/day1quiz1.tex
index d0da0dd..ee1fe83 100644
--- a/day1/day1quiz1.tex
+++ b/day1/day1quiz1.tex
@@ -58,4 +58,162 @@ On the top right hand corner please write down the following:
\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}
diff --git a/day1/day1quiz2.tex b/day1/day1quiz2.tex
index 05ab609..539e1e5 100644
--- a/day1/day1quiz2.tex
+++ b/day1/day1quiz2.tex
@@ -61,6 +61,232 @@ On the top right hand corner please write down the following:
\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}
diff --git a/day1/exercise/aliquot.py b/day1/exercise/aliquot.py
index 96ea916..25518f0 100644
--- a/day1/exercise/aliquot.py
+++ b/day1/exercise/aliquot.py
@@ -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? '))
diff --git a/day1/exercise/collatz.py b/day1/exercise/collatz.py
index 2b980d7..7bed967 100644
--- a/day1/exercise/collatz.py
+++ b/day1/exercise/collatz.py
@@ -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
diff --git a/day1/exercise/datestring.py b/day1/exercise/datestring.py
index d990904..4e8cad4 100644
--- a/day1/exercise/datestring.py
+++ b/day1/exercise/datestring.py
@@ -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,
- }
-
- dd, month, yyyy = date_str.split()
-
- mm = month2mm[month]
- return int(yyyy), int(dd.strip(',')), mm
+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 }
+COMMA = ','
+SPACE = ' '
date_str = raw_input('Enter a date string? ')
-print get_date_from_str(date_str)
+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 )
+
+print dd,mm, yyyy
diff --git a/day1/exercise/pytriads.py b/day1/exercise/pytriads.py
index 4c47313..bea543d 100644
--- a/day1/exercise/pytriads.py
+++ b/day1/exercise/pytriads.py
@@ -10,12 +10,8 @@ def gcd(a, b):
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
diff --git a/day1/session1.tex b/day1/session1.tex
index 9805f2e..a0a9595 100644
--- a/day1/session1.tex
+++ b/day1/session1.tex
@@ -562,7 +562,7 @@ is in the rectangle $(0, -1.5)$ (left bottom coordinate) and $(2\pi,
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}
diff --git a/day1/session4.tex b/day1/session4.tex
index ddac34a..257fd54 100644
--- a/day1/session4.tex
+++ b/day1/session4.tex
@@ -646,7 +646,7 @@ What will be printed?
\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 @@ array([[2,3],
\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}