Question

In: Computer Science

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

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 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

Code screenshot:

sales.py

def read_data(file_name):
   sales = []
   with open(file_name) as file:
       # For each line
       for record in file.read().split('\n'):
           # Split each line wit respect to white space
           record = record.split()
           # Switch first and last name
           record[0],record[1] = record[1],record[0]
           # Append record to sales
           sales.append(record)
   # Return sales
   return sales

def compute_sum(sales):
   # Initial sum is 0
   sum = 0
   # For each record in sale
   for record in sales:
       # Add sales to sum
       sum += int(record[2])
   # Return sum
   return sum

def compute_avg(sales):
   # Average = sum/number of sales
   return compute_sum(sales)//len(sales)

def get_largest_sale(sales):
   # Assume first record as largest sale
   max_sale = sales[0]
   # For each record in sales
   for record in sales:
       # If current record has largest sale than current large sale
       if record[2]>max_sale[2]:
           # update max_sale
           max_sale = record
   # Return the result
   return max_sale[0]+' '+max_sale[1]+' '+max_sale[2]

def get_smallest_sale(sales):
   # Assume first record as smallest sale
   min_sale = sales[0]
   # For each record in sales
   for record in sales:
       # If current record has largest sale than current small sale
       if record[2]<min_sale[2]:
           # update min_sale
           min_sale = record
   # Return the result
   return min_sale[0]+' '+min_sale[1]+' '+min_sale[2]

def main():
   # Funtion calling and printing result to console
   sales = read_data('data.txt')
   print('Number of sales: ',len(sales))
   print('Total: ',compute_sum(sales))
   print('Average: ',compute_avg(sales))
   print('Largest sale: ',get_largest_sale(sales))
   print('Smallest sale: ',get_smallest_sale(sales))

if __name__ == '__main__':
   main()

output screenshot:


Related Solutions

(1) These are the sales prices of recent home sales at a real estate office: $475,000...
(1) These are the sales prices of recent home sales at a real estate office: $475,000 $310,000   $350,000    $400,000   $475,000   $1,000,000   $390,000 Find the x̄, median and the mode for the prices. In your opinion, which is the best measure of central tendency for this data set? Why? (2) Identify the population, parameter, sample, and statistic in the statistical study quoted below. What type of data was gathered in the study? The United Hospital Fund recently examined the cases of...
A real estate agency says that the mean home sales price in City A is the...
A real estate agency says that the mean home sales price in City A is the same as in City B. The mean home sales price for 30 homes in City A is ​$127,402. Assume the population standard deviation is ​$25,880. The mean home sales price for 30 homes in City B is ​$112, 276. Assume the population standard deviation is ​$27,112. At alpha =0.01​, is there enough evidence to reject the​ agency's claim? Find the critical​ value(s) and identify...
You are the real estate sales agent for the Martins who are selling a home to...
You are the real estate sales agent for the Martins who are selling a home to the Howells. The Martins signed a purchase agreement with the following personal property stipulated: The tools in the garage, and the refrigerator, besides the paintings in the living room, will remain the property of the seller. The signed contract has just been received by the Martins and they call you as their sales agent to add the bookcase in the master bedroom as another...
A real estate agency says that the mean home sales price in City A is the...
A real estate agency says that the mean home sales price in City A is the same as in City B. The mean home sales price for 20 homes in City A is ​$127 comma 462. Assume the population standard deviation is ​$25 comma 879. The mean home sales price for 20 homes in City B is ​$112 comma 289. Assume the population standard deviation is ​$27 comma 112. At alphaequals0.01​, is there enough evidence to reject the​ agency's claim?...
Scarborough Sales, a real estate company specializing in apartment rentals and home sales, is having difficulty...
Scarborough Sales, a real estate company specializing in apartment rentals and home sales, is having difficulty in gathering appropriate cost information for evaluating its operations. It owns several large apartment complexes and sells homes owned by builders or existing homeowners. As the company's new accountant, you define cost by major activity. You use this information for allocating costs to cost objects. In addition, cost pools are created for appropriate cost allocations. The owner of the company is interested in exactly...
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.  
I need critical log on real estate project, means survey about real estate project.
I need critical log on real estate project, means survey about real estate project.
A real estate major collected information on some recent local home sales. The first 6 lines...
A real estate major collected information on some recent local home sales. The first 6 lines of the database appear below. The columns correspond to the house identification number, the community name, the ZIP code, the number of acres of the property, the year the house was built, the market value, and the size of the living area (in square feet). House_ID Neighborhood Mail_ZIP Acres Yr_Built Full_Market_Value Size 41340053 Greenfield Manor 12859 1.00 1967 $1,00,400 960 4128001474 Fort Amherst 12801...
A real estate developer wishes to study the relationship between the size of home a client...
A real estate developer wishes to study the relationship between the size of home a client will purchase (in square feet) and other variables. Possible independent variables include the family income, family size, whether there is a senior adult parent living with the family (1 for yes, 0 for no), and the total years of education beyond high school for the husband and wife. The sample information is reported below. Family Square Feet Income (000s) Family Size Senior Parent Education...
A real estate developer wishes to study the relationship between the size of home a client...
A real estate developer wishes to study the relationship between the size of home a client will purchase (in square feet) and other variables. Possible independent variables include the family income, family size, whether there is a senior adult parent living with the family (1 for yes, 0 for no), and the total years of education beyond high school for the husband and wife. The sample information is reported below. Family Square Feet Income (000s) Family Size Senior Parent Education...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT