Lab 2 Test-Driven Development
Lab 2 Test-Driven Development
In this lab you will get hand-on experience on test driven development (TDD).
Submissions:
1. All your code files. Your code should be self-documenting and contain necessary comments.
2. A document which describes your test case design and reports output from testing and test coverage.
Set is a card game about finding patterns. Each card contains a design with 4 different properties: color
(red, green or purple), shape (diamond, oval or squiggly), quantity (one, two, or three) and pattern (solid,
striped or outlined). A set is a group of three cards which are either all the same or all different for each
property. That is to say, any feature in the 'Set' of three cards is either common to all three cards or is
different on each card. You can try playing Set online . A more detailed description of the game rule can
be found in http://smart-games.org/en/main/rules/.
To implement the game, each Set card can be uniquely represented by a 4-bit integer in base 3, where each
digit represents a different property and each property has three possible values. A full hand in Set is a
group of twelve unique cards, so a hand can be represented by a list of twelve 4-digit integers in base 3. For
example, the hand shown right could be represented by the following list.
def count_sets(cards):
"""Return the number of sets in the provided Set hand.
Parameters:
cards (list(str)) a list of twelve cards as 4-bit integers in
base 3 as strings, such as ["1022", "1122", ..., "1020"].
Returns:
(int) The number of sets in the hand.
Raises:
ValueError: if the list does not contain a valid Set hand, meaning
- there are not exactly 12 cards,
- the cards are not all unique,
- one or more cards does not have exactly 4 digits, or
- one or more cards has a character other than 0, 1, or 2.
"""
pass
def is_set(a, b, c):
"""Determine if the cards a, b, and c constitute a set.
Parameters:
a, b, c (str): string representations of 4-bit integers in base 3.
For example, "1022", "1122", and "1020" (which is not a set).
Returns:
True if a, b, and c form a set, meaning the ith digit of a, b,
and c are either the same or all different for i=1,2,3,4.
False if a, b, and c do not form a set.
"""
pass
Task
1. Write unit tests for the functions, but do not implement them yet. Focus on what the functions should
do rather than on how they will be implemented.
Hint: if three cards form a set, then the first digits of the cards are either all the same or all different. Then
the sums of these digits can only be 0, 3, or 6. Thus a group of cards forms a set only if for each set of
digits—first digits, second digits, etc.—the sum is a multiple of 3.
2. Implement the actual functions, making changes until all tests pass.
3. It is not sufficient to just have tests, but to rigorously test the code. Examine the test coverage, add
additional test cases if necessary.