Question

In: Computer Science

Program Behavior This program will analyze real estate sales data stored in an input file. Each...

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.

Solutions

Expert Solution

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:


Related Solutions

Programming Project 3 Home Sales Program Behavior This program will analyze real estate sales data stored...
Programming Project 3 Home Sales 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...
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
FASB ASC 5-3 Real Estate. Several FASB Statement deal with accounting for real estate sales. Search...
FASB ASC 5-3 Real Estate. Several FASB Statement deal with accounting for real estate sales. Search the FASB ASC database to determine under what conditions profit from real estate sales can be recognized. Cut and paste your findings, and then write a summary of what you found.   Financial Accounting Theory And Analysis Text and Cases 12th Edition.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT