Python_Problem_Set_1.tex
author Puneeth Chaganti <puneeth@fossee.in>
Fri, 18 Sep 2009 15:49:06 +0530
changeset 62 780effcd7c5a
parent 0 6d71487a99d3
child 90 1983962aabcb
permissions -rw-r--r--
fixed table in session4.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
\documentclass[11pt]{article}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
\title{Basic Python Problem Set 1}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
\author{Asokan Pichai}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
\date{}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
\begin{document}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
\maketitle
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
\section{Pyramids}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
\begin{enumerate}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
    \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
    10
        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
    11
        \begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
                     *
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
        \end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
    \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
    18
        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
    19
        \begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
                     *
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
        \end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
    \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
    26
        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
    27
        \begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
        1
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
        2 2
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
        3 3 3
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
        4 4 4 4
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
        \end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
    \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
    34
        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
    35
        \begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
          1
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
          2   4
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
          3   6   9
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
          4   8  12  16
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
          5  10  15  20  25
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
        \end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
    \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
    43
        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
    44
        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
    45
        \begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
          1
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
          2   3
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
          4   5   6
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
          7   8   9  10
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
         11  12  
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
        \end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
\end{enumerate}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
\section{Some mathematics}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
\begin{enumerate}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
    \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
    56
    \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
    57
        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
    58
    \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
    59
        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
    60
        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
    61
        $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
    62
        $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
    63
        functions and copy them to your area.)
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
\end{enumerate}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
\section{Code}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
\begin{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
#-------------------------------- #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
# Accepts an integer and returns  #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
# True if all digits are even     #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
# False otherwise                 #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
#-------------------------------- #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
def areDigitsEven( n ):
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
    while n > 0:
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
        if (n % 10) %2 != 0:
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
            return False
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
        n = n / 10
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
    return True
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
#-------------------------------- #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
# Accepts an integer and returns  #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
# True if it is a perfect square  #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
# False otherwise                 #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
#-------------------------------- #
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
def isSquare( n ):
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
    i = 1
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
    while i * i < n:
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
        i += 1
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
    return i * i == n
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
    
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
\end{verbatim}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
\end{document}
6d71487a99d3 Includes STTP Design, Python-Problem Set.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91