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

A programming genius called Hank Wrote a system to \'access\' his \'bank\' When

ID: 3678157 • Letter: A

Question

A programming genius called Hank
Wrote a system to 'access' his 'bank'
When his memory failed him
They nailed him then jailed him
Now his 'storage' is 'basic' and dank.

Lab Question 3 (Sorting Files): Write a function sortFile (src, dst) that sorts file with name src line by line in lexicographic order and writes the result to file with name dst without modifying src. Place the function in the file Q3_yourmacid.py. Hints: If f is a file object, list (f) reads the whole file as a list of lines. Use the sort method of lists for sorting and the join method of strings for concatenating a list of strings. For example sorting limerick.txt (from the web site) would result in a file with: A programming genius called Hank Now his 'storage' is 'basic' and dank. They nlled him then jailed him When his memory failed him Wrote a system to 'access' his 'bank'

Explanation / Answer

python program:

#!/usr/bin/python

# Function definition is here
def sortFile(src,dst ):
fo = open(src,'r')
list1=list(fo)
list1.sort();
fos = open(dst, 'wb+')
str=" ".join(list1)
print str
fos.write(str)
fo.close()
fos.close();
return;
#function calling is here
sortFile("foo.txt","foos.txt")

output:

A programming genius called Hank

Now his 'storage' is 'basic' and dank.
They nailed him then jailed him

When his memory failed him

Wrote a system to 'access' his 'bank'

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