summaryrefslogtreecommitdiff
path: root/basic_python/practice_ds.tex
blob: 50ebf7e2ca13c17c00532d7e7abc8f48f3db6d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tutorial slides on Python.
%
% Author: FOSSEE
% Copyright (c) 2017, FOSSEE, IIT Bombay
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[14pt,compress]{beamer}

\input{macros.tex}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
\title[Basic Python]{Practice exercises: data structures}

\author[FOSSEE Team] {The FOSSEE Group}

\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {Mumbai, India}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT STARTS
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}[fragile,plain]
  \frametitle{Note: Python 2.x and 3.x}

 If you are using Python 2.x
  \begin{itemize}
  \item Use \typ{raw\_input} instead of \typ{input}
  \item Use the following for \typ{print}
  \end{itemize}
 \begin{lstlisting}
from __future__ import print_function
\end{lstlisting}
\end{frame}

\begin{frame}[plain]
  \frametitle{Exercise: simple list creation}
  \begin{enumerate}
  \item Ask the user to enter an integer, \typ{n}
  \item Create a list of integers from 0 to \typ{n-1}
  \item Print this list
  \end{enumerate}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  n = int(input())
  x = list(range(n))
  print(x)
\end{lstlisting}
\end{frame}

\begin{frame}[plain]
  \frametitle{Exercise: more list creation}
  \begin{enumerate}
  \item Ask the user to enter an integer, \typ{n}
  \item Store the square of all the odd numbers less than \typ{n} in a list
  \item Print this list
  \end{enumerate}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  n = int(input())
  result = []
  for i in range(1, n, 2):
      result.append(i*i)
  print(result)
\end{lstlisting}
\end{frame}

\begin{frame}[plain]
  \frametitle{Exercise: tuple creation}
  \begin{enumerate}
  \item Ask the user to enter an integer, \typ{n}
  \item Store the square of all the odd numbers less than \typ{n}
  \item Print a tuple of this list
  \end{enumerate}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  n = int(input())
  result = []
  for i in range(1, n, 2):
      result.append(i*i)
  print(tuple(result))
\end{lstlisting}
\end{frame}


\begin{frame}[plain,fragile]
  \frametitle{Hint: string to list/tuple}
  Here is an easy way to convert a string to a list of characters.

  Try this:
  \begin{lstlisting}
    In []: x = 'hello'
    In []: print(list(x))
    In []: print(tuple(x))
  \end{lstlisting}
\end{frame}

\begin{frame}[plain]
  \frametitle{Exercise: list of Fibonacci}
  \begin{enumerate}
  \item Ask the user to enter an integer, \typ{n} ($\geq 1$)
  \item Store the first \typ{n} numbers of the Fibonnaci series in a list
  \item Print this list
  \end{enumerate}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  n = int(input())
  a, b = 0, 1
  result = [0]
  for i in range(n-1):
      result.append(b)
      a, b = b, a+b
  print(result)
\end{lstlisting}
\end{frame}

\begin{frame}[plain,fragile]
  \frametitle{Exercise: square a list of integers}
  \begin{enumerate}
  \item Ask the user to enter a list of integers separated by spaces
  \item Convert this into a list of integers but square each element
  \item Print this list
  \end{enumerate}
  For example, if the user enters \typ{1 2 3 4}, print:
  \begin{lstlisting}
    [1, 4, 9, 16]
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  text = input()
  result = []
  for item in text.split():
      x = int(item)
      result.append(x*x)
  print(result)
\end{lstlisting}
\end{frame}

\begin{frame}[plain,fragile]
  \frametitle{Exercise: list of tuples}
  \begin{enumerate}
  \item Ask the user to enter a list of integers separated by spaces
  \item Convert this into a list of integers but square each element
  \item Store the integer and its square in a tuple, put this into a list
  \item Print this list
  \end{enumerate}
  For example, if the user enters \typ{1 2 3 4}, print:
  \begin{lstlisting}
    [(1, 1), (2, 4), (3, 9), (4, 16)]
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  text = input()
  result = []
  for item in text.split():
      x = int(item)
      result.append((x, x*x))
  print(result)
\end{lstlisting}
\end{frame}

\begin{frame}[plain,fragile]
  \frametitle{Hint: iterating over a list of tuples}
  Consider the following code:
  \begin{small}
  \begin{lstlisting}
