$23.99
Purpose: This homework will require you to understand each problem and use sequence, selection, and iterative (repetitive) control structures in your solutions as applicable.
This is not a collaborative assignment; you must design, implement and test the solution(s) on your own. You may not consult or discuss the solution with anyone other than the course instructor or TAs. In addition, you may not include solutions or portions of solutions obtained from any source other than those provided in class, Moodle, or this assignment (i.e, do not refer to any internet sources other than those specified below, in class, or on Moodle). Obtaining or sharing solutions to any homework problems for this class is considered academic misconduct. If you are not sure what this means, consult the class syllabus or discuss it with the course instructor.
The total point value for programming assignments will be awarded for solutions that are complete, correct, and well-constructed. A "well-constructed" program entails good design, appropriate comments and general readability (descriptive names for variables and procedures, appropriate use of blank space, etc.). The following will result in a score reduction equal to a percentage of the total possible points:
● Incorrectly named source file (100%)
● Program Construction (up to 25%)
● Constraints not followed (up to 100%)
● Failure to execute due to syntax errors (75%)
Note that your work will be graded using, and must function correctly with the current version of Python
3 on CSE Labs UNIX machines. If you complete your programming assignment using a different system,
it is your responsibility to ensure your programs work on CSE Labs machines prior to submitting them. Finally, this assignment contains 3 problems (A, B, and C) worth a total of 50 points, and is 7 pages long.
A: (15 Points) Body Mass Index Calculation
Body Mass Index (BMI) is a calculation used by professionals in the medical field to help determine if their patients are at a healthy weight.
BMI is a number based on your weight in kilograms and height in meters. It is calculated by the following formula:
Below are the four standard weight status categories associated with the BMI ranges:
BMI ranges Weight Status
Below 18.5 Underweight
18.5 – 24.9 Normal
25.0 – 29.9 Overweight
30.0 and Above Obese
In this problem, you will write a Python 3 program that calculates BMI and weight status. You should define and use two “pure” functions in your program: get_BMI and get_BMI_status.
get_BMI receives inputs W and H and returns the calculated BMI.
get_BMI_status receives a BMI value as input and returns the associated weight status.
Recall that a “pure” function does not include 'side effects' such as obtaining input from the keyboard printing. Thus, code to prompt the user for input or print output should not be present inside of the functions specified above. Please ensure that your solution conforms to the constraints specified below.
Constraints:
1. Define and use two pure functions named get_BMI() and get_BMI_status() as described above and demonstrated in the example output below.
2. Include comments before each function that describe the functionality of, input to, and outputs of, the defined function.
Here is an example:
#
# This function is to calculate the BMI based on the weight W and height H with the formula:
# BMI = W / H**2.
# Input: weight W in kilograms, height H in meters
# Output: BMI
#
3. The BMI value your program prints should be rounded to one significant digit (one number to the right of the decimal point)
Below are two examples of how your program's input and output should look (user input in bold font): Example 1:
Enter body weight, in kilograms: 65
Enter body height, in meters: 1.75
The BMI is: 21.2
The corresponding weight status is: Normal
Example 2:
Enter body weight, in kilograms: 80
Enter body height, in meters: 1.7
The BMI is: 27.7
The corresponding weight status is: Overweight
Remember to test your program thoroughly on a variety of values other than those included in the examples.
B. (15 points) Calculating the Rate of Zombie Transmission
According to Prof Robert J Smith’s book, “Mathematical Modeling of Zombies,” University of Ottawa Press, 2014, the rate of zombie transmission, from a walking dead individual to a non-zombie individual, can be calculated by the formula:
where:
is the likelihood of transmission is the total population
is the number of susceptible people
is the initial number of zombies (calculated from a percent of the total population)
In this problem, you will write a Python 3 program to calculate the rate of zombie transmission, specified in the formula above. Please ensure your program conforms to the following constraints.
Constraints
➢ b, the likelihood of transmission, is a percent that is calculated from an input number between 1 and 100, inclusive (so you will need to divide the number a user enters for b by 100 to make it a percent value from 0 to 1)
➢ N, the total population, is a number between 451 and 1384688986 (inclusive, and which represents an estimate of the population of the smallest and largest countries as obtained from the following sites:
https://www.worldatlas.com/articles/least-populated-country-in-the-world.html https://www.census.gov/popclock/print.php?component=counter)
➢ S, the number of susceptible people, corresponds to a number of people between 10 and 90 percent (inclusive) of the total population N. As shown in the example output below, rather than directly accepting a value for S, you will get the corresponding percentage percentS from the user (so you will need to divide the number a user enters for percentS by 100 to make it a percent value from 0 to 1, and multiply the result by N to get the value S used in the formula above)
➢ Z, the initial number of zombies, corresponds to a number of people between 0.25% and 2% (inclusive) of the total population N. As shown in the example output below, rather than directly accepting a value for Z, you will get the corresponding percentage percentZ from the user (so, you will need to divide the number a user enters for percentZ by 100 to make it a percent value from 0 to 1, and multiply the result by N to get the value Z used in the formula above)
➢ You should define and use a pure function that takes parameters named b1, S1, and Z1 and returns the rate of Zombie transmission computed from these values
➢ Your program should check the inputs b, N, percentS, and percentZ to make sure they are within the allowable ranges specified above. If one or more of the values for b, N,
percentS, and percentZ are not in the allowable range, your program should print an error message.
➢ Your program should contain a continuation loop that allows the user to compute another rate of transmission.
➢ The rate of zombie transmission should be rounded to two digits after the decimal point.
➢ Document the function you define with comments -- include a description of what the function does, any input parameters to the function, any return value or values from the function, and any other input or output carried out by the function.
Example (user input is in bold font):
Enter the % likelihood of transmission (1 <= b <= 100): 1
Enter the total population: (451 <= N <= 1384688986): 50000
Enter the % of population that is susceptible to infection (10 <= percentS <= 90): 10
Enter initial % of population that spontaneously turns into zombies: (.25 <= percentZ
<= 2): 1
b is: 0.01
S is: 5000.0
Z is: 500.0
The rate of zombie transmission is: 25000.00
Continue ('Y' or 'y'): y
Enter the % likelihood of transmission (1 <= b <= 100): 10
Enter the total population: (451 <= N <= 1384688986): 100000
Enter the % of population that is susceptible to infection (10 <= percentS <= 90): 5
Enter initial % of population that spontaneously turns into zombies: (.25 <= percentZ
<= 2): 1
You entered an illegal input value! Continue ('Y' or 'y'): Y
Enter the % likelihood of transmission (1 <= b <= 100): 10
Enter the total population: (451 <= N <= 1384688986): 400
Enter the % of population that is susceptible to infection (10 <= percentS <= 90): 20
Enter initial % of population that spontaneously turns into zombies: (.25 <= percentZ
<= 2): .5
You entered an illegal input value! Continue ('Y' or 'y'): y
Enter the % likelihood of transmission (1 <= b <= 100): 10
Enter the total population: (451 <= N <= 1384688986): 500
Enter the % of population that is susceptible to infection (10 <= percentS <= 90): 20
Enter initial % of population that spontaneously turns into zombies: (.25 <= percentZ
<= 2): .5
b is: 0.1
S is: 100.0
Z is: 2.5
The rate of zombie transmission is: 25.00
Continue ('Y' or 'y'): n
Finally, remember to test your program thoroughly on a variety of values other than those included above
(both valid and invalid cases).
C. (20 points) Drawing Patterns in Turtle Graphics
Create a Python program that uses turtle graphics commands to draw figures similar to the three figures pictured in the example output below (individual triangles forming a rectangle pattern, star
pattern, or a star pattern enclosed within a rectangle pattern).
Constraints
➢ Your program should define and use three procedures: drawTriangle, drawRectanglePattern, and drawStarPattern
➢ drawTriangle should accept at least one value, the length of a side of the triangle, and should draw an equilateral triangle.
➢ drawRectanglePattern should call the procedure drawTriangle to draw a triangle at each vertex of the rectangle. You may choose any fixed side lengths for the rectangle itself, but the images you produce should look similar to those in the example output below.
➢ drawStarPattern should call the procedure drawTriangle to draw a triangle at each of the vertices of a 5-pointed star. You may choose any fixed side lengths for the star itself, but the images you produce should look similar to those in the example output below.
➢ If both a star pattern and rectangle pattern are drawn, the star pattern should be enclosed within the rectangle pattern.
➢ For any pattern, the triangle side length should range between 25 and 50 (25 <= side <= 50), inclusive, or an error should be printed and the default value 25 should be used as the length of the side of a triangle.
➢ Your program should print an error if a user specifies a command that is not recognized as input.
➢ For this problem only, you can treat the turtle as a global variable -- though it is better programming practice to pass the turtle as a parameter to your procedures.
Below are examples of how your program's input and output should look (user input in bold font):
Example 1:
Enter length of side: 10
Error: 10 is less than 25 or greater than 50, using default value of 25
Enter drawing pattern: r or R for rectangle, s or S for star, b or B for both: X
Error: pattern: X not recognized!
Example 2:
Enter length of side: 25
Enter drawing pattern: r or R for rectangle, s or S for star, b or B for
both: r
Turtle Canvas:
Example 3:
Enter length of side: 50
Enter drawing pattern: r or R for rectangle, s or S for star, b or B for both: S
Turtle Canvas:
Example 4:
Enter length of side: 50
Enter drawing pattern: r or R for rectangle, s or S for star, b or B for both: B
Turtle Canvas
Hints:
See https://docs.python.org/3/library/turtle.html for an introduction to turtle graphics and a comprehensive list of turtle graphics commands.
You may find the turtle methods penup, pendown, setpos, and color useful. Remember to import the turtle module (refer to Lab 2, or the link above).
Submit your Homework Problems via Moodle
You must follow the correct naming conventions for each of the programs, or you will not receive credit for your homework.
When you are done with each of the problems, submit the files containing the source code via the appropriate link in Moodle as follows:
For Problem A, name the source code file <username_2A.py. For Problem B, name the source code file <username_2B.py. For Problem C, name the source code file <username_2C.py.
For each problem, replace <username with your U of M email address (in a similar fashion to HW 1). For example, if your email address is smithx1234@umn.edu, for Problem A, your file should be
named smithx1234_2A.py. Then submit your program using the HW 2 Problem A submission link
in Moodle. There will be a similar link on Moodle for programming Homework problems 2B, and 2C.
Make sure that each of your programs runs correctly on the CSE Lab machines or you may not receive credit.
Also, make sure to submit something for each problem before the deadline so you get some points. Some points are better than no points, and a program that runs without errors and does something related to the homework problem will receive more credit than a program that does not run without errors.
Remember that you can always re-submit a newer, better version of your program via Moodle before the deadline, even if you have already submitted something.
Finally - Have fun! Solving computational problems and writing code can be difficult and frustrating at times, but it can also be very fun and satisfying. Learning to manage frustration is part of becoming a computer scientist. Take a break if you get stuck. I like to take a walk or read a newspaper online!