CSC111 Midterm Solution 2015
--D. Thiebaut (talk) 13:23, 25 March 2015 (EDT)
# midterm2015
# solution
def question1():
print( """
strings are immutable
lists are not immutable
integers are not immutable
floats are not immutable
python is not a compiled language
Literals: 1 True 6.3 "hello"
""")
def question2():
a = "Hello"
b = 3
c = 123
d = a, b, a, c
c, a, b, e = d
print( a, b, c, d, e )
# 3 Hello Hello ('Hello', 3, 'Hello', 123) 123
def question3():
print( """
def f1( a, b, c ):
print( “{0:1}-{1:1} {1:1}”.format( a, b, c ) )
print( “-“ * 60 )
def f2( name ):
print( name * 3 )
# f2 needs to return stomething
def r2d2( r100 ):
f2( “hello” )
a = f2( 3 )
# f2 does not return anything
b = f2( “3” )
# f2 does not return anything
f1( a, b, f2( “world!” ) )
# f2("world!") is None
def main():
r2d2( 100 )
f1( a, b, c )
# a, b, c not defined
main()
""" )
def question4():
x = 5 #input( "> " ) #5
y = 4 #input( "> " ) #4
x = int( x )
y = int( y )
for i in range( 1, x+1 ):
print( "-" * y, "0" * i )
def question5():
Nums = [1, -10, 2, 3, 4, -5, 100, 200, -150 ]
sump = 0
sumn = 0
for i in range( len( Nums ) ):
if Nums[i] >= 0:
sump = sump + Nums[i]
else:
sumn = sumn + Nums[i]
print( sump, sumn )
def question6():
def getPositiveList( nums ):
newList = []
for n in nums:
if n > 0:
newList.append( n )
return newList
Nums = [1, -10, 2, 3, 4, -5, 100, 200, -150 ]
Numsp = getPositiveList( Nums )
print( "Nums = ", Nums )
print( "Numsp = ", Numsp )
def question7():
Nums1 = [1, -10, 2, 30, 4, -51, 100, 2, 150 ]
Nums2 = [3, -10, 21, 3, 41, -5, 100, 2000, -150 ]
count = 0
for i in range( len( Nums1 ) ):
if Nums1[i] == Nums2[i]:
count = count + 1
print( "number of ints matching: ", count )
def question8():
print( "Refer to the Python docs page at" )
print( "https://docs.python.org/3.4/library/stdtypes.html#string-methods" )
def question9():
name1 = "Alex"
age1 = 12
coef1 = 6.188888
name2 = "Anastasia"
age2 = 22
coef2 = 8
print( "12345678901234567890123456789012345678901234567890" )
print( "name: {0:>10} age: {1:3} coefficient: {2:7.2f}/20".format( name1, age1, coef1 ) )
print( "name: {0:>10} age: {1:3} coefficient: {2:7.2f}/20".format( name2, age2, coef2 ) )
print()
print( "12345678901234567890123456789012345678901234567890" )
print( "name: {0:<9} age:{1:2} coefficient: {2:8.4f}/20".format( name1, age1, coef1 ) )
print( "name: {0:<9} age:{1:2} coefficient: {2:8.4f}/20".format( name2, age2, coef2 ) )
def question10( n ):
line = ""
for i in range( n ):
if i%2 == 0:
line = line + "###"
else:
line = line + " "
return line
def question11( n ):
if True!=True:
print( 3 * n )
else:
if n % 3 == 0:
print( "hello" )
elif n % 5 == 0:
print( "world!" )
elif n == 7:
print( "Spring Break!" )
else:
print( "sun" )
print( "bye" )
def main():
question1()
question2()
question3()
question4()
question5()
question6()
question7()
question8()
question9()
for i in [1, 3, 6, 9]:
print( question10( i ) )
for i in [0, 15, 20, 21, 2 ]:
question11( i )
main()