sttp/Python_Problem_Set_1.tex
author amit@thunder
Tue, 02 Mar 2010 18:43:02 +0530
changeset 0 27e1f5bd2774
permissions -rw-r--r--
Using test review as the test directory for testing sees.hook for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     1
\documentclass[11pt]{article}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     2
\title{Basic Python Problem Set 1}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     3
\author{Asokan Pichai}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     4
\date{}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     5
\begin{document}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     6
\maketitle
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     7
\section{Pyramids}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     8
\begin{enumerate}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    11
        \begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    12
                     *
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    13
                    ***
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    14
                   *****
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    15
                  *******
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    16
        \end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    19
        \begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    20
                     *
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    21
                    * *
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    22
                   * * *
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    23
                  * * * *
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    24
        \end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    26
        user. The pyramid must appear aligned to the left edge of the screen.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    27
        \begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    28
        1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    29
        2 2
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    30
        3 3 3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    31
        4 4 4 4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    32
        \end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    34
        user. The pyramid must appear aligned to the left edge of the screen.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    35
        \begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    36
          1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    37
          2   4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    38
          3   6   9
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    39
          4   8  12  16
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    40
          5  10  15  20  25
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    41
        \end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    42
    \item Write a program to display the following output. The last number where the program will stop printing has to
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    44
        depending on the last number, the base of the pyramid may be smaller than the line above it.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    45
        \begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    46
          1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    47
          2   3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    48
          4   5   6
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    49
          7   8   9  10
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    50
         11  12  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    51
        \end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    52
\end{enumerate}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    53
\section{Some mathematics}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    54
\begin{enumerate}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    55
    \item Write a program to calculate the gcd of two numbers.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    57
        their digits: that is $abc = a^3 + b^3 + c^3$
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    58
    \item Write a program that lists all four digit numbers that are perfect squares \emph{and} have all their digits
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
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
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    60
        both criteria. For example, $5625$ is a square, it does not have all digits even and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    61
        $4842$ is not a square and hence they should not appear in the output. You can assume that two functions
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    62
        $areDigitsEven()$ and $isSquare()$ are available. (Please ask the instructor for the exact location of these
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    63
        functions and copy them to your area.)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    64
\end{enumerate}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    65
\section{Code}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    66
\begin{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    67
#-------------------------------- #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    68
# Accepts an integer and returns  #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    69
# True if all digits are even     #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    70
# False otherwise                 #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    71
#-------------------------------- #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    72
def areDigitsEven( n ):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    73
    while n > 0:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    74
        if (n % 10) %2 != 0:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    75
            return False
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    76
        n = n / 10
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    77
    return True
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    78
#-------------------------------- #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    79
# Accepts an integer and returns  #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    80
# True if it is a perfect square  #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    81
# False otherwise                 #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    82
#-------------------------------- #
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    83
def isSquare( n ):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    84
    i = 1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    85
    while i * i < n:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    86
        i += 1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    87
    return i * i == n
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    88
    
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    89
\end{verbatim}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    90
\end{document}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    91