Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CS-UY 1114 / Python

Second Midterm Exam – 20 November 2018

Question 1 (18 points)

For each of the following, write what will be printed on the screen, or write ERROR if the code causes an error.

 

code fragment

output (or ERROR)

.

var = [1, 2, 3]

var[3] = var[1] + 3

print(var)

 

.

ls = ['a','b','c','d']

ls = ls[:2] + ls[2:]

print(ls)

 

.

var = [1, 2, 3]

var.pop(2)

print(var)

 

.

st = 'The Quick'

print(st.find('elephant'))

 

.

st1 = 'Tan'

st2 = 'don'

print([st1, st2])

 

 

.

ls = [4]

stuff = (1, 2, 3, ls)

ls.append(5)

print(stuff)

 

 

.

st1 = 'Tur'

st2 = st1 + 'key'

st1 += st2

print(st2)

 

 

.

st = 'homework'

print(st[4::-2])

 

 

.

ls = ['CS']

st = '1114'

print(ls + st)

 

Question 2 (12 points)

Given the following definitions:

def top_func(val) :

print("top , val=" ,val)

if val > 10 :

return val // 10

else :

return val - 1

def bottom_func(val) :

print("bottom start , val=" ,val)

val = top_func(val)

print("bottom middle , val=" ,val)

val = top_func(val)

return val

def main() :

val = 35

result = bottom_func(val)

print("main , val=" ,val)

print("main, result=",result)

What will be printed when main() is called?

Output:

Question 3 (15 points)

Given the following definitions:

def f1(item) :

retval = ' '

i = 0

for ch in item :

if i % 2 == 0 :

retval += ch

i += 1

return retval

def f2(item) :

print("f2 item=" ,item)

if len(item) > 3 :

res = item[ :-(len(item) // 2)]

else :

res = f1(item[ : :-1])

print("f2 res =" ,res)

return res

def main() :

st = "Midterm two"

ls = st .split()

for i in range(len(ls)) :

ls[i] = f2(ls[i])

print(ls)

What would be printed when calling main()?

Output:

Question 4 (10 points)

Given the following definitions:

def func1(ls) :

ls .sort()

def func2(ls) :

ls = []

return ls

def main() :

ls = [9 , 29 , 7 , 13]

func1(ls)

print("ls=" ,ls)

ls2 = ls

ls3 = func2(ls2)

print("ls2=" ,ls2)

print("ls3=",ls3)

What would be printed when calling main()?

Output:

ls=

ls2=

ls3=

Question 5 (25 points)

Write a function find_room that will assign students to a classroom where they should take their final exam.  Your function should take two arguments:

a)  a list of room specifications, where each room specification is a tuple giving the low and high bound of last names that should be assigned to that room;

e.g.  [("Aa","Mz"),("Na", "Za")]

b)  a list of students' last/family names. e.g.  ['Jackson', 'Hendrix', 'Cobain', 'Presley']

Your function will return a list of lists of student names, such that the first sublist contains all students assigned to the first room, and so on.  Given the examples above, your function    would return  [['Jackson', 'Hendrix', 'Cobain'], ['Presley']] meaning        Jackson, Hendrix, and Cobain belong in room 1 (the room with names Aa thru Mz) and       Presley belongs in room 2 (the room with names Na through Za). The order in which           names appear in each resulting sublist does not matter.

Here is an example:

>>> find_room([("Aa","Mz"),("Na", "Za")],["Gordon","Ypsilon","Lightyear","Skywalker"])

[['Gordon', 'Lightyear'], ['Ypsilon', 'Skywalker']]

If a student has a last/family name that cannot be classified into one of the given rooms,     their name should be omitted from the results. For example, although Aardvark is assigned to the first room above, Zuniga has a last name outside of the range of all available rooms, and therefore she is not assigned to any room.

>>> find_room([("Aa","Mz"),("Na", "Za")],["Aardvark","Zuniga"])

[['Aardvark'], []]

You may assume that the room specifications do not overlap; that is, there is at most one room that a student can be assigned to.

Hint: recall that you can compare lexicographical order of strings using the usual comparison operators.

Question 5 answer

def find_room(rooms, students):

# sig: list(tuple(str, str)), list(str) -> list(list(str))

Question 6 (20 points):

Hashtags have become a popular addition to most social media platforms and can be used for easy categorization and association of posts.  Many hashtags are a single word             (#Midterms), but some are multiple words (#SpatulaKing, #midtermsAreCool).  We would   like to write an interpreter for hashtags to help convert them to a list of separated words like ["midterms", "are", "cool"].  You can trust that the person writing the hashtag      has capitalized the first letter of every word after the first and no other capitals are in the     string.  You should make sure that your resulting list has no capitals.

Write a function, called htl, which will receive the hashtag, as a string, and will return a list of the words in the hashtag.  For example:

htl("#midtermsAreCool") == ['midterms', 'are', 'cool']

htl("#SpatulaKing") == ['spatula', 'king']

You may assume that the parameter is a valid hashtag, beginning with a "#" and followed by a sequence of upper- and lower-case letters.

def htl (hashtag):

# sig: str -> list(str)