author | amit |
Tue, 07 Sep 2010 11:03:27 +0530 | |
changeset 123 | 7a16c80c584d |
parent 90 | 1983962aabcb |
permissions | -rw-r--r-- |
90 | 1 |
%% a comment |
0
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
2 |
\documentclass[11pt]{article} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
3 |
\title{Basic Python Problem Set 1} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
4 |
\author{Asokan Pichai} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
\date{} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
6 |
\begin{document} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
7 |
\maketitle |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
8 |
\section{Pyramids} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
9 |
\begin{enumerate} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
10 |
\item Write a program to display the following pyramid. The number of lines in the pyramid should not be hard-coded. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
11 |
It should be obtained from the user. The pyramid should appear as close to the centre of the screen as possible. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
12 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
* |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
14 |
*** |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
15 |
***** |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
16 |
******* |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
17 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
18 |
\item Write a program to display the following pyramid. The number of lines in the pyramid should not be hard-coded. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
19 |
It should be obtained from the user. The pyramid should appear as close to the centre of the screen as possible. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
20 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
21 |
* |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
22 |
* * |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
23 |
* * * |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
* * * * |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
25 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
26 |
\item Write a program to display the following pyramid. The number of lines has to be a parameter obtained from the |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
27 |
user. The pyramid must appear aligned to the left edge of the screen. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
28 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
29 |
1 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
30 |
2 2 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
31 |
3 3 3 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
32 |
4 4 4 4 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
33 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
34 |
\item Write a program to display the following pyramid. The number of lines has to be a parameter obtained from the |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
35 |
user. The pyramid must appear aligned to the left edge of the screen. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
36 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
37 |
1 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
38 |
2 4 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
39 |
3 6 9 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
40 |
4 8 12 16 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
41 |
5 10 15 20 25 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
42 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
43 |
\item Write a program to display the following output. The last number where the program will stop printing has to |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
44 |
be a parameter obtained from the user. The pyramid must appear aligned to the left edge of the screen. Note that |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
45 |
depending on the last number, the base of the pyramid may be smaller than the line above it. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
46 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
47 |
1 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
48 |
2 3 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
49 |
4 5 6 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
50 |
7 8 9 10 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
51 |
11 12 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
52 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
53 |
\end{enumerate} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
54 |
\section{Some mathematics} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
55 |
\begin{enumerate} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
56 |
\item Write a program to calculate the gcd of two numbers. |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
57 |
\item Write a program to generate all the Armstrong numbers: three numbers that are equal to the sum of the cubes of |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
58 |
their digits: that is $abc = a^3 + b^3 + c^3$ |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
59 |
\item Write a program that lists all four digit numbers that are perfect squares \emph{and} have all their digits |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
60 |
even. An example is $6400$ which is $80^2$. Note that in order to be listed a four digit number must satisfy |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
61 |
both criteria. For example, $5625$ is a square, it does not have all digits even and |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
62 |
$4842$ is not a square and hence they should not appear in the output. You can assume that two functions |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
63 |
$areDigitsEven()$ and $isSquare()$ are available. (Please ask the instructor for the exact location of these |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
64 |
functions and copy them to your area.) |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
65 |
\end{enumerate} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
66 |
\section{Code} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
67 |
\begin{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
68 |
#-------------------------------- # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
69 |
# Accepts an integer and returns # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
70 |
# True if all digits are even # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
71 |
# False otherwise # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
72 |
#-------------------------------- # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
73 |
def areDigitsEven( n ): |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
74 |
while n > 0: |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
75 |
if (n % 10) %2 != 0: |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
return False |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
77 |
n = n / 10 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
78 |
return True |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
79 |
#-------------------------------- # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
80 |
# Accepts an integer and returns # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
81 |
# True if it is a perfect square # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
82 |
# False otherwise # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
83 |
#-------------------------------- # |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
84 |
def isSquare( n ): |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
85 |
i = 1 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
86 |
while i * i < n: |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
87 |
i += 1 |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
88 |
return i * i == n |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
89 |
|
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
90 |
\end{verbatim} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
91 |
\end{document} |
6d71487a99d3
Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
92 |