108 |
108 |
109 \begin{frame} |
109 \begin{frame} |
110 \titlepage |
110 \titlepage |
111 \end{frame} |
111 \end{frame} |
112 |
112 |
113 \section{Functions and basic data structures} |
113 \section{Control Flow} |
114 |
114 |
115 \subsection{Exercises on Control flow} |
115 \subsection{Exercises} |
116 \begin{frame} |
116 \begin{frame} |
117 \frametitle{Problem set 1} |
117 \frametitle{Problem set 1} |
118 \begin{itemize} |
118 \begin{itemize} |
119 \item All the problems can be\\ |
119 \item All the problems can be\\ |
120 solved using \kwrd{if} and \kwrd{while} |
120 solved using \kwrd{if} and \kwrd{while} |
121 \end{itemize} |
121 \end{itemize} |
122 \end{frame} |
122 \end{frame} |
123 |
123 |
124 \begin{frame}{Problem 1.1} |
124 \begin{frame}{Problem 1.1} |
125 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$\\ |
125 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$\\ |
|
126 \begin{block} |
|
127 {Information...} |
126 These are called $Armstrong$ numbers. |
128 These are called $Armstrong$ numbers. |
|
129 \end{block} |
127 \end{frame} |
130 \end{frame} |
128 |
131 |
129 \begin{frame}{Problem 1.2 - Collatz sequence} |
132 \begin{frame}{Problem 1.2 - Collatz sequence} |
130 \begin{enumerate} |
133 \begin{enumerate} |
131 \item Start with an arbitrary (positive) integer. |
134 \item Start with an arbitrary (positive) integer. |
227 x *= x |
231 x *= x |
228 return z |
232 return z |
229 \end{lstlisting} |
233 \end{lstlisting} |
230 \end{frame} |
234 \end{frame} |
231 |
235 |
|
236 \subsection{Built-in functions} |
232 \begin{frame} |
237 \begin{frame} |
233 {Before writing a function} |
238 {Before writing a function} |
234 \begin{itemize} |
239 \begin{itemize} |
235 \item Builtin functions for various and sundry |
240 \item Variety of builtin functions are available |
236 \item \typ{abs, any, all, len, max, min} |
241 \item \typ{abs, any, all, len, max, min} |
237 \item \typ{pow, range, sum, type} |
242 \item \typ{pow, range, sum, type} |
238 \item Refer here: |
243 \item Refer here: |
239 \url{http://docs.python.org/library/functions.html} |
244 \url{http://docs.python.org/library/functions.html} |
240 \end{itemize} |
245 \end{itemize} |
242 \end{frame} |
247 \end{frame} |
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
244 % TIME: 10 m, running 30m |
249 % TIME: 10 m, running 30m |
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
246 |
251 |
247 \begin{frame}{Problem set 2} |
252 \subsection{Exercises} |
248 The focus is on writing functions and calling them. |
253 \begin{frame}{Problem set 2: Problem 2.1} |
249 \end{frame} |
|
250 |
|
251 \begin{frame}{Problem 2.1} |
|
252 Write a function to return the gcd of two numbers. |
254 Write a function to return the gcd of two numbers. |
253 \end{frame} |
255 \end{frame} |
254 |
256 |
255 \begin{frame}{Problem 2.2} |
257 \begin{frame}{Problem 2.2} |
256 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. \\ |
258 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\ |
257 Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100 |
259 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 is also not primitive. |
258 \end{frame} |
260 \end{frame} |
259 |
261 |
260 \begin{frame}{Problem 2.3} |
262 \begin{frame}{Problem 2.3} |
261 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). |
263 Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).} |
262 \end{frame} |
264 \end{frame} |
263 |
265 |
264 \begin{frame}{Problem 2.4} |
266 \begin{frame}{Problem 2.4} |
265 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.\\ |
267 The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\ |
266 Write a function that returns the aliquot number of a given number. |
268 Write a function that returns the aliquot number of a given number. |
267 \end{frame} |
269 \end{frame} |
268 |
270 |
269 \begin{frame}{Problem 2.5} |
271 \begin{frame}{Problem 2.5} |
270 A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\ |
272 A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\ |
271 Example: \texttt{220, 284}\\ |
273 Example: \texttt{220, 284}\\ |
272 Write a program that prints all five digit amicable pairs. |
274 Write a program that prints all four digit amicable pairs. |
273 \inctime{25} |
275 \inctime{25} |
274 \end{frame} |
276 \end{frame} |
275 |
277 |
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
277 % TIME: 25 m, running 55m |
279 % TIME: 25 m, running 55m |
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
279 |
281 |
280 \subsection{Lists} |
282 \section{Lists} |
281 |
283 |
|
284 \subsection{Manipulating} |
282 \begin{frame}[fragile] |
285 \begin{frame}[fragile] |
283 \frametitle{List creation and indexing} |
286 \frametitle{List creation and indexing} |
284 \begin{lstlisting} |
287 \begin{lstlisting} |
285 >>> a = [] # An empty list. |
288 >>> a = [] # An empty list. |
286 >>> a = [1, 2, 3, 4] # More useful. |
289 >>> a = [1, 2, 3, 4] # More useful. |
287 >>> len(a) |
290 >>> len(a) |
288 4 |
291 4 |
289 >>> a[0] + a[1] + a[2] + a[-1] |
292 >>> a[0] + a[1] + a[-1] |
290 10 |
293 7 |
291 \end{lstlisting} |
294 \end{lstlisting} |
292 \begin{itemize} |
295 \begin{itemize} |
293 \item Indices start with ? |
296 \item Indices start with ? |
294 \item Negative indices indicate ? |
297 \item Negative indices indicate ? |
295 \end{itemize} |
298 \end{itemize} |
364 |
367 |
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
366 % TIME: 10 m, running 65m |
369 % TIME: 10 m, running 65m |
367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
368 |
371 |
|
372 \subsection{Methods} |
369 \begin{frame}[fragile] |
373 \begin{frame}[fragile] |
370 \frametitle{List methods} |
374 \frametitle{List methods} |
371 \begin{lstlisting} |
375 \begin{lstlisting} |
372 >>> a = ['spam', 'eggs', 1, 12] |
376 >>> a = ['spam', 'eggs', 1, 12] |
373 >>> a.reverse() # in situ |
377 >>> a.reverse() # in situ |
401 |
405 |
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
403 % TIME: 5 m, running 70m |
407 % TIME: 5 m, running 70m |
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
405 |
409 |
|
410 \section{Tuples} |
406 \begin{frame}[fragile] |
411 \begin{frame}[fragile] |
407 \frametitle{Tuples: immutable} |
412 \frametitle{Tuples: immutable} |
408 \begin{lstlisting} |
413 \begin{lstlisting} |
409 >>> t = (0, 1, 2) |
414 >>> t = (0, 1, 2) |
410 >>> print t[0], t[1], t[2], t[-1] |
415 >>> print t[0], t[1], t[2], t[-1] |
411 0 1 2 2 |
416 0 1 2 2 |
412 >>> t[0] = 1 |
417 >>> t[0] = 1 |
413 Traceback (most recent call last): |
418 Traceback (most recent call last): |
414 File "<stdin>", line 1, in ? |
419 File "<stdin>", line 1, in ? |
415 TypeError: object does not support item assignment |
420 TypeError: object does not support |
|
421 item assignment |
416 \end{lstlisting} |
422 \end{lstlisting} |
417 \begin{itemize} |
423 \begin{itemize} |
418 \item Multiple return values are actually a tuple. |
424 \item Multiple return values are actually a tuple. |
419 \item Exchange is tuple (un)packing |
425 \item Exchange is tuple (un)packing |
420 \end{itemize} |
426 \end{itemize} |
423 |
429 |
424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
425 % TIME: 5 m, running 75m |
431 % TIME: 5 m, running 75m |
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
427 |
433 |
|
434 \section{for and range()} |
428 \begin{frame}[fragile] |
435 \begin{frame}[fragile] |
429 \frametitle{\typ{range()} function} |
436 \frametitle{\typ{range()} function} |
430 \begin{lstlisting} |
437 \begin{lstlisting} |
431 >>> range(7) |
438 >>> range(7) |
432 [0, 1, 2, 3, 4, 5, 6] |
439 [0, 1, 2, 3, 4, 5, 6] |