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