In []: data = [(1, 1), (2, 4), (3, 9), (4, 16)]
\end{lstlisting}
\end{small}
We can iterate over this as follows:
\begin{small}
\begin{lstlisting}
In []: for x, y in data:
.....:     print(x, y)
\end{lstlisting}
\end{small}
\end{frame}


\begin{frame}[plain,fragile]
  \frametitle{Exercise: list methods}
  \begin{enumerate}
  \item Ask the user to enter a string
  \item Convert this into a list of characters
  \item Sort this list in ascending order
  \item Now eliminate any repeated values in this list
  \item Print this list
  \end{enumerate}
  For example, if the user enters \typ{hello}, print:
  \begin{lstlisting}
    ['e', 'h', 'l', 'o']
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  text = input()
  chars = list(text)
  chars.sort()
  result = []
  for c in chars:
      if c not in result:
          result.append(c)
  print(result)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Another solution}
\begin{lstlisting}
  text = input()
  chars = set(text)
  chars = list(chars)
  chars.sort()
  print(chars)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Another solution}
\begin{lstlisting}
  chars = set(input())
  print(sorted(chars))
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain,fragile]
  \frametitle{Exercise: simple dictionaries}
  \begin{enumerate}
  \item Ask the user for a list of integers separated by spaces
  \item For each integer, store the string version as the key and the square of
    the integer value as the value in a dictionary.
  \item Print the resulting dictionary
  \end{enumerate}
For example if the user enters "1 3 5", print:
\begin{lstlisting}
{'1': 1, '3': 9, '5': 25}
\end{lstlisting}
Hint: simply print the resulting dictionary
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
text = input()
d = {}
for item in text.split():
    x = int(item)
    d[item] = x*x
print(d)
\end{lstlisting}
\end{frame}


\begin{frame}[fragile,plain,fragile]
  \frametitle{Exercise: mapping using dicts}
  \begin{enumerate}
  \item Create a mapping from 3 character month name to month number
  \item Hint: for example \typ{\{'jan': 1, 'dec': 12\}}
  \item Ask the user for a 3 character month code lower/upper mixed
  \item Print the month number corresponding to the month the user entered.
  \end{enumerate}
For example if the user enters "Jul", print:
\begin{lstlisting}
7
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
months = ('jan feb mar apr may jun jul ' +
         'aug sep oct nov dec').split()
month2mm = {}
for i in range(len(months)):
    month2mm[months[i]] = i + 1

text = input()
mon = text[:3].lower()
print(month2mm[mon])
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Aside: using \typ{enumerate} example}
\begin{lstlisting}
for i, char in enumerate('hello'):
    print(i, char)
\end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Aside: using \typ{enumerate}}
\begin{lstlisting}
months = ('jan feb mar apr may jun jul ' +
         'aug sep oct nov dec').split()
month2mm = {}
for i, month in enumerate(months):
    month2mm[month] = i + 1

# Is easier/nicer than this:

for i in range(len(months)):
    month2mm[months[i]] = i + 1
\end{lstlisting}

\end{frame}



\begin{frame}[plain,fragile]
  \frametitle{Exercise: dictionaries}
  \begin{enumerate}
  \item Ask the user to enter a string
  \item Convert this to lower case
  \item Count the number of occurrences of each character in the string
  \item Hint: use a dict
  \item Print the result in sorted order of the characters
  \end{enumerate}
  For example, if the user enters \typ{helloo}, print:
  \begin{lstlisting}
    e 1
    h 1
    l 2
    o 2
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
  text = input().lower()
  result = {}
  for char in text:
      if char in result:
          result[char] += 1
      else:
          result[char] = 1

  for char in sorted(result):
      print(char, result[char])
\end{lstlisting}
\end{frame}


\begin{frame}[plain]
  {Problem: datestring to date tuple}

  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 program that takes such a string as input and prints a tuple (yyyy,
  mm, dd) where all three elements are ints.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Solution}
 \small
  \begin{lstlisting}
months = ('jan feb mar apr may jun jul ' +
         'aug sep oct nov dec').split()
month2mm = {}
for i, month in enumerate(months):
    month2mm[month] = i + 1

date = input()
date = date.replace(',', ' ')
day, month, year = date.split()

dd, yyyy = int(day), int(year)
mon = month[:3].lower()
mm = month2mm[mon]
print((yyyy, mm, dd))
\end{lstlisting}
\end{frame}

\begin{frame}
  \centering
  \Huge

  That's all folks!
\end{frame}
\end{document}