Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The file unsorted_fruits.txt (which is available in the resources section of thi

ID: 3733456 • Letter: T

Question

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:
http://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.
http://youtu.be/JA4neapsdqQ

This site has an example of how to sort a list structure. http://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?

Explanation / Answer

PYTHON CODE:

# opening the input file
inp_file=open(r'C:usersdskdesktopunsorted_fruits.txt')

# opening the output file for writing
out_file=open(r'C:usersdskdesktopsorted_fruits.txt','w')

# reading all the lines from the input file
fruits=inp_file.readlines()

# sorting the list
fruits.sort()

# looping through every fruit in the list
for fruit in fruits:

    # removing the space
    fruit=fruit.strip()

    # if fruit is valid
    if fruit:

        # writing the output to the file
        out_file.write(fruit+' ')

# closing the files
inp_file.close()
out_file.close()

      


PSEUDO CODE:

Pseudo code:

open the input file for reading

open the output file for writing the result

read all the lines and store it in a list called "fruits"

sort the "fruits" list using sort built-in function

FOR every fruit in the "fruits" list
  
   remove the space at the end and beginning of the fruit
  
   if fruit is valid
       write fruit into the output file
ENDFOR

close the input file
close the output file

contents of unsorted_fruits.txt:

papaya
kiwifruit
zapote blanco
huckleberry
banana
fig
lime
xigua
vanilla
yiessas
tamarind
umkolo
quince
apple
imbu
elderberry
juneberry
mango
strawberry
nectarine
date
cherry
orange
watermelon
grape
raspberry


contents of sorted_fruits.txt:

apple
banana
cherry
date
elderberry
fig
grape
huckleberry
imbu
juneberry
kiwifruit
lime
mango
nectarine
orange
papaya
quince
raspberry
strawberry
tamarind
umkolo
vanilla
watermelon
xigua
yiessas
zapote blanco

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote