Microsoft Word - Decision Exercises.docx
Decision Exercises
#1 More BMI (10 points)
If you completed the BMI exercise in the Basic applications, you could continue expanding that
application here. The video sequence added decisions to the logic to provide the user with an
indication of their BMI’s weight status. Again you need to merely follow along with the video
sequence and produce the exact results. The autograder will provide a basic indication if your
code produces the four categories, even if your logic is slightly different from mine.
https:
www.youtube.com/playlist?list=PLgvXUCQxGQrxU5_0KnzFWrAd3xgKx48uW
#2 Animal Classifier (20 points)
When algorithms include decisions, they naturally become more complex to code and also to
test. Part of successfully using decisions means analyzing the appropriate test cases required to
ensure the resulting code indeed works as planned. The following flowchart was proposed as
an algorithm for classifying most animals based on their traits.
Each result in this flowchart (e.g., Reptile, Mammal, Fish…) defines a new test case that needs
you to define the input values that would lead to that scenario.
1 -Test Planning
The flowchart above has many possible outcomes and sets of answer to the questions (i.e.,
inputs) that will result in each outcome. Testing decisions starts by identifying all possible
outcomes and ensuring an input dataset exists to reach each outcome. Use a table like the one
elow to plan out your tests. You can create this table electronically or on paper, you won’t
turn in step 1, but it is a good exercise to plan your testing.
Results Has
Skeleton
Has
Wings
Has
Feathers
Lays
Eggs
Drinks
Milk
Undergoes
Metamorphosis
Has Gills
Insect False False False False False False False
* The number of rows provided does not indicate the number of test cases expected
2 – Fill the data for automated testing
This step translates the inputs and expected results from the table above into your Python
code. At the top of the animalClassifier.py file is a list of variables that define the logic’s result
(e.g., AMPHIBIAN = “Amphibian”). Below these variables is a Python list (a data structure we
will practice using in a few weeks) that defines the test cases. Your data from above will drive
the testing!
The table contains each of the possible outputs from the diagram above. As the code stands
now, every input is set to False. Your table above should indicate which of the values should
instead be True to determine if the animal is an insect, bird, or whatnot. Very carefully modify
the appropriate values from False to True (it must be a capital T). If you accidentally delete a
comma (,) or square
acket ([]), the code will
eak.
3 – The poor miscategorized platypus
Sometimes on a software project, someone will provide you with exact rules they expect you to
code, like the flowchart above. The flowchart above, though, has at least one logical problem!
Think of the humble platypus. It has a skeleton, and its young drinks milk, yet it also lays eggs,
yet the logic does not accurately report the platypus as a mammal (Can you predict what it
would return?). You can help classify the platypus and earn some points by improving the logic.
You do not need to add the platypus test case to your table above, I have a separate test case
for that already!
#3 States of Matter (15 points)
In basic science, we learn that matter has three states – solid, liquid, and gas. One easy
example of this concept is how water transitions between ice, its fluid state, and steam as it
changes temperature, but there is some transition space between these temperatures in
physical testing (if you freeze or boil water, it does not suddenly solidify or evaporate). In this
exercise, I have written some code that
attempted to show the transitions
etween these states by having a
threshold in the logic. The hope was to
epresent the continuum between
states with a small temperature
threshold between each state.
In the na
ow band of 3 degrees above
and below each temperature (i.e., the
threshold), the app should report the
transition state rather than the ‘main
three’, but mine does not work
co
ectly.
1 – Fix my
oken code
Your task is to fix my code so that it passes each test case. The file comes with a built-in tester,
so your main goal is to make those failures turn into successes. Start by identifying the failing
test cases and their reason for failing, then modify the code to the desired outcome.
2 – Try your own version of the logic
A programmer can write code to realize a set of rules in many ways. To practice your skills at
writing decision logic, I provided a second method named determineStateAlternative().
For more points, write your version of this decision logic from scratch (i.e., don’t copy mine)
within this method such that it also passes the test cases.
#4 Rock, Paper, Scissors (30 points)
The game Rock, Paper, Scissors presents two players with a simple choice that could result in
either player winning or a tie. The rules of the game are easy to represent as a picture, but the
picture does not translate directly into an algorithm using decisions
I started a Rock, Paper, Scissors game (rps.py)
ut have not yet coded several critical decisions
that you must implement to finish the game.
Download the code and start by running it. The
provided code includes test cases followed by a
user interface. Work through the code (top-
down) and add one piece at a time. Remember
to “code small”, ensuring your code is always
unning without e
ors. Do not attempt to code
all steps before testing! You will need to add logic at the following places:
1. To play against the computer, we need to select a choice the computer player makes
andomly. The function getComputerMove() generates a random number (0, 1, or
2) into the variable move. Add logic here to return one of the constants ROCK, PAPER,
or SCISSORS as the computer’s move using a statement such as
return ROCK
2. To get the player’s move, we need to ensure their input is one of the valid moves. The
getPlayerMove() function defines the iteration logic for you and prompts the user for
their input, storing the value in a variable move. Add logic to this method to ensure the
user has typed in either “rock” (or “r”), “paper” (or “p”), or “scissors” (or “s”) and return
the appropriate constant value as described above. If the user does not enter one of
the three recognizable moves, print a line that tells them the valid options they can
enter.
3. Select the winner – your first task is to create a series of decisions within the function
winner(player1Move, player2Move)that realizes the game’s rules as dictated
in the picture above. Your logic must return the winning player number (1 or 2) or 0 if it
was a tie. The function parameters are set to one of the constant variables (ROCK,
PAPER, SCISSORS) to ensure your logic matches the valid input options. The
automated test cases will help you to know when you have completed this function
4. The player needs to know the result of the game, and we need a decision to tell them.
Modify the playGameWithUser() function to report the result and keep the score.
Update the variable reportResult to one of the three constant values
(RESULT_TIE, RESULT_P1, or RESULT_P2). Then update the three variables that
track each outcome’s count using the variables provided (player1Wins,
player2Wins, and ties).
#5 Coin Meter Classifier (30 points)
A toll booth system uses high-tech sensors to determine the diameter and weight of coins
dropped into a basket to pay for tolls. The file CoinMeter.py has started the code for determining
which type of coin was deposited by defining the known weights and diameters for pennies,
nickels, dimes, and quarters for the most common US cu
ency values. Since coins may not
conform exactly to these measures, the system allows for a threshold for the weight and
diameter, also provided as a constant in the code. Your code should check if an incoming coin is
within this threshold. If the weight threshold is 3%, a coin weighs between 97% and 103% of the
target weight of a penny, for example, to be considered a penny. A coin must be within the
thresholds of both the diameter and weight.
Individual determinination
The file coinMeter.py includes a constant value of each coin type and the expected weight and
diameter for each coin. You must complete the functions:
isAPenny
isANickel
isADime
isAQuarter
The caller to each function provides the weight and diameter of a candidate coin. Your code
should check if the values fall within the threshold of that coin’s values and return True if it does
or False if it does not.
You can also complete the classifyCoin function, which identifies which type of coin a caller
presented (PENNY, NICKEL, DIME, QUARTER, or UNKNOWN). The logic should checks if the
provided weight and height fall into any of the four mutually exclusive coin thresholds (e.g., you