CS 0002-Python代写-Assignment 8
时间:2022-12-03
Assignment 8
CS 0002 Section 8
Matt Zeidenberg
For this assignment you will be writing six programs which each should be saved independently as
their own ".py" file. Name your files in the manner you have done in previous assignments and
submit them in Brightspace as usual.
If you work with another person on any of these programs, please be sure to credit them in the
comments in the header of the program. Remember to comment your programs well and to put a
header at the top of the program with your name, the course number and section, the date, and the
assignment number and problem number.
Document every function you write with an “input-processing-output” (IPO)-style header. You
should do this for all the functions you write in this class.
Problem #1: Coin Flipping Function (5 points)
Write a function flip that flips a coin. It should take no arguments and return a single character,
either an “H” for heads or a “T” for tails.
To do this, you will need to import the random module using “import random”. Once you have that
imported, you can either use the random.choice method or the random.randint method to
implement the function. The first of these allows you to select randomly from a list, and the second
randomly from a range of numbers.
Grading: Four points correct logic, one point comments
Problem #2: A Function to Flip a Coin Multiple Times (5 points)
Write a function flip_many that flips a coin n times, where n is passed into the argument. The
function should return a list of n “H” and “T” characters. Here are a couple of sample runs (your
results may vary due to randomness):
print(flip_many(3)) # ['H', 'T', 'H']
print(flip_many(7)) # ['H', 'H', 'H', 'T', 'T', 'H', 'T']
Grading: Four points correct logic, one point comments
Problem #3: All Possible Lists (10 points)
Using filp_many with n=3, make and print a list of all the possible lists of three ordered coin flips. It
should be something like this (your result will vary based on randomness):
[['H', 'H', 'T'], ['T', 'H', 'T'], ['T', 'H', 'H'], ['H', 'T', 'T'], ['T',
'T', 'T'], ['H', 'T', 'H'], ['H', 'H', 'H'], ['T', 'T', 'H']]
Then sort the list using the sorted function and print it. You should get the following:
[['H', 'H', 'H'], ['H', 'H', 'T'], ['H', 'T', 'H'], ['H', 'T', 'T'], ['T',
'H', 'H'], ['T', 'H', 'T'], ['T', 'T', 'H'], ['T', 'T', 'T']]
Then use the sorted function to sort each output of flip_many and generate all possible lists of three
coin flips where the order doesn’t matter. The result should have four items. Then sort this list and
print it. You should get the following:
[['H', 'H', 'H'], ['H', 'H', 'T'], ['H', 'T', 'T'], ['T', 'T', 'T']]
Grading: Five points correct logic, three points correct output, two points comments
Problem #4: A Real-Estate Database (10 points)
For this problem, you need to construct four lists that look like the following:
["Studio Condo","Charming Cottage", "Ranch","Tudor-Style Home","Georgian-Style Mansion"]
(call the above list “descriptions”)
[400,800,1200, 2400, 6000] (call this list “square_footage”)
[0,1,2,3,8] (call this list “n_bedrooms”)
[100000,200000,300000,500000,2000000] (call this list “prices”)
This represents five homes for sale. So, for instance, the Studio Condo is 400 square feet, has zero
bedrooms, and costs $100,000, etc.
In your main program, write a loop that collects this information and constructs the four lists above.
The loop should work with any description/square footage/number of bedrooms/price information
but demonstrate it with the four lists above. Exit the loop when the user types “stop” for the
description. Here is how your program should work (user input is underlined):
Enter a home description, enter stop to exit: Studio Condo
Enter the square footage: 400
Enter the number of bedrooms: 0
Enter the price: 100000
Enter a home description, enter stop to exit: Charming Cottage
Enter the square footage: 800
Enter the number of bedrooms: 1
Enter the price: 200000
Enter a home description, enter stop to exit: Ranch
Enter the square footage: 1200
Enter the number of bedrooms: 2
Enter the price: 300000
Enter a home description, enter stop to exit: Tudor-Style Home
Enter the square footage: 2400
Enter the number of bedrooms: 3
Enter the price: 500000
Enter a home description, enter stop to exit: Georgian-Style Mansion
Enter the square footage: 6000
Enter the number of bedrooms: 8
Enter the price: 2000000
Enter a home description, enter stop to exit: stop
The descriptions are:
['Studio Condo', 'Charming Cottage', 'Ranch', 'Tudor-Style Home',
'Georgian-Style Mansion']
The square footages are: [400, 800, 1200, 2400, 6000]
The numbers of bedrooms are: [0, 1, 2, 3, 8]
The prices are: [100000, 200000, 300000, 500000, 2000000]
Grading: Five points correct logic, three points correct output, two points comments
Problem #5: Selecting Properties Using Criteria (10 points)
Simulate searching the real estate database. You can start this program by “wiring in” the four lists
used in the previous problem as follows:
descriptions=["Studio Condo","Charming Cottage",
"Ranch","Tudor-Style Home","Georgian-Style Mansion"]
square_footage=[400,800,1200, 2400, 6000]
n_bedrooms=[0,1,2,3,8]
prices=[100000,200000,300000,500000,2000000]
The searching should work by first asking what you want to search on, and then the minimum and
maximum values for that field, and then output the results. Here are three sample runs. User input
is underlined.
Search by 1-Square Footage 2-Bedrooms 3-Price:1
Enter the minimum square footage: 800
Enter the maximum square footage: 2400
Results:
Description Square Footage Bedrooms Price ($)
Charming Cottage 800 1 200,000
Ranch 1200 2 300,000
Tudor-Style Home 2400 3 500,000
Search by 1-Square Footage 2-Bedrooms 3-Price:2
Enter the minimum number of bedrooms: 0
Enter the maximum number of bedrooms: 2
Results:
Description Square Footage Bedrooms Price ($)
Studio Condo 400 0 100,000
Charming Cottage 800 1 200,000
Ranch 1200 2 300,000
Search by 1-Square Footage 2-Bedrooms 3-Price:3
Enter the minimum price: 300000
Enter the maximum price: 3000000
Results:
Description Square Footage Bedrooms Price ($)
Ranch 1200 2 300,000
Tudor-Style Home 2400 3 500,000
Georgian-Style Mansion 6000 8 2,000,000
Grading: Five points correct logic, three points correct output, two points comments