Download CSC 127A - Exam1 - University of Arizona

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ISTA130 Midterm Fall 11 Section Leader_______ Name____________________________150pts
1. Write an X To the left of each valid Python name (identifier). (4pts)
a) __ X pyhonIndetfriaR
c) __ X a_B_C_d
b)
d) ___ x*y
___ 9to5
all or none
2. Write an X To the left of each Python reserved word (keyword). (4pts)
a) _ X if
c) ___ Arizona
b) ___ four
d) _ X for
all or none
3. For each Python expression, indicate its value in the space to its left. Be sure to write a resulting value of the
appropriate type. For example, 2.0 is different than 2 which is also different than '2'. (12pts)
a) __3.5____ 14 / 4
f) _2____ 1 + 2 * 3 // 4
b) ___3___ 14 // 4
g) _1.0___ 1 / 4 * 4
c) ___2___ 14 % 4
h) _7.56__ eval('7.56')
all or none
d) _18.0__ 14 + 4.0
i) _456__ eval('456')
-1/2 if type is wrong (1 not 1.0)
e) _'144'__ '14' + '4'
j) __24__ 99 % 50 % 25
(or 144 not '144')
4. Write the output that would be printed from each of the following code fragments. (8pts)
x = 9
y = x / 2
print('y ==', y)
+1 4.5
y == 4.5
a, b = 5, 9
b, a = a, b
print(a, b)
n = 4
for i in range(1, n + 1, 1):
print(i, end=' ')
95
1234
5. For each Python expression, indicate its value in the space to its left. Be sure to list a resulting value of the appropriate
type. For example, 2.0 is different than 2. Assume import math (6pts)
a) __3.0___ math.sqrt(9.0)
d) __3.46__
round(3.45678, 2)
b) ___3___ abs(5-8)
e) __3.5___
min(3.5678, 3.5)
c) __3.0___ abs(8.0 - 5.0)
f) __-3____
max(-5, -3)
all or none
-1/2 if type is wrong (3 not 3.0)
6. Write the complete dialog (input and output) from this program when the user enters 5 for x followed by 6 for y. (4pts)
x = int(input('x? '))
y = int(input('y? '))
print('Max: ', max(x, y))
print('Min: ', min(x, y))
7. Write a complete Python
program that reads two test scores
and prints the average of the two.
Your dialog should look like this
when the user enters 90.0
followed by 81.0. (6pts)
x? 5
y? 6
Max: 6
Min: 5
1 pt for each correct line
-1 for wrong value after Min or Max
-1/2 for any missing label like x?
t1 = eval(input('Test1? '))
t2 = eval(input('Test2? '))
print('Average:', (t1+t2)/2)
Test1? 90.0
Test2? 81.0
Average: 85.5
1
1 used eval or float
1 used input
1 prompts correct
1 prints Average and a number
2 formula for average correct (t1+t2/2 is not)
8. Write the output generated by the following code. (4pts)
a = 20;
b = 8;
if a % 10 == 0:
b = b * 10
a = a + 10
if a < b:
b = b + 5
print(a, b)
three
All or none
9. For each Python expression, write True if the expression evaluates to true. Write False if the expression evaluates to
false. Assume the following variables always have the values shown. (6pts)
s = 'algorithm'
n = 12
x = 99.9
a) __False__ n + 0.001 == 12
d) _ False___ s >= 'apple' and s <= 'zebra'
b) __False__ x + 1.0 >= 101.0
e) _True____ not(s.upper() == 'Algorithm')
c) __True___ 10 <= n <= 16
f) True____ s[2:4] + s[4:6]=='gori'
All or none
10. Given 2 integers, complete function inRange to return True if both arguments are in the range 0..100 inclusive.
Return False if either is not in the range of 0..100. (10pts)
def inRange(a, b):
return (0 <= a <= 100) and (0 <= b <= 100)
-or more commonlyif (0 <= a <= 100) and (0 <= b <= 100)
return True
else
return False
+3 Expression correct for a 0 <= a <= 100
+3 Expression correct for b 0 <= b <= 100
+2 Use if else to distinguish or return the Boolean Expression
+1 returns True somewhere/somehow
+1 returns False somewhere/somehow
11. Given three integers, complete oneDiffersBy10orMore(a, b, c) to return True if any one integer parameters is
10 or more than either of the other two. Only any two integers need to differ by 10 or more to return True. (12pts)
def oneDiffersBy10orMore(a, b, c):
p1 = abs(a-b) >= 10
p2 = abs(a-c) >= 10
p3 = abs(b-c) >= 10
return p1 or p2 or p3
def oneDiffersBy10orMore(a, b, c):
maximum = max(max(a,b),c)
minimum = min(min(a,b),c)
return maximum - minimum >= 10
def oneDiffersBy10orMore(a, b, c):
# Most common solution
if abs(a - b) >= 10:
return True
if abs(a - c) >= 10:
return True
if abs(c - b) >= 10:
return True
return False
2
12. Write the output generated by the following code (2pts)
s = 'Madison'
if s < 'D':
print('one')
elif s < 'M':
print('two')
elif s < 'Q':
three
print('three')
else:
print('four')
(all or none)
13. Complete function weekEndOrDay(day) to return the string 'WeekEnd' if the argument day is 6 or 7 or return the
string'WeekDay' if the argument day is 1, 2, 3, 4, or 5. If day is outside the range of 1 through 7 return '??'. (12pts)
weekEndOrDay(1)
weekEndOrDay(5)
weekEndOrDay(6)
weekEndOrDay(7)
weekEndOrDay(0)
weekEndOrDay(8)
returns
returns
returns
returns
returns
returns
'WeekDay'
'WeekDay'
'WeekEnd'
'WeekEnd'
'??'
'??'
def weekEndOrDay(day):
if day <= 0 or day >= 8:
return '??'
elif 1 <= day <= 5:
return 'WeekDay'
else:
return 'WeekEnd'
14. For each Python expression, indicate its value in the space to its left. Assume the string type variable s is assigned the
string value 'park union' . Use ' ' when the expression evaluates to a string. (4pts)
s = 'park union'
a) ___'ru'___ s[2:3] + s[5]
b) _
'paun'_ s[0:2] + s[5:7]
c) ____3_____ len(s[1:4])
d) ____20_____ 2 * len(s)
All or none ' ' not needed
15. Complete middleTwo(s) to return the middle two characters of s. When the length of s is odd, favor the left. (8pts)
middleTwo('A')
middleTwo('AB')
middleTwo('ABC')
middleTwo('ABCD')
middleTwo('ABCDE')
def middleTwo(s):
if len(s) <= 2:
return s
else:
mid = len(s) // 2 -1
return s[mid:mid+2]
returns
returns
returns
returns
returns
'A'
'AB'
'AB'
'BC'
'BC'
+1 returns anything
+1 returns any part of string
+2 returns exactly a string of length 2
+3 returns the correct two characters
+1 Grader discretion / No syntax error
3
16. How many times will each code fragment print 'Hello '? 0 and infinite are legitimate answers. (12pts, 3 pts each)
n = 1
for lcv in range(n):
for lcv in range(5):
# Tricky
n = 5
c = 1
while c <= n:
n = 6;
c = 0;
while c <= n:
print('Hello ')
c = c + 2
5
print('Hello ')
1
print('Hello ');
All or none
print('Hello ')
c = 1
4
infinite
17. Write the output generated by this Python code. (4pts)
s = 'UOA'
for index in range(len(s) - 1):
print(s[index], '_', s[len(s) - index - 1])
U_A
O_O
+1 for each letter U A O O
-2 for 3rd line
18. Complete function howSwedish(s) to return how often the string 'Abba' occurs in s. This is case insensitive so
any mix of upper and lower case 'AbBa' must count as 1 occurrence. 'Abba' may overlap. (12pts)
howSwedish('')
returns
howSwedish('abb')
returns
howSwedish('Abba_aBBa') returns
howSwedish('abba')
returns
howSwedish('abbabba')
returns
howSwedish('AbbAbbAbba')returns
howSwedish('abNOTba')
returns
def howSwedish(s):
result = 0
s = s.upper()
for i in range(len(s) - 3):
if s[i:i+4] == 'ABBA':
result += 1
return result
0
0
2
1
2 (overlap)
3 (overlap)
0 ('Abba' must be consecutive, 'abba' cannot be split with other characters)
+2 Maintains a count (r to the left)
1 initialize to 0
+1 Loop exists
+3 Comparison to 'ABBA' ('abba' if lower() used)
1 if
1 ==
1 correct string slicing
+2 Case insensitive
4
19. For each Python expression, indicate its value in the space to its left. Assume the following list type variable list
always has the assigned value of [-9, 1, 9, 3, 4] (4pts)
list = [-9, 1, 9, 3, 4]
a) ____1___ list[1]
d) __
0____ list[0] + list[2]
b) ____5___ len(list)
e) ___3____ list[len(list)-2]
All or None
20. Complete function sumAllOdds(list) to return the sum of all odd integers in the given list, including negative
integers. If there are no odd numbers in list, return 0. (12pts)
sumAllOdds(
sumAllOdds(
sumAllOdds(
sumAllOdds(
sumAllOdds(
sumAllOdds(
[1, 2] )
returns 1
[1, 2, 3, 4] ) returns 1 + 3 == 4
[-3, -2, -1] ) returns -3 + -1 == -4
[-99] )
returns -99
[] )
returns 0 (when len(list) == 0 return 0)
[2, 4, 6] )
returns 0 (list has no odds, so return 0)
def sumAllOdds(list):
sum = 0
for i in range(len(list)):
if list[i] % 2 != 0:
sum += list[i]
return sum
+3 Maintains sum
1 initialize to 0
2 adds list element when appropriate
+3 Loop
1 exists
1 syntax
1 upper bound exclusive is len(list)
+5 Decision
1 if exists
4 determine odd numbers correctly
indexes list element with []
logic correct (True when odd)
+1 returns
Special case:
-2 returning too early
5