Menu

[Solved]-Python Please Confused Start 125 Lab Street Survey Lab Street Survey Lab Write Program Rea Q37279619

in Python please. just confused on how tostart

12.5 Lab: Street Survey

Lab: Street Survey

In this lab, you will write a program to read an input filecontaining the names of all the streets in a local city, countingup how many instances of “Drives”, “Lanes”, “Circles”, “Courts”,”Roads”, etc. there are.

Once the data is collected, it will be processed to compute thepercentage that each street “type” contributes to the total. Thisdata will be sorted (descending) and written to a report file. Notethat only street types with more than 5 instances will be countedindividually; any types with 5 or fewer instances will be lumpedinto a final “OTHER” category.

The Data Files

Data files are provided for the cities of Roseville and CitrusHeights. The beginning of the Roseville file(roseville-streets.txt) looks like this:

15 Mile DrAbbey LnAbby Gate DrAbby LnAberdeen CirAberdeen CtAcacia CtAcklan AlAckleton Way…

Starter Code

Starter code is provided to give your program structure.

The required coding is indicated with # TODO (N): comments. Yourjob is to implement each of the functions, where indicated.

def input_file():

  • prompt user for filename

def basename(infilename):

  • return file name with the “dot-suffix” removed

def make_title(basename):

  • return the given string transformed to title case

def output_file(basename):

  • use the given basename to generate the output file name

def count_streets(filename):

  • open the given file and read in each line
  • return dictionary containing street type counts

def compute_breakdown(streets):

  • process street dictionary to generate a sorted list oftuples
  • return total count and sorted list of tuples

def write_report(total, data, basename):

  • write the report to the output file

Sample runs:

  • Note that entering the name of a file that does not existshould write an error message and then exit the program:

Enter filename> unknown.txtNo such file: “unknown.txt”

  • Entering the name of a street file should result in an outputfile being written:

Enter filename> roseville-streets.txtReport written to: report-roseville-streets.txt

  • The beginning of the report file should look like this:

Roseville Streets—————– 928: Ct (40.02%) 410: Dr (17.68%) …

Hints

1) Use os.path.isfile() to determine if the fileexists.

2) Use the with open(…) as … form of statement, to openand process the input / output files.

3) Use a dictionary with the “street types” as the keys, andinteger counts as the values, to collect the data.

4) When computing the “breakdown” data, don’t forget thatonly those street types with more than 5 instancesshould be added to the data as individual counts; all others shouldbe lumped together in a single, final count of “OTHER”.

5) When opening the output report file, don’t forget tospecify the file mode of ‘w’, for write.

#=== streetsurvey.py

import os.path

# the format string for results written to the output reportfile
fmt_out_line = ‘{:>6}: {:<6} ({:.2f}%)n’

def input_file():
    # TODO (1): prompt user for filename
   #           iffile doesn’t exist, return None
   #          otherwise, return the entered filename
    return None

def basename(infilename):
    # TODO (2): return file name with the”dot-suffix” removed
   #           -e.g. ‘myfile.txt’ –> ‘myfile’
    return ”

def make_title(basename):
    # TODO (3): return the given string transformedto title case:
   #           -replace all dashes with spaces
   #           – usethe string function `title()` to convert to title case
    return ”

def output_file(basename):
    # TODO (4): use the given basename to generatethe output file name
   #           -e.g. ‘myfile’ –> ‘report-myfile.txt’
    return ”

def count_streets(filename):
    streets = {}

    # TODO (5): open the given file and read ineach line
   #           -extract the last “word” of the line (‘Dr’, ‘Ln’, ‘Ct’, etc.)
   #           – adda new entry (value = 1), if not yet in the dictionary
   #           -increment entry’s value, if in the dictionary already

    # with …

    return streets

def compute_breakdown(streets):
    data = []
    other = 0
    total = 0

    # TODO (6): process street dictionary togenerate a sorted list of tuples
   #           – foreach key:
   #              – get the value
   #              – add value to `total`
   #              – if value > 5, add a tuple to data: (value, key)
   #              – otherwise, add value to `other`
   #           -sort the list of tuples in descending order
   #           -append a final tuple to data: (other, ‘OTHER’)
   #           -return the results

    return (total, data)

def write_report(total, data, basename):
    outfilename = output_file(basename)
    title = make_title(basename)
    print(‘Report written to:{}’.format(outfilename))

    # TODO (7): write the report to the outputfile
   #           -open the file for write
   #           -write the title (and a newline)
   #           -write a line of dashes as long as the title (and a newline)
   #           – foreach tuple in the data list:
   #              – compute the percentage (count / total * 100)
   #              – write out the data using `fmt_out_line` for formatting
   #           -write a line of dashes as long as the title (and a newline)

    # with …

# Program main entry point (Should not be modified)
def main():
    infilename = input_file()
    if infilename != None:
        street_data =count_streets(infilename)
        total, cooked_data =compute_breakdown(street_data)
        write_report(total,cooked_data, basename(infilename))

# Required to run the program when invoked from the PythonInterpreter
if __name__ == ‘__main__’:
    main()

Expert Answer


Answer to in Python please. just confused on how to start 12.5 Lab: Street Survey Lab: Street Survey In this lab, you will write a… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *