Answer To: Microsoft Word - Decision Exercises.docx Decision Exercises #1 More BMI (10 points) If you completed...
Sandeep Kumar answered on Apr 23 2021
DT/.vscode/settings.json
{
"python.pythonPath": "C:\\Users\\nax\\AppData\\Local\\Programs\\Python\\Python39\\python.exe"
}
DT/animalclassifier-eurjlcjv.py
# From: https://www.researchgate.net/publication/315146311_ANIMAL_CLASSIFICATION_IN_WILDLIFE_THROUGH_IMAGES_USING_STATISTICAL_METHODS_AND_DECISION_TREE
AMPHIBIAN = "Amphibian"
BIRD = "Bird"
FISH = "Fish"
INSECT = "Insect"
MAMMAL = "Mammal"
REPTILE = "Reptile"
#....................... 2 ..............................
# Modify the following data structure to match your defined test cases.
# The inputs to the test cases are next represented by Boolean values
# (must be True and False, case sensitive)
# that align to one of the input values in the following order:
# skeleton, wings, feathers, eggs, milk, meta, gills
TEST_CASES = [[INSECT , [False, False, False, False, False, False, False]],
[BIRD , [True, True, True, True, False, False, False]],
[MAMMAL , [True, True, False, True, True, False, False]],
[MAMMAL , [True, True, False, True, True, False, False]],
[AMPHIBIAN , [True, False, False, True, False, True, False]],
[REPTILE , [True, False, False, True, False, False, False]],
[FISH , [True, False, False, True, False, False, True]]]
# The following code, AnimalClassifier, is a class. It helps to group data
# and is a vital part of Object-Oriented programming. We won't do a lot with
# classes in this course, but they are important later. For now, it is just a way
# of grouping the various properties to classify an animal.
class AnimalClassifier:
hasSkeleton = False
hasWings = False
hasFeathers = False
laysEggs = False
drinksMilk = False
undergoesMetamorphasis = False
hasGills = False
#................... 3 ..........................................
def classify(self):
'''
Use the attributes of the Animal Classifier task to determine the animal
self - an grouping of the AnimalClassifier attributes
returns the class of the animal with those properties
'''
if self.hasSkeleton:
if self.hasWings:
if self.hasFeathers:
return BIRD
else:
return MAMMAL
else:
if not self.laysEggs:
if self.drinksMilk:
return MAMMAL
if self.undergoesMetamorphasis:
return AMPHIBIAN
else:
if self.hasGills:
return FISH
if self.drinksMilk:
return MAMMAL
else:
return REPTILE
else:
return INSECT
#==================================================================
# Everything below here is tester code DO NOT MODIFY
def __init__(self, skeleton, wings, feathers, eggs, milk, meta, gills):
'''
Initializes the class with the provided values
'''
self.hasSkeleton = skeleton
self.hasWings = wings
self.hasFeathers = feathers
self.laysEggs = eggs
self.drinksMilk = milk
self.hasGills = gills
self.undergoesMetamorphasis = meta
def getBoolean(prompt):
'''
Prompt the user a question that is answered true or false
prompt - the text to ask the user
returns True or False as specified by the user
'''
while True:
answer = input(prompt).lower()
if (answer == "true" or answer == 't'):
return True
elif (answer == "false" or answer == 'f'):
return False
else:
print("You must answer True (t) or False (f)")
def presentToUser():
'''
Ask the user a series of questions that help to classify the animal and
then show them the result
'''
hasSkeleton = getBoolean("Does your animal have a skeleton:")
hasWings = getBoolean("Does it have wings:")
hasFeathers = getBoolean("Does it have feathers:")
laysEggs = getBoolean("Does it lay eggs:")
drinksMilk = getBoolean("Does its young drink milk:")
undergoesMetamorphasis = getBoolean("Does it undergo a metamorphasis:")
hasGills = getBoolean("Does it have gills:")
classifier = AnimalClassifier(hasSkeleton, hasWings, hasFeathers, laysEggs,
drinksMilk, undergoesMetamorphasis, hasGills)
print("Your animal is a", classifier.classify())
def testClassifier():
'''
Test the classify logic using the test cases defined above
'''
success = True
for testCase in TEST_CASES:
answer = testCase[0]
parameters = testCase[1]
test = AnimalClassifier(parameters[0], parameters[1], parameters[2], parameters[3],
parameters[4], parameters[5], parameters[6])
result = test.classify()
if result != answer:
success = False
print ("Test case Failed: I expected", answer, "but got", result, "for the inputs", parameters)
return success
def testPlatypus():
success = True
answer = MAMMAL
parameters = [True, False, False, True, True, False, False]
test = AnimalClassifier(parameters[0], parameters[1], parameters[2], parameters[3],
parameters[4], parameters[5], parameters[6])
result = test.classify()
if result != answer:
success = False
print ("Platypus Test case Failed: I expected", answer, "but got", result, "for the inputs", parameters)
return success
if __name__ == '__main__':
if testClassifier() and testPlatypus():
print("All test cases passed!")
print(".................")
print("Classify your own animal now...")
presentToUser()
DT/coinmeter-qmgjurkp.py
PENNY = 0
NICKEL = 1
DIME = 2
QUARTER = 3
UNKNOWN = 4
PENNY_WEIGHT = 2.5
NICKEL_WEIGHT = 5.0
DIME_WEIGHT = 2.268
QUARTER_WEIGHT = 5.67
PENNY_DIAMETER = 0.75
NICKEL_DIAMETER = 0.835
DIME_DIAMETER = 0.705
QUARTER_DIAMETER = 0.955
WEIGHT_THRESHOLD = 0.03
DIAMETER_THRESHOLD = 0.01
def isAPenny(weight, diameter):
'''
Checks to see if the coin is a penny
weight - the weight of current coin to evaluate
diameter - the diameter of current coin to evaluate
returns True if coin is a penny
'''
if ((0.97*PENNY_WEIGHT <= weight and weight <= 1.03*PENNY_WEIGHT) and (0.97*PENNY_DIAMETER <= diameter and diameter <= 1.03*PENNY_DIAMETER)):
return True
else:
return False
def isANickel(weight, diameter):
'''
Checks to see if the coin is a nickel
weight - the weight of current coin to evaluate
diameter - the diameter of current coin to evaluate
returns True if coin is a nickel
'''
if ((0.97*NICKEL_WEIGHT <= weight and weight <= 1.03*NICKEL_WEIGHT) and (0.97*NICKEL_DIAMETER <= diameter and diameter <= 1.03*NICKEL_DIAMETER)):
return True
else:
return False
def isADime(weight, diameter):
'''
Checks to see if the coin is a dime
weight - the weight of current coin to evaluate
diameter - the diameter of current coin to evaluate
returns True if coin is a dime
'''
if ((0.97*DIME_WEIGHT <= weight and weight<= 1.03*DIME_WEIGHT ) and ( 0.97*DIME_DIAMETER <= diameter and diameter <= 1.03*DIME_DIAMETER)):
return True
else:
return False
def isAQuarter(weight, diameter):
'''
Checks to see if the coin is a quarter
weight - the weight of current coin to evaluate
diameter - the diameter of current coin to evaluate
returns True if coin is a quarter
'''
if ((0.97*QUARTER_WEIGHT <= weight and weight<= 1.03*QUARTER_WEIGHT ) and (0.97*QUARTER_DIAMETER <= diameter and diameter <= 1.03*QUARTER_DIAMETER)):
return True
else:
return False
def clasifyCoin(weight, diameter):
'''
Determine what type of coin was received based on the weight and diameter
weight - the weight of current coin to evaluate
diameter - the diameter of current coin to evaluate
returns One of the coin types listed above, or UNKNOWN
'''
if isAQuarter(weight, diameter)==True:
return 3
elif isADime(weight, diameter)==True:
return 2
elif isANickel(weight, diameter)==True:
return 1
elif isAPenny(weight, diameter)==True:
return 0
else:
return 4
#==================================================================
# Everything below here is tester code DO NOT MODIFY
TEST_DATA = [[2.5, 0.75, PENNY],
[5.0, 0.835, NICKEL],
[2.268, 0.705, DIME],
[5.67, 0.955, QUARTER],
[5.67, 0.75, UNKNOWN],
[5.1, 0.83, NICKEL],
[5.7, 0.95, QUARTER],
[2.4, 0.705, UNKNOWN]]
# Test the coin functions
def coinTester():
passed = True
#Penny Tests
weight = TEST_DATA[0][0]
diameter = TEST_DATA[0][1]
if not isAPenny(weight, diameter):
passed = False
print("isAPenny Fail: got False expected True",
"for the values weight =", weight, "and diameter =", diameter)
weight = TEST_DATA[1][0]
diameter = TEST_DATA[1][1]
if isAPenny(weight, diameter):
passed = False
print("isAPenny Fail: got True expected False",
"for the values weight =", weight, "and diameter =", diameter)
#Nickel Tests
weight = TEST_DATA[1][0]
diameter = TEST_DATA[1][1]
if not isANickel(weight, diameter):
passed = False
print("isANickel Fail: got False expected True",
"for the values weight =", weight, "and diameter =", diameter)
weight = TEST_DATA[2][0]
diameter = TEST_DATA[2][1]
if isANickel(weight, diameter):
passed = False
print("isANickel Fail: got True expected False",
"for the values weight =", weight, "and diameter =", diameter)
#Dime tests
weight = TEST_DATA[2][0]
diameter = TEST_DATA[2][1]
if not isADime(weight, diameter):
passed = False
print("isADime Fail: got False expected True",
"for the values weight =", weight, "and diameter =", diameter)
weight = TEST_DATA[3][0]
diameter = TEST_DATA[3][1]
if isADime(weight, diameter):
passed = False
print("isADime Fail: got True expected False",
"for the values weight =", weight, "and diameter =", diameter)
#Quarter Tests
weight = TEST_DATA[3][0]
diameter = TEST_DATA[3][1]
if not isAQuarter(weight, diameter):
passed = False
print("isAQuarter Fail: got False expected True",
"for the values weight =", weight, "and diameter =", diameter)
weight = TEST_DATA[4][0]
diameter = TEST_DATA[4][1]
if isAQuarter(weight, diameter):
passed = False
print("isAQuarter Fail: got True expected False",
"for the values weight =", weight, "and diameter =", diameter)
for testData in TEST_DATA:
weight = testData[0]
diameter = testData[1]
expected = testData[2]
answer = clasifyCoin(weight, diameter)
if answer != expected:
passed = False
print("Fail: got", answer, "expected", expected,
"for the values weight =", weight, "and diameter =", diameter)
if passed:
print("All coin tests passed")
# Only run this code below if this is called as the main, not imported
if __name__ == '__main__':
coinTester()
DT/graphics-ggngv2j4.py
# graphics.py
"""Simple object oriented graphics library
The library is designed to make it very easy for novice programmers to
experiment with computer graphics in an object oriented fashion. It is
written by John Zelle for use with the book "Python Programming: An
Introduction to Computer Science" (Franklin, Beedle & Associates).
LICENSE: This is open-source software released under the terms of the
GPL (http://www.gnu.org/licenses/gpl.html).
PLATFORMS: The package is a wrapper around Tkinter and should run on
any platform where Tkinter is available.
INSTALLATION: Put this file somewhere where Python can see it.
OVERVIEW: There are two kinds of objects in the library. The GraphWin
class implements a window where drawing can be done and various
GraphicsObjects are provided that can be drawn into a GraphWin. As a
simple example, here is a complete program to draw a circle of radius
10 centered in a 100x100 window:
--------------------------------------------------------------------
from graphics import *
def main():
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done
main()
--------------------------------------------------------------------
GraphWin objects support coordinate transformation through the
setCoords method and mouse and keyboard interaction methods.
The library provides the following graphical objects:
Point
Line
Circle
Oval
Rectangle
Polygon
Text
Entry (for text-based input)
Image
Various attributes of graphical objects can be set such as
outline-color, fill-color and line-width. Graphical objects also
support moving and hiding for animation effects.
The library also provides a very simple class for pixel-based image
manipulation, Pixmap. A pixmap can be loaded from a file and displayed
using an Image object. Both getPixel and setPixel methods are provided
for manipulating the image.
DOCUMENTATION: For complete documentation, see Chapter 4 of "Python
Programming: An Introduction to Computer Science" by John Zelle,
published by Franklin, Beedle & Associates. Also see
http://mcsp.wartburg.edu/zelle/python for a quick reference"""
__version__ = "5.0"
# Version 5 8/26/2016
# * update at bottom to fix MacOS issue causing askopenfile() to hang
# * update takes an optional parameter specifying update rate
# * Entry objects get focus when drawn
# * __repr_ for all objects
# * fixed offset problem in window, made canvas borderless
# Version 4.3 4/25/2014
# * Fixed Image getPixel to work with Python 3.4, TK 8.6 (tuple type handling)
# * Added interactive keyboard input (getKey and checkKey) to GraphWin
# * Modified setCoords to cause redraw of current objects, thus
# changing the view. This supports scrolling around via setCoords.
#
# Version 4.2 5/26/2011
# * Modified Image to allow multiple undraws like other GraphicsObjects
# Version 4.1 12/29/2009
# * Merged Pixmap and Image class. Old Pixmap removed, use Image.
# Version 4.0.1 10/08/2009
# * Modified the autoflush on GraphWin to default to True
# * Autoflush check on close, setBackground
# * Fixed getMouse to flush pending clicks at entry
# Version 4.0 08/2009
# * Reverted to non-threaded version. The advantages (robustness,
# efficiency, ability to use with other Tk code, etc.)...