Professional Documents
Culture Documents
Test Bank For Building Python Programs Plus Mylab Programming With Pearson Etext Stuart Reges Marty Stepp Allison Obourn Full Download
Test Bank For Building Python Programs Plus Mylab Programming With Pearson Etext Stuart Reges Marty Stepp Allison Obourn Full Download
Test Bank For Building Python Programs Plus Mylab Programming With Pearson Etext Stuart Reges Marty Stepp Allison Obourn Full Download
The three entries below have specific values for the first and second parameter to function mystery. For each entry,
indicate what values would be stored in the dictionary returned by function mystery if the given dictionaries are passed
as parameters.
Page | 1
2. 2D List Mystery
In the left-hand column below are specific two-dimensional lists. You are to indicate in the right-hand column what
values would be stored in the set returned by function mystery if the list in the left-hand column is passed as a
parameter to mystery.
Page | 2
3. Searching and Sorting
(a) Suppose we are performing a binary search on a sorted list called numbers initialized as follows:
# index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
numbers = [-9, -6, -2, -1, 0, 1, 3, 4, 5, 7, 9, 10, 12, 19, 23, 26]
# search for the value 3
index = binary_search(numbers, 3)
Write the indexes of the elements that would be examined by the binary search (the mid values in our algorithm's
code) and write the value that would be returned from the search. Assume that we are using the binary search
algorithm shown in lecture and section.
(b) Write the state of the elements of the list below after each of the first 3 passes of the outermost loop of the
selection sort algorithm.
numbers = [37, 19, 9, 48, 13, 55, 74, 2]
selection_sort(numbers)
(c) Trace the complete execution of the merge sort algorithm when called on the list below, similarly to the
example trace of merge sort shown in the lecture slides. Show the sub-lists that are created by the algorithm and show
the merging of sub-lists into larger sorted lists.
numbers = [37, 19, 9, 48, 13, 55, 74, 2]
merge_sort(numbers)
Page | 3
4. String Programming
Write a function called same_dashes that takes two strings as parameters and that returns whether or not they have
dashes in the same places (returning True if they do and returning False otherwise). Strings of different lengths
should always cause your function to return False. See examples below.
call returned value
same_dashes("hi--there-you.", "12--(134)-7539") True
same_dashes("-15-389", "-xy-zzy") True
same_dashes("criminal-plan", "(206)555-1384") True
same_dashes("abc", "9.8") True
same_dashes("1st-has-more characters", "2nd-has-less") False
same_dashes("1st-has-less", "2nd-has-more characters") False
In solving this problem you may not use count or find.
Page | 4
5. 2D List Programming
Write a function called cap that takes 2D list and a number as parameters. Your function should replace any numbers
stored in the passed in list of lists that are greater than the passed in number with the passed in number. Examples are
shown in the table below using the below list. Note that the passed in list might be of any size.
data = [[18, 14, 29], [12, 7], [2, 22, 5]]
Page | 5
6. Collections Programming
Write a function called overlap that takes a set of integers and a list of integers as parameters and that returns a new
set containing values that appear in both structures. For example, given set and list:
set1: {0, 19, 8, 9, 12, 13, 14, 15}
You are not allowed to construct any structures, besides the set you will return, to solve this problem. You may not alter
the passed in list or set. You may not convert the list to a set.
Page | 6
7. Collections
Write a function called split that takes a set of strings as a parameter and that returns the result of splitting the strings
into different sets based on the length of the strings. In particular, your function should return a dictionary whose keys
are integers and whose values are sets of strings of that length. For example, if a variable called words contains the
following set of strings:
{to, be, or, not, that, is, the, question}
then the call split(words) should return a dictionary whose values are sets of strings of equal length and whose keys
are the string lengths:
{2={be, is, or, to}, 3={not, the}, 4={that}, 8={question}}
Notice that strings of length 2 like "be" and "is" appear in a set whose key is 2. If the set had instead stored these
strings:
{four, score, and, seven, years, ago, our, fathers, brought, forth}
Your function should construct the new dictionary and each of the sets contained in the dictionary but should otherwise
not construct any new data structures. It should also not modify the set of words passed as a parameter.
Page | 7
8. List Programming
Write a function called odds_to_back that takes a list of integer values as a parameter and that moves all odd
numbers to the back of the list, preserving their relative order. For example, if a variable called list stores this
sequence of values:
[7, 2, 8, 9, 4, 13, 7, 1, 9, 10]
then the following call:
odds_to_back(list)
should leave the list with the following values:
[2, 8, 4, 10, 7, 9, 13, 7, 1, 9]
Notice that the list begins with the even values in their original order followed by the odd values in their original order.
You may not construct any extra data structures to solve this problem. You must solve it by manipulating the list you are
passed as a parameter.
Page | 8
9. Programming
Write a function called acronym that takes as a parameter a string containing a phrase and that returns a string that has
the acronym for the phrase. For example, the following call:
acronym("self-contained underwater breathing apparatus")
should return "SCUBA". The acronym is formed by combining the capitalized first letters of each word in the phrase.
Words in the phrase will be separated by some combination of dashes and spaces. There might be extra spaces or
dashes at the beginning or end of the phrase. The string passed as a parameter will not contain any characters other
than dashes, spaces, and letters, and is guaranteed to contain at least one word. Below are several sample calls.
Page | 9
Final Sample 5 Solutions
1.
{'baz': 'wind', 'bar': 'earth', 'mumble': 'fire', 'foo': 'air'}
{'five': 'quatro', 'one': 'dos', 'three': 'tres'}
{'g': 'seven', 'b': 'years', 'c': 'seven', 'e': 'ago'}
2.
{1, 2, 13, 14}
{28, 29, 30, 7}
{3, 14, 15, 21, 23, 24}
3.
a)
7, 3, 5, 6
6
b)
[2, 9, 13, 48, 19, 55, 74, 37]
c)
[37, 19, 9, 48, 13, 55, 74, 2]
[37, 19, 9, 48] [13, 55, 74, 2]
[37, 19] [9, 48] [13, 55] [74, 2]
[37] [19] [9] [48] [13] [55] [74] [2]
[19, 37] [9, 48] [13, 55] [2, 74]
[9, 19, 37, 48] [2, 13, 55, 74]
[2, 9, 13, 19, 37, 48, 55, 74]
4.
def same_dashes(str1, str2):
if len(str1) != len(str2):
return False
for i in range(len(str1)):
if (str1[i] == '-' and str2[i] != '-') or (str2[i] == '-'
and str1[i] != '-'):
return False
return True
5.
def cap(data, big):
for i in range(len(data)):
Page | 10
for j in range(len(data[i])):
if data[i][j] > big:
data[i][j] = big
6.
def overlap(s, lis):
result = set()
for el in lis:
if el in s:
result.add(el)
return result
7.
def split(words):
buckets = {}
for word in words:
n = len(word)
if n not in buckets:
buckets[n] = set()
buckets[n].add(word)
return buckets
8.
def odds_to_back(data):
i = 0
length = len(data)
while i < length:
if data[i] % 2 == 1:
old = data.pop(i)
data.append(old)
length -= 1
else:
i += 1
9.
def acronym(s):
in_word = False
s = s.upper()
result = ""
for i in range(len(s)):
ch = s[i]
Page | 11
Test Bank for Building Python Programs Plus MyLab Programming with Pearson eText, Stuart Reg
Page | 12