Final Exam 

Introduction to Computer Science
NCNU CSIE

Date:  January 8th, 2016
Time: 08:10-13:00
Open book and computer; turn off mobile phones

  1. (20%) Consider the following code.  We want the output for N=3 is

    *
    ***
    *****
    ***
    *

    while the output for N=5 is

    *
    ***
    *****
    *******
    *********
    *******
    *****
    ***
    *

    What should be expression (1)?

    # for-loop
    N = eval( input("N = ? ") )
    for i in range(2*N-1):
        j =     (1)   
        k = 2*N - 1 - 2*j
        print(' '*j + '*'*k)


  2. (20%) Design a Python program which will automatically detect the size of the window, and print stars ('*') like a snake, from the top to the bottom, as shown in the figure below.   To keep it simple, you may assume that we shall not resize the window while the program is running. Note: The cursor should be disabled while the star is moving.
    Snake
  3. (20%) Consider the following code.  We want the output for N=3 is
    ***
    **
    *

    while the output for N=5 is
    *****
    ****
    ***
    **
    *

    What should be expression (3)?

    # String Formatting
    N = eval( input("N = ? ") )
    for i in range(N):
        fmt =     (3)   
        print(fmt.format("*"*(N-i)))



  4. (20%) An anagram is formed by rearranging the letters of a word (e.g., "hamlet" is an anagram of "amleth").  A palindrome is a word which reads the same backward or forward (e.g., "noon", "level", "racecar").  Write a Python program which ask the user to input a word, and find out that, among all anagrams of the word, how many of them are palindromes.  For example, if the input word is "noon", the output number is "2".  If the input word is "cccaaddbb", the output number is "24".

  5. (20%) Imagine that during a war, we intercepted an encrypted message "ESQ LZW XGJUW TW OALZ QGM."  According to the intelligence collected by our spy, the rebellious troops is using the Caesar cipher, but we are not sure about which key value was used to generate this encrypted message.   Please write a Python program, trying to decrypt with all 26 possible keys, and determine the key value and the original plaintext message.  Be sure to submit both your Python program and the decrypted message.

  6. (20%) Design a Polynomial class to handle polynomials. 
    1. For simplicity, in this program we assume the coefficient of each term will be an integer. 
    2. Its constructor will take a list as input, For example, Polynomial([1, 0, 3]) represents 1 + 3 X2.
    3. Design its method __str__() so that Polynomial([1, 0, 3]) will be printed out as "1 + 3 x**2", while Polynomial([1, -3, 2]) will be printed out as "1 -3 x + 2 x**2".  A term with coefficient should be omitted.
    Test your class definition with the following main program:

    def main():
    p1 = Polynomial([1, 0, 3])
    p2 = Polynomial([1, -3, 2])
    print(p1)
    print(p2)

  7. (20%) Inherit the Polynomial class defined in the previous question.  Add a method deg() which returns the degree of this polynomial.  For example, the degree of Polynomial([1,0,0,-1]) is 3.
    Test your subclass with the following main program

    def main():
    p1 = Polynomial([-1, 0, 3])
    p2 = Polynomial([1, -3, 0, -1])
    fmt="deg({0}) = {1}"
    print( fmt.format(p1, p1.deg()) )
    print( fmt.format(p2, p2.deg()) )

    The result should be

    deg(-1+3x**2) = 2
    deg(1-3x-x**3) = 3

  8. (20%) Design a polyAdd() function (not a method of the class Polynomial), so that polyAdd(p1, p2) will return a polynomial which is the result by adding two polynomials p1 and p2.  Also, design a polySub(p1, p2) function to subtract a polynomial p2 from p1.  Test your class with the following main program:

    def main():
    p1 = Polynomial([1,2])
    p2 = Polynomial([2, 0, 1])
    print(p1, p2, sep='\n')
    p3 = polyAdd(p1, p2)
    p4 = polySub(p1, p2)
    print(p3, p4, sep='\n')
    p5 = Polynomial([2, 1, 1])
    p6 = Polynomial([1, 1, 1])
    p7 = polySub(p5, p6)
    fmt="deg({0}) = {1}"
    print( fmt.format(p7, p7.deg()) )