For this assignment you must create either pseudo code or a


The file unsorted_fruits.txt (which is available in the resources section of this unit in the resources section) contains a list of 26 fruits, each one with a name that begins with a different letter of the alphabet.

Write a program named sort_fruits.py that reads in the fruits from the file unsorted_fruits.txt and writes them out in alphabetical order to a file named sorted_fruits.txt.

For this assignment you must incorporate the use of a list, for loop and / or while loop.

For this assignment you must create either pseudo code or a flowchart of your program and attach it along with your assignment submission. You model must show how your program will read the input file, sort the entries and generate the output file.

In addition, you must submit both your program source code sort_fruits.py as well as the sorted file sorted_fruits.txt with your assignment.

The following sections within the downloadable .pdf version of the textbook contain examples of Python code that you can use to complete this assignment.

  • Section 10 Sorting
  • Section 14.2 Reading and writing files
  • Section 14.4 Filenames and paths

Similar examples can be found in the interactive version of the textbook as follows:

  • Section 10 Lists
  • Section 11.3 Reading Files
  • Section 11.6 Writing Text Files
  • Section 11.2 Finding a File on your Disk 

You can use the following fragment of code the open and read in the data from the unsorted_fruits.txt file. You will need to determine how to use a list structure to sort the items and how to write the data back out into the sorted_fruits.txt file.

infile = open("unsorted_fruits.tex", "r")

outfile=open("sorted_fruits.txt","w")

fruit=infile.read(50)

outfile.write(fruit)

print (fruit)

infile.close()

outfile.close()

In using this code fragment, once you have read a line from the file into the variable text, you must then determine how to load it into a list structure. 

Also keep in mind that you may need to include the directory path when opening files so this may need to be included in your open statement.

The following web site references might come in handy. 

This provides an example of reading and writing text files in python: 

https://www.dreamincode.net/forums/topic/198139-reading-and-writing-to-a-txt-file-in-python/

This video provides an example of reading data from a file and loading it into a list structure and sorting the list. 

https://youtu.be/JA4neapsdqQ

This site has an example of how to sort a list structure. https://effbot.org/zone/python-list.htm

Similar examples can be found in the interactive version of the textbook as follows:

  •   Section 10 Lists
  •   Section 11.3 Reading Files
  •   Section 11.6 Writing Text Files
  •   Section 11.2 Finding a File on your Disk 

Assignment rubric 

  • Does the assignment read the input file, sort the items in the file and write the sorted list in the correct order to the output file (You can determine this by looking at the contents of the output file that should be attached with the submission)?
  • Did the assignment include either pseudo code or a flowchart that outlines the conceptual model of the program detailing its operation and flow?
  • Did the assignment include the use of the python list structure and either the for loop and/or the while loop?
  • Did the assignment include comments and descriptive variables?

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: For this assignment you must create either pseudo code or a
Reference No:- TGS02302783

Now Priced at $30 (50% Discount)

Recommended (91%)

Rated (4.3/5)