author | Madhusudan.C.S <madhusudancs@gmail.com> |
Fri, 02 Apr 2010 15:45:07 +0530 (2010-04-02) | |
changeset 453 | 38adf73fd123 |
parent 92 | d9c31aacd835 |
permissions | -rw-r--r-- |
62
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
1 |
\documentclass[12pt]{article} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
2 |
\title{Python Workshop\\Problems and Exercises} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
3 |
\author{Asokan Pichai\\Prabhu Ramachandran} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
4 |
\begin{document} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
\maketitle |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
6 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
7 |
\section{Python} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
8 |
\subsection{Getting started} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
9 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
10 |
>>> print 'Hello Python' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
11 |
>>> print 3124 * 126789 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
12 |
>>> 1786 % 12 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
>>> 3124 * 126789 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
14 |
>>> a = 3124 * 126789 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
15 |
>>> big = 12345678901234567890 ** 3 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
16 |
>>> verybig = big * big * big * big |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
17 |
>>> 12345**6, 12345**67, 12345**678 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
18 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
19 |
>>> s = 'Hello ' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
20 |
>>> p = 'World' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
21 |
>>> s + p |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
22 |
>>> s * 12 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
23 |
>>> s * s |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
>>> s + p * 12, (s + p)* 12 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
25 |
>>> s * 12 + p * 12 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
26 |
>>> 12 * s |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
27 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
28 |
\newpage |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
29 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
30 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
31 |
>>> 17/2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
32 |
>>> 17/2.0 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
33 |
>>> 17.0/2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
34 |
>>> 17.0/8.5 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
35 |
>>> int(17/2.0) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
36 |
>>> float(17/2) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
37 |
>>> str(17/2.0) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
38 |
>>> round( 7.5 ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
39 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
40 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
41 |
\subsection{Mini exercises} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
42 |
\begin{itemize} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
43 |
\item Round a float to the nearest integer, using \texttt{int()}? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
44 |
\item What does this do? \\\texttt{round(amount * 10) /10.0 } |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
45 |
\item How to round a number to the nearest 5 paise? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
46 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
47 |
\item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
48 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
49 |
\item How to round a number to the nearest 20 paise? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
50 |
\end{itemize} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
51 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
52 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
53 |
amount = 12.68 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
54 |
denom = 0.05 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
55 |
nCoins = round(amount/denom) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
56 |
rAmount = nCoins * denom |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
57 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
58 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
59 |
\subsection{Dynamic typing} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
60 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
61 |
a = 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
62 |
a = 1.1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
63 |
a = "Now I am a string!" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
64 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
65 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
66 |
\subsection{Comments} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
67 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
68 |
a = 1 # In-line comments |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
69 |
# Comment in a line to itself. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
70 |
a = "# This is not a comment!" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
71 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
72 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
73 |
\section{Data types} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
74 |
\subsection{Numbers} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
75 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
>>> a = 1 # Int. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
77 |
>>> l = 1000000L # Long |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
78 |
>>> e = 1.01325e5 # float |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
79 |
>>> f = 3.14159 # float |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
80 |
>>> c = 1+1j # Complex! |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
81 |
>>> print f*c/a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
82 |
(3.14159+3.14159j) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
83 |
>>> print c.real, c.imag |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
84 |
1.0 1.0 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
85 |
>>> abs(c) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
86 |
1.4142135623730951 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
87 |
>>> abs( 8 - 9.5 ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
88 |
1.5 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
89 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
90 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
91 |
\subsection{Boolean} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
92 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
93 |
>>> t = True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
94 |
>>> f = not t |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
95 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
96 |
>>> f or t |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
97 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
98 |
>>> f and t |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
99 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
100 |
>>> NOT True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
101 |
\ldots ??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
102 |
>>> not TRUE |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
103 |
\ldots ??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
104 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
105 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
106 |
\subsection{Relational and logical operators} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
107 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
108 |
>>> a, b, c = -1, 0, 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
109 |
>>> a == b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
110 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
111 |
>>> a <= b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
112 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
113 |
>>> a + b != c |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
114 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
115 |
>>> a < b < c |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
116 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
117 |
>>> c >= a + b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
118 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
119 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
120 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
121 |
\subsection{Strings} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
122 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
123 |
s = 'this is a string' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
124 |
s = 'This one has "quotes" inside!' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
125 |
s = "I have 'single-quotes' inside!" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
126 |
l = "A string spanning many lines\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
127 |
one more line\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
128 |
yet another" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
129 |
t = """A triple quoted string does |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
130 |
not need to be escaped at the end and |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
131 |
"can have nested quotes" etc.""" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
132 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
133 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
134 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
135 |
>>> w = "hello" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
136 |
>>> print w[0] + w[2] + w[-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
137 |
hlo |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
138 |
>>> len(w) # guess what |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
139 |
5 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
140 |
>>> s = u'Unicode strings!' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
141 |
>>> # Raw strings (note the leading 'r') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
142 |
... r_s = r'A string $\alpha \nu$' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
143 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
144 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
145 |
>>> w[0] = 'H' # Can't do that! |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
146 |
Traceback (most recent call last): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
147 |
File "<stdin>", line 1, in ? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
148 |
TypeError: object does not support item assignment |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
149 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
150 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
151 |
\subsection{IPython} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
152 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
153 |
In [1]: a = 'hello world' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
154 |
In [2]: a.startswith('hell') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
155 |
Out[2]: True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
156 |
In [3]: a.endswith('ld') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
157 |
Out[3]: True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
158 |
In [4]: a.upper() |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
159 |
Out[4]: 'HELLO WORLD' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
160 |
In [5]: a.upper().lower() |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
161 |
Out[5]: 'hello world' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
162 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
163 |
In [6]: a.split() |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
164 |
Out[6]: ['hello', 'world'] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
165 |
In [7]: ''.join(['a', 'b', 'c']) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
166 |
Out[7]: 'abc' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
167 |
In [8] 'd' in ''.join( 'a', 'b', 'c') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
168 |
Out[8]: False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
169 |
a.split( 'o' )} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
170 |
??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
171 |
'x'.join( a.split( 'o' ) ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
172 |
??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
173 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
174 |
In [11]: x, y = 1, 1.2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
175 |
In [12]: 'x is %s, y is %s' %(x, y) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
176 |
Out[12]: 'x is 1, y is 1.234' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
177 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
178 |
'x is \%d, y is \%f' \%(x, y) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
179 |
??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
180 |
'x is \%3d, y is \%4.2f' \%(x, y) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
181 |
??? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
182 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
183 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
184 |
\subsection{A classic problem} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
185 |
How to interchange values of two variables? Please note that the type of either variable is unknown and it is not necessary that both be of the same type even! |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
186 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
187 |
\subsection{Basic conditional flow} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
188 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
189 |
In [21]: a = 7 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
190 |
In [22]: b = 8 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
191 |
In [23]: if a > b: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
192 |
....: print 'Hello' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
193 |
....: else: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
194 |
....: print 'World' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
195 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
196 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
197 |
World |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
198 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
199 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
200 |
\subsection{\texttt{If...elif...else} example} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
201 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
202 |
x = int(raw_input("Enter an integer:")) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
203 |
if x < 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
204 |
print 'Be positive!' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
205 |
elif x == 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
206 |
print 'Zero' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
207 |
elif x == 1: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
208 |
print 'Single' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
209 |
else: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
210 |
print 'More' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
211 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
212 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
213 |
\subsection{Basic looping} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
214 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
215 |
# Fibonacci series: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
216 |
# the sum of two elements |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
217 |
# defines the next |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
218 |
a, b = 0, 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
219 |
while b < 10: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
220 |
print b, |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
221 |
a, b = b, a + b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
222 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
223 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
224 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
225 |
\section{Problem set 1} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
226 |
All the problems can be solved using \texttt{if} and \texttt{while} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
227 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
228 |
\item[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$\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
229 |
These are called $Armstrong$ numbers. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
230 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
231 |
\item[1.2 Collatz sequence] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
232 |
\begin{enumerate} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
233 |
\item Start with an arbitrary (positive) integer. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
234 |
\item If the number is even, divide by 2; if the number is odd multiply by 3 and add 1. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
235 |
\item Repeat the procedure with the new number. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
236 |
\item There is a cycle of 4, 2, 1 at which the procedure loops. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
237 |
\end{enumerate} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
238 |
Write a program that accepts the starting value and prints out the Collatz sequence. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
239 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
240 |
\item[1.3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
241 |
Write a program that prints the following pyramid on the screen. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
242 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
243 |
1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
244 |
2 2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
245 |
3 3 3 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
246 |
4 4 4 4 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
247 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
248 |
The number of lines must be obtained from the user as input.\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
249 |
When can your code fail? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
250 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
251 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
252 |
\subsection{Functions: examples} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
253 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
254 |
def signum( r ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
255 |
"""returns 0 if r is zero |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
256 |
-1 if r is negative |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
257 |
+1 if r is positive""" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
258 |
if r < 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
259 |
return -1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
260 |
elif r > 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
261 |
return 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
262 |
else: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
263 |
return 0 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
264 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
265 |
def pad( n, size ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
266 |
"""pads integer n with spaces |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
267 |
into a string of length size |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
268 |
""" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
269 |
SPACE = ' ' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
270 |
s = str( n ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
271 |
padSize = size - len( s ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
272 |
return padSize * SPACE + s |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
273 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
274 |
What about \%3d? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
275 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
276 |
\subsection {What does this function do?} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
277 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
278 |
def what( n ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
279 |
if n < 0: n = -n |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
280 |
while n > 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
281 |
if n % 2 == 1: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
282 |
return False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
283 |
n /= 10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
284 |
return True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
285 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
286 |
\newpage |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
287 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
288 |
\subsection{What does this function do?} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
289 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
290 |
def what( n ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
291 |
i = 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
292 |
while i * i < n: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
293 |
i += 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
294 |
return i * i == n, i |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
295 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
296 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
297 |
\subsection{What does this function do?} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
298 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
299 |
def what( n, x ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
300 |
z = 1.0 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
301 |
if n < 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
302 |
x = 1.0 / x |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
303 |
n = -n |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
304 |
while n > 0: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
305 |
if n % 2 == 1: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
306 |
z *= x |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
307 |
n /= 2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
308 |
x *= x |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
309 |
return z |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
310 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
311 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
312 |
\section{Problem set 2} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
313 |
The focus is on writing functions and calling them. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
314 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
315 |
\item[2.1] Write a function to return the gcd of two numbers. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
316 |
\item[2.2 Primitive Pythagorean Triads] 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. \\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
317 |
Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
318 |
\item[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). |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
319 |
\item[2.4 Aliquot] 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.\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
320 |
Write a function that returns the aliquot number of a given number. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
321 |
\item[2.5 Amicable pairs] A pair of numbers (a, b) is said to be \emph{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
322 |
Example: \texttt{220, 284}\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
323 |
Write a program that prints all five digit amicable pairs. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
324 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
325 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
326 |
\section{Lists} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
327 |
\subsection{List creation and indexing} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
328 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
329 |
>>> a = [] # An empty list. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
330 |
>>> a = [1, 2, 3, 4] # More useful. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
331 |
>>> len(a) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
332 |
4 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
333 |
>>> a[0] + a[1] + a[2] + a[-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
334 |
10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
335 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
336 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
337 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
338 |
>>> a[1:3] # A slice. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
339 |
[2, 3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
340 |
>>> a[1:-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
341 |
[2, 3, 4] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
342 |
>>> a[1:] == a[1:-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
343 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
344 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
345 |
Explain last result |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
346 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
347 |
\newpage |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
348 |
\subsection{List: more slices} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
349 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
350 |
>>> a[0:-1:2] # Notice the step! |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
351 |
[1, 3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
352 |
>>> a[::2] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
353 |
[1, 3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
354 |
>>> a[-1::-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
355 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
356 |
What do you think the last one will do?\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
357 |
\emph{Note: Strings also use same indexing and slicing.} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
358 |
\subsection{List: examples} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
359 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
360 |
>>> a = [1, 2, 3, 4] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
361 |
>>> a[:2] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
362 |
[1, 3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
363 |
>>> a[0:-1:2] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
364 |
[1, 3] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
365 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
366 |
\emph{Lists are mutable (unlike strings)} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
367 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
368 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
369 |
>>> a[1] = 20 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
370 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
371 |
[1, 20, 3, 4] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
372 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
373 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
374 |
\subsection{Lists are mutable and heterogenous} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
375 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
376 |
>>> a = ['spam', 'eggs', 100, 1234] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
377 |
>>> a[2] = a[2] + 23 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
378 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
379 |
['spam', 'eggs', 123, 1234] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
380 |
>>> a[0:2] = [1, 12] # Replace items |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
381 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
382 |
[1, 12, 123, 1234] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
383 |
>>> a[0:2] = [] # Remove items |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
384 |
>>> a.append( 12345 ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
385 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
386 |
[123, 1234, 12345] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
387 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
388 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
389 |
\subsection{List methods} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
390 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
391 |
>>> a = ['spam', 'eggs', 1, 12] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
392 |
>>> a.reverse() # in situ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
393 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
394 |
[12, 1, 'eggs', 'spam'] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
395 |
>>> a.append(['x', 1]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
396 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
397 |
[12, 1, 'eggs', 'spam', ['x', 1]] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
398 |
>>> a.extend([1,2]) # Extend the list. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
399 |
>>> a.remove( 'spam' ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
400 |
>>> a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
401 |
[12, 1, 'eggs', ['x', 1], 1, 2] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
402 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
403 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
404 |
\subsection{List containership} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
405 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
406 |
>>> a = ['cat', 'dog', 'rat', 'croc'] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
407 |
>>> 'dog' in a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
408 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
409 |
>>> 'snake' in a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
410 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
411 |
>>> 'snake' not in a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
412 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
413 |
>>> 'ell' in 'hello world' |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
414 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
415 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
416 |
\subsection{Tuples: immutable} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
417 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
418 |
>>> t = (0, 1, 2) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
419 |
>>> print t[0], t[1], t[2], t[-1] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
420 |
0 1 2 2 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
421 |
>>> t[0] = 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
422 |
Traceback (most recent call last): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
423 |
File "<stdin>", line 1, in ? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
424 |
TypeError: object does not support item assignment |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
425 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
426 |
Multiple return values are actually a tuple.\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
427 |
Exchange is tuple (un)packing |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
428 |
\subsection{\texttt{range()} function} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
429 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
430 |
>>> range(7) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
431 |
[0, 1, 2, 3, 4, 5, 6] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
432 |
>>> range( 3, 9) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
433 |
[3, 4, 5, 6, 7, 8] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
434 |
>>> range( 4, 17, 3) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
435 |
[4, 7, 10, 13, 16] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
436 |
>>> range( 5, 1, -1) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
437 |
[5, 4, 3, 2] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
438 |
>>> range( 8, 12, -1) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
439 |
[] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
440 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
441 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
442 |
\subsection{\texttt{for\ldots range(\ldots)} idiom} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
443 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
444 |
In [83]: for i in range(5): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
445 |
....: print i, i * i |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
446 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
447 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
448 |
0 0 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
449 |
1 1 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
450 |
2 4 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
451 |
3 9 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
452 |
4 16 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
453 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
454 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
455 |
\subsection{\texttt{for}: the list companion} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
456 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
457 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
458 |
In [84]: a = ['a', 'b', 'c'] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
459 |
In [85]: for x in a: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
460 |
....: print x, chr( ord(x) + 10 ) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
461 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
462 |
a k |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
463 |
b l |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
464 |
c m |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
465 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
466 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
467 |
\subsection{\texttt{for}: the list companion} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
468 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
469 |
In [89]: for p, ch in enumerate( a ): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
470 |
....: print p, ch |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
471 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
472 |
....: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
473 |
0 a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
474 |
1 b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
475 |
2 c |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
476 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
477 |
Try: \texttt{print enumerate(a)} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
478 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
479 |
\section{Problem set 3} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
480 |
As you can guess, idea is to use \texttt{for}! |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
481 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
482 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
483 |
\item[3.1] Which of the earlier problems is simpler when we use \texttt{for} instead of \texttt{while}? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
484 |
\item[3.2] Given an empty chessboard and one Bishop placed in any square, say (r, c), generate the list of all squares the Bishop could move to. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
485 |
\item[3.3] Given two real numbers \texttt{a, b}, and an integer \texttt{N}, write a |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
486 |
function named \texttt{linspace( a, b, N)} that returns an ordered list |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
487 |
of \texttt{N} points starting with \texttt{a} and ending in \texttt{b} and |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
488 |
equally spaced.\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
489 |
For example, \texttt{linspace(0, 5, 11)}, should return, \\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
490 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
491 |
[ 0.0 , 0.5, 1.0 , 1.5, 2.0 , 2.5, |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
492 |
3.0 , 3.5, 4.0 , 4.5, 5.0 ] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
493 |
\end{verbatim} |
92
d9c31aacd835
Made little changes to handout.tex.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
70
diff
changeset
|
494 |
\item[3.4] Use the \texttt{linspace} function and generate a list of N tuples of the form\\ |
62
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
495 |
\texttt{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions, |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
496 |
\begin{itemize} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
497 |
\item \texttt{f(x) = sin(x)} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
498 |
\item \texttt{f(x) = sin(x) + sin(10*x)}. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
499 |
\end{itemize} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
500 |
|
92
d9c31aacd835
Made little changes to handout.tex.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
70
diff
changeset
|
501 |
\item[3.5] Using the tuples generated earlier, determine the intervals where the roots of the functions lie. |
62
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
502 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
503 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
504 |
\section{IO} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
505 |
\subsection{Simple tokenizing and parsing} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
506 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
507 |
s = """The quick brown fox jumped |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
508 |
over the lazy dog""" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
509 |
for word in s.split(): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
510 |
print word.capitalize() |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
511 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
512 |
|
92
d9c31aacd835
Made little changes to handout.tex.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
70
diff
changeset
|
513 |
\section{Problem Set 4} |
62
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
514 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
515 |
\item[4.1] Given a string like, ``1, 3-7, 12, 15, 18-21'', produce the list\\ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
516 |
\texttt{[1,3,4,5,6,7,12,15,18,19,20,21]} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
517 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
518 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
519 |
\subsection{File handling} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
520 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
521 |
>>> f = open('/path/to/file_name') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
522 |
>>> data = f.read() # Read entire file. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
523 |
>>> line = f.readline() # Read one line. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
524 |
>>> f.close() # close the file. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
525 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
526 |
Writing files |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
527 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
528 |
>>> f = open('/path/to/file_name', 'w') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
529 |
>>> f.write('hello world\n') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
530 |
>>> f.close() |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
531 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
532 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
533 |
\subsection{File and \texttt{for}} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
534 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
535 |
>>> f = open('/path/to/file_name') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
536 |
>>> for line in f: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
537 |
... print line |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
538 |
... |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
539 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
540 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
541 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
542 |
\item[4.2] The given file has lakhs of records in the form: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
543 |
\texttt{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
544 |
Some entries may be empty. Read the data from this file and print the |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
545 |
name of the student with the maximum total marks. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
546 |
\item[4.3] For the same data file compute the average marks in different |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
547 |
subjects, the student with the maximum mark in each subject and also |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
548 |
the standard deviation of the marks. Do this efficiently. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
549 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
550 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
551 |
\section{Modules} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
552 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
553 |
>>> sqrt(2) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
554 |
Traceback (most recent call last): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
555 |
File "<stdin>", line 1, in <module> |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
556 |
NameError: name 'sqrt' is not defined |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
557 |
>>> import math |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
558 |
>>> math.sqrt(2) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
559 |
1.4142135623730951 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
560 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
561 |
>>> from math import sqrt |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
562 |
>>> from math import * |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
563 |
>>> from os.path import exists |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
564 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
565 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
566 |
\subsection{Modules: example} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
567 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
568 |
# --- arith.py --- |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
569 |
def gcd(a, b): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
570 |
if a%b == 0: return b |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
571 |
return gcd(b, a%b) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
572 |
def lcm(a, b): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
573 |
return a*b/gcd(a, b) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
574 |
# ------------------ |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
575 |
>>> import arith |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
576 |
>>> arith.gcd(26, 65) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
577 |
13 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
578 |
>>> arith.lcm(26, 65) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
579 |
130 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
580 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
581 |
\section{Problem set 5} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
582 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
583 |
\item[5.1] Put all the functions you have written so far as part of the problems |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
584 |
into one module called \texttt{iitb.py} and use this module from IPython. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
585 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
586 |
\newpage |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
587 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
588 |
\section{Data Structures} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
589 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
590 |
\subsection{Dictonary} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
591 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
592 |
>>>d = { 'Hitchhiker\'s guide' : 42, 'Terminator' : 'I\'ll be back'} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
593 |
>>>d['Terminator'] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
594 |
"I'll be back" |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
595 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
596 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
597 |
\subsection{Problem Set 6.1} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
598 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
599 |
\item[6.1.1] You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number a string and another number, with a comma sometimes separating the items.Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
600 |
\item[6.1.2] Count word frequencies in a file. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
601 |
\item[6.1.3] Find the most used Python keywords in your Python code (import keyword). |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
602 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
603 |
\subsection{Set} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
604 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
605 |
>>> f10 = set([1,2,3,5,8]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
606 |
>>> p10 = set([2,3,5,7]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
607 |
>>> f10|p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
608 |
set([1, 2, 3, 5, 7, 8]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
609 |
>>> f10&p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
610 |
set([2, 3, 5]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
611 |
>>> f10-p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
612 |
set([8, 1]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
613 |
>>> p10-f10, f10^p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
614 |
set([7]), set([1, 7, 8]) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
615 |
>>> set([2,3]) < p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
616 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
617 |
>>> set([2,3]) <= p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
618 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
619 |
>>> 2 in p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
620 |
True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
621 |
>>> 4 in p10 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
622 |
False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
623 |
>>> len(f10) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
624 |
5 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
625 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
626 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
627 |
\subsection{Problem Set 6.2} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
628 |
\begin{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
629 |
\item[6.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these? |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
630 |
\item[6.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range. |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
631 |
\end{description} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
632 |
\subsection{Fuctions: default arguments} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
633 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
634 |
def ask_ok(prompt, complaint='Yes or no!'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
635 |
while True: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
636 |
ok = raw_input(prompt) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
637 |
if ok in ('y', 'ye', 'yes'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
638 |
return True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
639 |
if ok in ('n', 'no', 'nop', |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
640 |
'nope'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
641 |
return False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
642 |
print complaint |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
643 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
644 |
ask_ok('?') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
645 |
ask_ok('?', '[Y/N]') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
646 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
647 |
\newpage |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
648 |
\subsection{Fuctions: keyword arguments} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
649 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
650 |
def ask_ok(prompt, complaint='Yes or no!'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
651 |
while True: |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
652 |
ok = raw_input(prompt) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
653 |
if ok in ('y', 'ye', 'yes'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
654 |
return True |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
655 |
if ok in ('n', 'no', 'nop', |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
656 |
'nope'): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
657 |
return False |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
658 |
print complaint |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
659 |
|
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
660 |
ask_ok(prompt='?') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
661 |
ask_ok(prompt='?', complaint='[y/n]') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
662 |
ask_ok(complaint='[y/n]', prompt='?') |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
663 |
\end{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
664 |
\subsection{List Comprehensions} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
665 |
Lets say we want to squares of all the numbers from 1 to 100 |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
666 |
\begin{verbatim} |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
667 |
squares = [] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
668 |
for i in range(1, 100): |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
669 |
squares.append(i * i) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
670 |
# list comprehension |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
671 |
squares = [i*i for i in range(1, 100) |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
672 |
if i % 10 in [1, 2, 5, 7]] |
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
673 |
\end{verbatim} |
70
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
674 |
\newpage |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
675 |
\section{Further Reference:} |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
676 |
\begin{itemize} |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
677 |
\item Most referred and trusted material for learning \emph{Python} language is available at docs.python.org/tutorial/ |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
678 |
\item ``may be one of the thinnest programming language books on my shelf, but it's also one of the best.'' -- \emph{Slashdot, AccordianGuy, September 8, 2004}- available at diveintopython.org/ |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
679 |
\item How to Think Like a Computer Scientist: Learning with Python available at http://www.openbookproject.net/thinkcs/python/english/ \\ |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
680 |
``The concepts covered here apply to all programming languages and to problem solving in general.'' -- \emph{Guido van Rossum, creator of Python} |
b138c4ac68e6
Added reference part in handouts for first day.
Shantanu <shantanu@fossee.in>
parents:
62
diff
changeset
|
681 |
\end{itemize} |
62
12bd6784d213
Forgot to add files, doing them now.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
682 |
\end{document} |