Key to Fall 2015 Coding Questions

 CS 111X - Fall 2015 - Final Exam
5/10
Computing ID: __________________________
Functions [25 pts total]
20. Write a function called ​centered_average​ that takes a list of ​ints​ and calculates the average
over the values ​but​ ignores the largest and smallest values in the list. If there are multiple copies of
the largest or smallest value, just ignore one copy. You can assume the list has at least 3 items in it.
The value returned should be an integer. [10 pts]
def ​centered_average(​lst​):
​lst​.remove(​max​(​lst​))
​lst​.remove(​min​(​lst​))
​return ​sum​(​lst​) // ​len​(​lst​)
2 pts header
4 pts removes min and max
4 pts calculates avg correctly
Several ways to do this! I expect some will loop over list multiple times to find min
and max. This is okay as long as it works.
CS 111X - Fall 2015 - Final Exam
6/10
Computing ID: __________________________
21. Write a function called ​palindrome​ that returns ​True​ or ​False​ whether the string passed in is
the same forwards and backwards, including case. i.e. “racecar”, “mom”, and “abcba” should return
True ​[10 pts]
def ​palindrome(my_str):
reverse = ​""
​for ​i ​in ​my_str:
reverse = i + reverse
​if ​reverse == my_str:
​return True
else​:
​return False
2 pts header, 6 pts algorithm, 2 pts proper return
Again, several ways to do this!
22. Read the following code, then explain what it does in one sentence. [5 pts]
def ​mystery_function(list):
x = ​0
​y = ​0
​for ​i ​in ​list:
​if ​i < ​0​:
y += ​1
​else​:
x += ​1
​return ​x > y
This function returns true if the list has more positive numbers than negative.
5 pts correct and well stated, 3 pts correct and poorly/overly long stated, 1 pt somewhat there
CS 111X - Fall 2015 - Final Exam
7/10
Computing ID: __________________________
Programming [25 pts total]
Santa needs your help! He’s been putting his list together as a standard
csv file (because, why not?), but now he needs to convert that data into
something that’s going to be easier for him to use when he’s giving out
presents and for the elves to load his sleigh!
He needs you to put together three things:
1. All the kids who were “nice,” with their names and presents;
2. All the kids who were “naughty,” also with their names and
“presents”;
3. All the presents, with the list of kids who gets them.
For example, when he looks up “Sammy” with the “nice” kids, he should get back that she is getting a
“ball.” When he looks up “ball” with the presents, he should get back a list with both “Sammy” and
“Davey” in it.
Here is an example of Santa’s list. Of course, his list is MUCH longer than this, so don’t code
specifically to this data!
Bill,nice,yo­yo Carl,naughty,coal Sally,nice,doll Susie,nice,bicycle Davey,nice,ball Gretchen,naughty,coal Steve,nice,bicycle Sammy,nice,ball It’s up to you, oh software development elf, to figure out the best way to do this and give Santa back
the data he desperately needs to make the holidays happen! Figure out how to create the
appropriate data structures that contain the information Santa needs. At the end of your program, it
should be apparent what Santa should look at to know the nice kids, naughty kids, and the overall
present allocation. You do not need to write any functions for this answer, but you can if you want.
Your answer MUST be limited to the blanks on the next page. Best of luck!
CS 111X - Fall 2015 - Final Exam
8/10
Computing ID: __________________________
Santa’s List Program
Only code on this page will be graded! So practice on scratch paper if needed!
santas_list = ​open​(​"santas_list.csv"​, ​"r"​)
nice = {}
naughty = {}
presents = {}
for ​line ​in ​santas_list:
kid = line.strip().split(​','​)
​if ​kid[​1​] == ​'nice'​:
nice[kid[​0​]] = kid[​2​]
​else​:
naughty[kid[​0​]] = kid[​2​]
​if ​kid[​2​] ​in ​presents:
presents[kid[​2​]].append(kid[​0​])
​else​:
presents[kid[​2​]] = [kid[​0​]]
print​(nice) // printing is not necessary
print​(naughty)
print​(presents)
5 pts recognizing good data structure to use (doesn’t HAVE to be dict if it works right)
5 pts reading the file properly
5 pts populating the data structures
10 pts overall algorithm and good code