Midterm Exam (2)

Introduction to Computer Science
NCNU CSIE

Date:  December 4th, 2015
Time: 08:10-11:00
Open book and computer; turn off mobile phones

  1. (20%) Consider the exercise of Timetable.  After you have designed a class Time, design a function diff(t1, t2) to calculate the difference between two objects of class Time.  You may test your class Time and function diff() with the following main program.
    def main():
        departure = Time("09:18")
        arrival = Time("10:01")
        show("Your train", departure, arrival)
        show("The previous train", departure.delta(-24),
            arrival.delta(-24) )
        show("The next train", departure.delta(60), arrival.delta(60))

    def show(t, d, a):
        print(t, "departs at", d,
            "and arrives at", a, "; travel time is", diff(d, a) )

    The results may look as follows:
    
    Your train departs at 09:18 and arrives at 10:01 ; travel time is 43
    The previous train departs at 08:54 and arrives at 09:37 ; travel time is 43
    The next train departs at 10:18 and arrives at 11:01 ; travel time is 43
    
    
  2. (20%) Design a Python program which will automatically detect the size of the window, and move a star ('*') around the border, as shown in the figure below.  Note that the star only needs to move a cycle, although the GIF file shows it moving repeatedly.  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.
    Move Around
  3. (20%) In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.  For example, the proper positive divisors of 6 are 1, 2, and 3, and they sum up to 6. Alternatively, you may say a perfect number is a number that is half the sum of all of its positive divisors (including itself).  For example, for the number 28, its factors (1 + 2 + 4 + 7 + 14 + 28) / 2 = 28.
    Suppose we already have the following main program to find perfect numbers.  Design a boolean function factor_sum() to calculate the sum of all positive divisors of a given number.  You may wish to test your program with N less than or equal to 4, because the fifth perfect number is a large number with 8 digits.
    Note: You only need to submit the function factor_sum() which you defined.

    print('This program will list "perfect numbers".')
    N = eval(input("How many perfect numbers do you want to print? "))
    i = 6       # The first perfect number
    for j in range(N):
        while factor_sum(i) != 2*i:
            i = i + 1
        print(i)
        i = i + 1

  4. (20%) The following program crashes during its execution.  Please modify a line so that it will run successfully and generate results as shown in the figure below.
    When you submit your answer, please highlight the modified line with color Magenta.
    import curses
    stdscr = curses.initscr()
    curses.start_color()
    s = "Pepsi Cola"

    for i in range(7):
        curses.init_pair(i, i, i+1)

    for i in range(7):
        stdscr.addstr( i, 10, s, curses.color_pair(i) )
    stdscr.refresh()

    curses.endwin()

    Pepsi Cola
  5. (20%) On CSIE webpage we can find a 104-1資工系導生人數明細.  For our convenience, let us convert it to a text file. Now please develop a Python program which analyzes the contents of this file, and summarize how many mentees are assigned to each teacher, correspondingly.  The results may look as follows.

    張克寧 20
    陳履恆  7
    黃育銘 15
    吳坤熹 21
    劉震昌 15
    陳依蓉 26
    林宣華  9
    陳恆佑 14
    黃光璿 14
    洪政欣 21
    鄭文凱 21
    周耀新  5
    石勝文 15
    楊峻權  6
    張景新 18

  6. (20%) 「可口可樂」飲料公司日前推出一個集瓶蓋活動。每個瓶蓋會隨機印上「可」「口」「樂」中的一個字。換句話說,每個字出現的機率都是三分之一。試撰寫 一個 Python 程式,模擬瓶蓋收集的過程,並計算平均要收集幾個瓶蓋,才能集滿「可」「口」「可」「樂」四個字。本題除了要上傳程式外,也要在 Moodle 的 Online Text 中寫上你根據模擬出來的數據所推測的答案。答案與「機率」課所教的不應差太多(由 Victor 認定)。
    For your convenience, the top-level design has been done and the main program is derived:
    def main():
        printIntro()
        n = getInputs()
        total_length = simNGames(n)
        printSummary(total_length, n)
    The program may run as follows:
    How many iterations do you want to simulate? 7
      8 樂樂口口樂口可可
     5 可口可口樂
     6 可口樂口樂可
     5 樂可可可口
    11 口口口口樂樂口口樂可可
     9 樂口口口可樂口樂可
    10 可可可樂樂可可樂樂口
    The average length for you to collect '可', '口', '可', '樂' is 54 / 7 =  7.71