科目名稱: 計算機概論 期末考 開課系所:資工系 考試日期 2018.1.18
系所別:
年級:
學號:
姓名:
考試時間 08:20-10:00
Open book; turn off computer & mobile phone
1
2


3


4

5

6
7
8

9

10






  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Shuffle
    def main():
        aList = ["Alpha", "Bravo", "Charlie", "Delta", "Echo",
                 "Foxtrot", "Golf", "Hotel", "India", "Juliet",
                 "Kilo", "Lima", "Mike", "November", "Oscar",
                 "Papa", "Quebec", "Romeo", "Sierra", "Tango",
                 "Uniform", "Victor", "Whiskey", "Xray", "Yankee",
                 "Zulu"]
        bList = shuffle(aList)
        print(bList[:8])

    def shuffle(a):
        N = len(a)
        result = [" "] * N
        for i in range(N):
            k = i * 7 % N
            result[k] = a[i]
        return result

    main()


  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # String concatenation

    def f(x):
        temp=''
        for c in x:
            temp = c + temp + c
        return temp

    def main():
        words = ['MALAYSIA', 'TRULY', 'ASIA']
        for x in words:
            print( f(x) )

    main()


  3. (10%) What will be the output of the following program?

    # Return Value of a Function
    def star(n):
        print( '*' * n )
        return n

    print( star(10) )
    print( star(5) , star(5) )


  4. (10%) What will be the output of the following program?

    # Definite Loop
    for i in range(1,6):
        for j in range(1, i+1):
            print(j, end='')
        print()


  5. (10%) What will be the output of the following program?

    # import a module
    names = ["alice", "bob", "charlie"]
    for x in names:
        fname = x + ".py"
        outfile = open(fname, "w")
        print('def walk():\n    print("'+x+' walks")', file=outfile)
        print('def smile():\n   print("'+x+' smiles")', file=outfile)
        print('def sing():\n    print("'+x+' sings")', file=outfile)
        outfile.close()

    from alice import smile
    from bob import walk
    from charlie import sing
    smile()
    walk()
    sing()


  6. (10%) What will be the output of the following program?

    # while
    def f(s, c):
        positions = []
        n = s.find(c)
        while n >=0:
            positions.append(n)
            n = s.find(c, n+1)
        return positions

    def main():
        title = "A SONG OF ICE AND FIRE"
        for c in f(title, ' '):
            print(c, end=' ')
        print()

    main()


  7. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Transposition Cipher
    def main():
        key, msg = getInputs()
        plaintext = encrypt(msg, key)
        print(plaintext)

    def getInputs():
        message = "C:eETSShmaHeroOtmfTFuhureteIeCsrCHeC"
        key = 3
        return key, message

    def encrypt(msg, N):
        from math import ceil
        s = ''
        for i in range(0, len(msg), N*N):
            s = s + transpose(msg[i:i+N*N], N)
        return s

    def transpose(msg, N):
        t = []
        for i in range(N):
        # t = [ ['A'] * N ] * N
            t.append( [chr(65+i)] * N )
        p = 0
        for i in range(N):
            for j in range(N):
                if p < len(msg):
                    t[i][j] = msg[p]
                p = p + 1
        s = ''
        for j in range(N):
            for i in range(N):
                s = s + t[i][j]
        return s

    main()
  8. (10%) Determine whether the following code has syntax erros or
        not.  If it is correct, predict its output.  If it is
        incorrect, point out the
        mistake(s).

    # Define a Class
    def gcd(a, b):
    while b != 0:
    a, b = b, a % b
    return a

    class Rational:
    def __init__(self, n=0, d=1):
    self.numerator = n
    self.denominator = d
    def reduce(self):
    d = gcd(self.numerator, self.denominator)
    self.numerator = self.numerator // d
    self.denominator = self.denominator // d
    def __str__(self):
    self.reduce()
    if self.denominator == 1:
    result = str(self.numerator)
    else:
    result = str(self.numerator)+'/'+str(self.denominator)
    return result

    def main():
    a = Rational(36)
    b = Rational(24, 1)
    c = Rational(36, 24)
    qList = [a, b, c]
    for q in qList:
    print(q)

    main()

  9. (10%) Determine whether the following code has syntax erros or
        not.  If it is correct, predict its output.  If it is
        incorrect, point out the
        mistake(s).
    # Method of an Object
    def gcd(a, b):
    while b != 0:
    a, b = b, a % b
    return a

    class Rational:
    def __init__(self, n=0, d=1):
    self.numerator = n
    self.denominator = d
    def reduce(self):
    d = gcd(self.numerator, self.denominator)
    self.numerator = self.numerator // d
    self.denominator = self.denominator // d
    def __str__(self):
    self.reduce()
    if self.denominator == 1:
    result = str(self.numerator)
    else:
    result = str(self.numerator)+'/'+str(self.denominator)
    return result
    def add(self, q):
    n = self.numerator * q.denominator + self.denominator * q.numerator
    d = self.denominator * q.denominator
    return Rational(n, d)

    def main():
    c = Rational(36, 24)
    d = Rational(24, 36)
    e = c.add(d)
    qList = [c, d, e]
    for q in qList:
    print(q)

    main()


  10. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Sorting a List of Objects
    def gcd(a, b):
    while b != 0:
    a, b = b, a % b
    return a

    class Rational:
    def __init__(self, n=0, d=1):
    self.numerator = n
    self.denominator = d
    def reduce(self):
    d = gcd(self.numerator, self.denominator)
    self.numerator = self.numerator // d
    self.denominator = self.denominator // d
    def __str__(self):
    self.reduce()
    if self.denominator == 1:
    result = str(self.numerator)
    else:
    result = str(self.numerator)+'/'+str(self.denominator)
    return result
    def add(self, q):
    n = self.numerator * q.denominator + self.denominator * q.numerator
    d = self.denominator * q.denominator
    return Rational(n, d)

    def reciprocal(q):
    return q.denominator / q.numerator

    def main():
    c = Rational(36, 24)
    d = Rational(24, 36)
    e = c.add(d)
    qList = [c, d, e]
    qList.sort(key=reciprocal)
    for q in qList:
    print(q)

    main()