day1/Problems.tex
author Madhusudan.C.S <madhusudancs@gmail.com>
Wed, 07 Oct 2009 00:43:37 +0530
changeset 46 63704b5650f1
parent 0 9243d75024cc
permissions -rwxr-xr-x
Changed the copyright and Institute for all the sessions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
\documentclass[12pt]{article}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
\title{Python Workshop\\Problems and Exercises}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
\author{Asokan Pichai\\Prabhu Ramachandran}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
\begin{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
\maketitle
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
\section{Python}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
\subsection{Getting started}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
   \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
>>> print 'Hello Python' 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
>>> print 3124 * 126789
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
>>> 1786 % 12
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
>>> 3124 * 126789
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
>>> a = 3124 * 126789
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
>>> big = 12345678901234567890 ** 3
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
>>> verybig = big * big * big * big 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
>>> 12345**6, 12345**67, 12345**678
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
>>> s = 'Hello '
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
>>> p = 'World'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
>>> s + p 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
>>> s * 12 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
>>> s * s
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
>>> s + p * 12, (s + p)* 12
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
>>> s * 12 + p * 12
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
>>> 12 * s 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
\newpage
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
>>> 17/2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
>>> 17/2.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
>>> 17.0/2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
>>> 17.0/8.5
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
>>> int(17/2.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
>>> float(17/2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
>>> str(17/2.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
>>> round( 7.5 )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
\subsection{Mini exercises}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
\begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
  \item Round a float to the nearest integer, using \texttt{int()}?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
  \item What does this do?  \\\texttt{round(amount * 10) /10.0 }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
  \item How to round a number to the nearest  5 paise?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
    \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
      \item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
    \end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
  \item How to round a number to the nearest 20 paise?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
\end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
    amount = 12.68
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
    denom = 0.05
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
    nCoins = round(amount/denom)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
    rAmount = nCoins * denom
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
\subsection{Dynamic typing}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
a = 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
a = 1.1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
a = "Now I am a string!"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
\subsection{Comments}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
a = 1  # In-line comments
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
# Comment in a line to itself.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
a = "# This is not a comment!"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
\section{Data types}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
\subsection{Numbers}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
>>> a = 1 # Int.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
>>> l = 1000000L # Long
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
>>> e = 1.01325e5 # float
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
>>> f = 3.14159 # float
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
>>> c = 1+1j # Complex!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
>>> print f*c/a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
(3.14159+3.14159j)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
>>> print c.real, c.imag
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
1.0 1.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
>>> abs(c)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
1.4142135623730951
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
>>> abs( 8 - 9.5 )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
1.5
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
\subsection{Boolean}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
>>> t = True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
>>> f = not t
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
>>> f or t
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
>>> f and t
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
>>>  NOT True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
\ldots ???
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
>>>  not TRUE
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
\ldots ???
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
\subsection{Relational and logical operators}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
>>> a, b, c = -1, 0, 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
>>> a == b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   110
False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
>>> a <= b 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
>>> a + b != c
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
>>> a < b < c
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
>>> c >= a + b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   118
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   119
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   121
\subsection{Strings}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   123
s = 'this is a string'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   124
s = 'This one has "quotes" inside!'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
s = "I have 'single-quotes' inside!"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
l = "A string spanning many lines\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   127
one more line\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   128
yet another"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   129
t = """A triple quoted string does
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   130
not need to be escaped at the end and 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   131
"can have nested quotes" etc."""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   132
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   133
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   134
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   135
>>> w = "hello"    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   136
>>> print w[0] + w[2] + w[-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   137
hlo
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   138
>>> len(w) # guess what
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   139
5
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   140
>>> s = u'Unicode strings!'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   141
>>> # Raw strings (note the leading 'r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   142
... r_s = r'A string $\alpha \nu$'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   143
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   144
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
>>> w[0] = 'H' # Can't do that!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   147
  File "<stdin>", line 1, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   148
TypeError: object does not support item assignment
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   149
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   150
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   151
  \subsection{IPython}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   152
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   153
In [1]: a = 'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   154
In [2]: a.startswith('hell')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   155
Out[2]: True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   156
In [3]: a.endswith('ld')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   157
Out[3]: True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   158
In [4]: a.upper()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   159
Out[4]: 'HELLO WORLD'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   160
In [5]: a.upper().lower()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   161
Out[5]: 'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   162
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   163
In [6]: a.split()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   164
Out[6]: ['hello', 'world']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   165
In [7]: ''.join(['a', 'b', 'c'])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   166
Out[7]: 'abc'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   167
In [8] 'd' in ''.join( 'a', 'b', 'c')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   168
Out[8]: False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   169
a.split( 'o' )}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   170
???
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   171
'x'.join( a.split( 'o' ) )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   172
???
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   173
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   174
In [11]: x, y = 1, 1.2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   175
In [12]: 'x is %s, y is %s' %(x, y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   176
Out[12]: 'x is 1, y is 1.234'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   177
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   178
'x is \%d, y is \%f' \%(x, y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   179
???
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   180
'x is \%3d, y is \%4.2f' \%(x, y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   181
??? 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   182
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   183
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   184
\subsection{A classic problem}
9243d75024cc Initial data added.
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!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   186
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   187
\subsection{Basic conditional flow}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   188
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   189
In [21]: a = 7
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   190
In [22]: b = 8
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   191
In [23]: if a > b:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   192
   ....:    print 'Hello'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   193
   ....: else:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   194
   ....:     print 'World'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   195
   ....:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   196
   ....:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   197
World
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   198
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   199
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   200
\subsection{\texttt{If...elif...else} example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   201
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   202
x = int(raw_input("Enter an integer:"))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   203
if x < 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   204
     print 'Be positive!'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   205
elif x == 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   206
     print 'Zero'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   207
elif x == 1:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   208
     print 'Single'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   209
else:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   210
     print 'More'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   211
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   212
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   213
\subsection{Basic looping}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   214
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   215
# Fibonacci series:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   216
# the sum of two elements
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   217
# defines the next
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   218
a, b = 0, 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   219
while b < 10:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   220
    print b,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   221
    a, b = b, a + b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   222
 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   223
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   224
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   225
\section{Problem set 1}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   226
All the problems can be solved using \texttt{if} and \texttt{while} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   227
\begin{description}
9243d75024cc Initial data added.
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$\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   229
These are called $Armstrong$ numbers.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   230
  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   231
\item[1.2 Collatz sequence]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   232
\begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   233
  \item Start with an arbitrary (positive) integer. 
9243d75024cc Initial data added.
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.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   235
  \item Repeat the procedure with the new number.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   236
  \item There is a cycle of 4, 2, 1 at which the procedure loops.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   237
\end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   238
    Write a program that accepts the starting value and prints out the Collatz sequence.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   239
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   240
  \item[1.3 Kaprekar's constant]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   241
  \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   242
    \item Take a four digit number--with at least two digits different.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   243
    \item Arrange the digits in ascending and descending order, giving A and D respectively.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   244
    \item Leave leading zeros in A!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   245
    \item Subtract A from D.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   246
    \item With the result, repeat from step 2.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   247
  \end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   248
  Write a program to accept a 4-digit number and display the progression to Kaprekar's constant.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   249
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   250
\item[1.4]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   251
  Write a program that prints the following pyramid on the screen. 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   252
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   253
1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   254
2  2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   255
3  3  3
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   256
4  4  4  4
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   257
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   258
The number of lines must be obtained from the user as input.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   259
When can your code fail?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   260
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   261
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   262
\subsection{Functions: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   263
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   264
def signum( r ):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   265
    """returns 0 if r is zero
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   266
    -1 if r is negative
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   267
    +1 if r is positive"""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   268
    if r < 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   269
        return -1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   270
    elif r > 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   271
        return 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   272
    else:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   273
        return 0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   274
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   275
def pad( n, size ): 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   276
    """pads integer n with spaces
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   277
    into a string of length size
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   278
    """
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   279
    SPACE = ' '
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   280
    s = str( n )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   281
    padSize = size - len( s )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   282
    return padSize * SPACE + s
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   283
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   284
What about \%3d?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   285
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   286
\subsection  {What does this function do?}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   287
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   288
def what( n ):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   289
    if n < 0: n = -n
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   290
    while n > 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   291
        if n % 2 == 1:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   292
            return False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   293
        n /= 10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   294
    return True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   295
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   296
\newpage
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   297
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   298
\subsection{What does this function do?}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   299
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   300
def what( n ):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   301
    i = 1    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   302
    while i * i < n:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   303
        i += 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   304
    return i * i == n, i
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   305
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   306
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   307
\subsection{What does this function do?}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   308
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   309
def what( n, x ):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   310
    z = 1.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   311
    if n < 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   312
        x = 1.0 / x
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   313
        n = -n
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   314
    while n > 0:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   315
        if n % 2 == 1:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   316
            z *= x
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   317
        n /= 2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   318
        x *= x
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   319
    return z
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   320
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   321
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   322
\section{Problem set 2}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   323
  The focus is on writing functions and calling them.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   324
\begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   325
  \item[2.1] Write a function to return the gcd of two numbers.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   326
  \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. \\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   327
Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   328
\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).
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   329
\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.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   330
  Write a function that returns the aliquot number of a given number. 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   331
\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.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   332
  Example: \texttt{220, 284}\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   333
  Write a program that prints all five digit amicable pairs.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   334
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   335
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   336
\section{Lists}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   337
\subsection{List creation and indexing}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   338
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   339
>>> a = [] # An empty list.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   340
>>> a = [1, 2, 3, 4] # More useful.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   341
>>> len(a) 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   342
4
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   343
>>> a[0] + a[1] + a[2] + a[-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   344
10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   345
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   346
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   347
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   348
>>> a[1:3] # A slice.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   349
[2, 3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   350
>>> a[1:-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   351
[2, 3, 4]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   352
>>> a[1:] == a[1:-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   353
False  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   354
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   355
Explain last result
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   356
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   357
\newpage
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   358
\subsection{List: more slices}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   359
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   360
>>> a[0:-1:2] # Notice the step!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   361
[1, 3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   362
>>> a[::2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   363
[1, 3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   364
>>> a[-1::-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   365
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   366
What do you think the last one will do?\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   367
\emph{Note: Strings also use same indexing and slicing.}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   368
  \subsection{List: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   369
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   370
>>> a = [1, 2, 3, 4]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   371
>>> a[:2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   372
[1, 3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   373
>>> a[0:-1:2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   374
[1, 3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   375
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   376
\emph{Lists are mutable (unlike strings)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   377
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   378
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   379
>>> a[1] = 20
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   380
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   381
[1, 20, 3, 4]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   382
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   383
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   384
  \subsection{Lists are mutable and heterogenous}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   385
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   386
>>> a = ['spam', 'eggs', 100, 1234]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   387
>>> a[2] = a[2] + 23
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   388
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   389
['spam', 'eggs', 123, 1234]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   390
>>> a[0:2] = [1, 12] # Replace items
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   391
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   392
[1, 12, 123, 1234]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   393
>>> a[0:2] = [] # Remove items
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   394
>>> a.append( 12345 )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   395
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   396
[123, 1234, 12345]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   397
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   398
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   399
  \subsection{List methods}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   400
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   401
>>> a = ['spam', 'eggs', 1, 12]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   402
>>> a.reverse() # in situ
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   403
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   404
[12, 1, 'eggs', 'spam']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   405
>>> a.append(['x', 1]) 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   406
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   407
[12, 1, 'eggs', 'spam', ['x', 1]]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   408
>>> a.extend([1,2]) # Extend the list.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   409
>>> a.remove( 'spam' )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   410
>>> a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   411
[12, 1, 'eggs', ['x', 1], 1, 2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   412
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   413
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   414
  \subsection{List containership}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   415
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   416
>>> a = ['cat', 'dog', 'rat', 'croc']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   417
>>> 'dog' in a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   418
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   419
>>> 'snake' in a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   420
False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   421
>>> 'snake' not in a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   422
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   423
>>> 'ell' in 'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   424
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   425
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   426
  \subsection{Tuples: immutable}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   427
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   428
>>> t = (0, 1, 2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   429
>>> print t[0], t[1], t[2], t[-1] 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   430
0 1 2 2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   431
>>> t[0] = 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   432
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   433
  File "<stdin>", line 1, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   434
TypeError: object does not support item assignment
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   435
\end{verbatim}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   436
    Multiple return values are actually a tuple.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   437
    Exchange is tuple (un)packing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   438
  \subsection{\texttt{range()} function}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   439
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   440
>>> range(7)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   441
[0, 1, 2, 3, 4, 5, 6]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   442
>>> range( 3, 9)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   443
[3, 4, 5, 6, 7, 8]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   444
>>> range( 4, 17, 3)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   445
[4, 7, 10, 13, 16]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   446
>>> range( 5, 1, -1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   447
[5, 4, 3, 2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   448
>>> range( 8, 12, -1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   449
[]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   450
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   451
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   452
  \subsection{\texttt{for\ldots range(\ldots)} idiom}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   453
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   454
In [83]: for i in range(5):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   455
   ....:     print i, i * i
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   456
   ....:     
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   457
   ....:     
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   458
0 0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   459
1 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   460
2 4
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   461
3 9
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   462
4 16
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   463
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   464
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   465
  \subsection{\texttt{for}: the list companion}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   466
  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   467
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   468
In [84]: a = ['a', 'b', 'c']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   469
In [85]: for x in a:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   470
   ....:    print x, chr( ord(x) + 10 )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   471
   ....:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   472
a  k
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   473
b  l
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   474
c  m
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   475
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   476
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   477
  \subsection{\texttt{for}: the list companion}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   478
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   479
In [89]: for p, ch in enumerate( a ):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   480
   ....:     print p, ch
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   481
   ....:     
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   482
   ....:     
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   483
0 a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   484
1 b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   485
2 c
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   486
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   487
Try: \texttt{print enumerate(a)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   488
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   489
\section{Problem set 3}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   490
  As you can guess, idea is to use \texttt{for}!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   491
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   492
\begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   493
  \item[3.1] Which of the earlier problems is simpler when we use \texttt{for} instead of \texttt{while}? 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   494
  \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.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   495
  \item[3.3] Given two real numbers \texttt{a, b}, and an integer \texttt{N}, write a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   496
  function named \texttt{linspace( a, b, N)} that returns an ordered list
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   497
  of \texttt{N} points starting with \texttt{a} and ending in \texttt{b} and
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   498
  equally spaced.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   499
  For example, \texttt{linspace(0, 5, 11)}, should return, \\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   500
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   501
[ 0.0 ,  0.5,  1.0 ,  1.5,  2.0 ,  2.5,  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   502
  3.0 ,  3.5,  4.0 ,  4.5,  5.0 ]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   503
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   504
  \item[3.4a] Use the \texttt{linspace} function and generate a list of N tuples of the form\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   505
\texttt{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   506
\begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   507
  \item \texttt{f(x) = sin(x)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   508
  \item \texttt{f(x) = sin(x) + sin(10*x)}.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   509
\end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   510
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   511
\item[3.4b] Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   512
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   513
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   514
\section{IO}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   515
  \subsection{Simple tokenizing and parsing}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   516
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   517
s = """The quick brown fox jumped
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   518
       over the lazy dog"""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   519
for word in s.split():
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   520
    print word.capitalize()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   521
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   522
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   523
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   524
    \item[4.1] Given a string like, ``1, 3-7, 12, 15, 18-21'', produce the list \texttt{[1,3,4,5,6,7,12,15,18,19,20,21]}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   525
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   526
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   527
  \subsection{File handling}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   528
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   529
>>> f = open('/path/to/file_name')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   530
>>> data = f.read() # Read entire file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   531
>>> line = f.readline() # Read one line.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   532
>>> f.close() # close the file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   533
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   534
Writing files
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   535
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   536
>>> f = open('/path/to/file_name', 'w')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   537
>>> f.write('hello world\n')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   538
>>> f.close()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   539
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   540
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   541
    \subsection{File and \texttt{for}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   542
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   543
>>> f = open('/path/to/file_name')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   544
>>> for line in f:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   545
...     print line
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   546
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   547
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   548
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   549
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   550
    \item[4.2] The given file has lakhs of records in the form:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   551
    \texttt{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   552
    Some entries may be empty.  Read the data from this file and print the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   553
    name of the student with the maximum total marks.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   554
  \item[4.3] For the same data file compute the average marks in different
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   555
    subjects, the student with the maximum mark in each subject and also
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   556
    the standard deviation of the marks.  Do this efficiently.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   557
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   558
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   559
\section{Modules}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   560
\begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   561
>>> sqrt(2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   562
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   563
  File "<stdin>", line 1, in <module>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   564
NameError: name 'sqrt' is not defined
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   565
>>> import math        
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   566
>>> math.sqrt(2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   567
1.4142135623730951
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   568
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   569
>>> from math import sqrt
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   570
>>> from math import *
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   571
>>> from os.path import exists
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   572
\end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   573
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   574
  \subsection{Modules: example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   575
  \begin{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   576
# --- arith.py ---
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   577
def gcd(a, b):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   578
    if a%b == 0: return b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   579
    return gcd(b, a%b)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   580
def lcm(a, b):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   581
    return a*b/gcd(a, b)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   582
# ------------------
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   583
>>> import arith
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   584
>>> arith.gcd(26, 65)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   585
13
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   586
>>> arith.lcm(26, 65)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   587
130
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   588
  \end{verbatim}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   589
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   590
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   591
    \item[5.1] Put all the functions you have written so far as part of the problems
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   592
  into one module called \texttt{iitb.py} and use this module from IPython.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   593
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   594
\end{document}