15-110 Spring 2011
Recitation 4
- Quiz 2
- Hw2 Review
We will briefly review some solutions to hw2, discussing more and less effective ways to solve some of the exercises.
- Better Mastermind (No duplicates)
Starting
from last week's sample solution for Mastermind, change the game so
that if the player makes a duplicate guess (same guess two times), they
lose immediately. Hint: you may want to use lists for this
(why?).
- findDuplicates
Write
the function findDuplicates that takes one parameter, a list, and
returns a list of all the duplicate values in the original list.
The resulting list should be in sorted order, with no duplicates
in it. Try to do this somewhat efficiently. Here are some
test cases for you:
list = [ 1, 2, 3, 4, 5, 6, 7, 6, 4, 2, 4, 6 ]
dups = [ 2, 4, 6 ]
assert(findDuplicates(list) == dups)
list = [ 1, 2, 3, 4, 5 ]
dups = [ ]
assert(findDuplicates(list) == dups)
list = [ 7, 7, 7, 7, 7, 7, 7 ]
dups = [ 7 ]
assert(findDuplicates(list) == dups)