The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 3 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
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.
Similar examples can be found in the interactive version of the textbook as follows:
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:
Assignment rubricÂ
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly