- (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).
# Definite Loop
for i in range(10, 2, -3):
print(i)
- (10%) What will be the output of the following program?
# Boolean Formula
def b(p):
if p:
return "T"
else:
return "F"
def main():
str="{0:^8} {1:^8} {2:^15}"
print( str.format("P", "Q", "P or Q") )
print( str.format("===", "===", "==========") )
for P in [True, False]:
for Q in [True, False]:
print( str.format(b(P), b(Q), b(P or Q) ) )
main()
- (10%) What will be the output of the following program?
# Type Conversion
import math
a = 2.718
b = 0.5
c = a + b
d = int(c)
e = math.ceil(c)
print(a, b, c, d, e)
- (10%) What will be the output of the following program?
#
Transposition Cipher
def main():
key, ciphertext, choice = getInputs()
plaintext = decrypt(ciphertext, key)
print(plaintext)
def getInputs():
choice = 1
message = "長非時營恨我忘夜此有卻闌身何營風靜小逝餘縠舟江生紋從海蘇平此寄軾"
key = 4
return key, message, choice
def decrypt(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):
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()
- (10%) What will be the output of the following program?
# String Slicing
str = "PROGRAMMING"
print( str[-7:7] )
- (10%) Consider
the following decision structure:
a, b, c,
= eval(input('Enter three numbers: '))
if a >
b:
if b > c:
print("Spam Please!")
else:
print("It's a late parrot!")
elif b
>= c:
print("Cheese Shoppe")
if a >= c:
print("Cheddar")
elif a < b:
print("Gouda")
elif c == b:
print("Swiss")
else:
print("Trees")
if c == b:
print("Chestnut")
else:
print("Larch")
print("Done")
What will be the output that would result from each of the input 5,3,3?
- (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).
# Recursive Function Invoking
def f(n):
if n >= 1:
f(n // 2)
print( n % 2, end='')
def main():
f(97)
print()
main()
-
(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).
# Factorization
def func(n):
i=2
a=n
while i<=n:
if i==n and n%i==0:
print(i)
elif n%i==0:
print(i, "*", sep="", end="")
n=n/i
i=1
i+=1
if a==n:
return
for n in [8, 81, 162]:
func(n)
-
(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).
# Defining Classes
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def move(self, dx, dy):
x = dx
y = dy
def main():
a = Point(1,8)
a.move(7, 5)
print(a.x, a.y)
main()
- (10%)
What
will be the output of the following program?
# __str__ Method of a Class (P.336)
class Time:
def __init__(self, string):
"""Creates a time, eg:
departure = Time('08:54')"""
hh, mm = string.split(':')
self.hour = int(hh)
self.minute = int(mm)
def modify(self, d):
"Shift the time by d minutes"
self.minute = self.minute + d
while self.minute < 0:
self.hour = self.hour - 1
self.minute = self.minute + 60
while self.minute >= 60:
self.hour = self.hour + 1
self.minute = self.minute - 60
self.hour = self.hour % 24
def delta(self, d):
"Return a Time object which was shifted d minutes"
t = Time("{0:02}:{1:02}".format(self.hour, self.minute))
t.modify(d)
return t
def __str__(self):
"Returns a string representing the time with the format hh:mm"
minute = self.minute
if self.hour < 12:
suffix = "AM"
hour = self.hour
else:
suffix = "PM"
hour = self.hour - 12
if hour == 0:
hour = 12
template = "{0:02}:{1:02}{2}"
return template.format(hour, minute, suffix)
def main():
departure = Time("11:18")
arrival = Time("12: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 )
main()
- (10%)
What
will be shown on the screen by the following program?
# curses library
import curses
stdscr = curses.initscr()
for i in range(1, 11):
stdscr.move(i, 10-i)
stdscr.addstr( "*" * i )
stdscr.refresh()
curses.endwin()