In: Computer Science
Program Behavior
This program will analyze real estate sales data stored in an input file. Each run of the program should analyze one file of data. The name of the input file should be read from the user.
Here is a sample run of the program as seen in the console window. User input is shown in blue:
Let's analyze those sales!
Enter the name of the file to process? sales.txt
Number of sales: 6
Total: 2176970
Average: 362828
Largest sale: Joseph Miller 610300
Smallest sale: Franklin Smith 199200
Now go sell some more houses!
Your program should conform to the prompts and behavior displayed above. Include blank lines in the output as shown.
Each line of the data file analyzed will contain the buyer's last name, the buyer's first name, and the sale price, in that order and separated by spaces. You can assume that the data file requested will exist and be in the proper format.
The data file analyzed in that example contains this data:
Cochran Daniel 274000
Smith Franklin 199200
Espinoza Miguel 252000
Miller Joseph 610300
Green Cynthia 482370
Nguyen Eric 359100
You can download that sample data file here https://drive.google.com/file/d/1bkW8HAvPtU5lmFAbLAJQfOLS5bFEBwf6/view?usp=sharing to test your program, but your program should process any data file with a similar structure. The input file may be of any length and have any filename. You should test your program with at least one other data file that you make.
Note that the average is printed as an integer, truncating any fractional part.
Your program should include the following functions:
read_data - This function should accept a string parameter representing the input file name to process and return a list containing the data from the file. Each element in the list should itself be a list representing one sale. Each element should contain the first name, last name, and purchase price in that order (note that the first and last names are switched compared to the input file). For the example given above, the list returned by the read_data function would be:
[['Daniel', 'Cochran', 274000], ['Franklin', 'Smith', 199200], ['Miguel', 'Espinoza', 252000], ['Joseph', 'Miller', 610300], ['Cynthia', 'Green', 482370], ['Eric', 'Nguyen', 359100]]
Use a with statement and for statement to process the input file as described in the textbook.
compute_sum - This function should accept the list of sales (produced by the read_data function) as a parameter, and return a sum of all sales represented in the list.
compute_avg - This function should accept the list of sales as a parameter and return the average of all sales represented in the list. Call the compute_sum function to help with this process.
get_largest_sale - This function should accept the list of sales as a parameter and return the entry in the list with the largest sale price. For the example above, the return value would be ['Joseph', 'Miller', 610300].
get_smallest_sale - Like get_largest_sale, but returns the entry with the smallest sale price. For the example above, the return value would be ['Franklin', 'Smith', 199200].
Do NOT attempt to use the built-in functions sum, min, or max in your program. Given the structure of the data, they are not helpful in this situation.
main - This function represents the main program, which reads the file name to process from the user and, with the assistance of the other functions, produces all output. For this project, do not print output in any function other than main.
Other than the definitions of the functions described above, the only code in the module should be the following, at the end of the file:
if __name__ == '__main__':
main()
That if statement keeps your program from being run when it is initially imported into the Web-CAT test environment. But your program will run as normal in Thonny. Note that there are two underscore characters before and after name and main.
Include an appropriate docstring comment below each function header describing the function.
Do NOT use techniques or code libraries that have not been covered in this course.
Include additional hashtag comments to your program as needed to explain specific processing.
A Word about List Access
A list that contains lists as elements operates the same way that any other lists do. Just remember that each element is itself a list. Here's a list containing three elements. Each element is a list containing integers as elements:
my_list = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
So my_list[0] is the list [1, 2, 3, 4] and my_list[2] is [9, 10, 11, 12].
Since my_list[2] is a list, you could use an index to get to a particular value. For instance, my_list[2][1] is the integer value 10 (the second value in the third list in my_list).
In this project, the sales list is a list of lists. When needed, you can access a particular value (first name, last name, or sales price) from a particular element in the sales list.
ef read_data(filename):
dataFromFile=[]
with open(filename,'r') as file:
data=file.readlines()
data=[i.strip()for i in data]
for i in data:
i=i.split(' ')
i[0],i[1]=i[1],i[0]
dataFromFile.append(i)
return dataFromFile
def coumpute_sum(list):
sum=0;
for i in list:
sum=sum+int(i[2])
return sum
def coumpute_avg(listData):
total=coumpute_sum(listData)
length=len(listData)
average=total/length
return round(average)
def get_largest_sale(data):
smallest=int(data[0][2])
index=0
for i in data:
if int(i[2])>smallest:
smallest=int(i[2])
index=i
return index
def get_smallest_sale(data):
smallest=int(data[0][2])
index=0
for i in data:
if int(i[2])<smallest:
smallest=int(i[2])
index=i
return index
def main():
print("Let's analyze those sales!")
print()
filename=input("Enter the name of the file to process? ")
print()
data=read_data(filename)
print("Number of sales: {}".format(len(data)))
print("Total: {}".format(coumpute_sum(data)))
print("Average: {}".format(coumpute_avg(data)))
largest=get_largest_sale(data)
print("Largest sale: {} {} {}".format(largest[0],largest[1],largest[2]))
smallest=get_smallest_sale(data)
print("Smallest sale: {} {} {}".format(smallest[0],smallest[1],smallest[2]))
print()
print("Now go sell some more houses!")
if __name__=='__main__':
main()
Editor:
output:
File: