Guest TA: David Zlotchenko (zlotchen@cs.utk.edu).
Background
In order to complete this lab, you will find it necessary to read a file
into memory. Python makes this rather easy.
Python supports a file object which is described in the Python
reference manual
in section 2.1.7. File objects are implemented using C's stdio package.
Perhaps the most useful commands are readline() and
readlines(), which returns
the next line in a file, as a string, and all the lines in a file, placed
in a list, respectively. For example:
>>> my_file = open('/home/cs494/notes/Python/roster', 'r')
>>> line = my_file.readline()
>>> line
'Little Leonard 6.3 DE Jr Tennessee Asheville, NC\012'
Note that the newline character is included in the string.
Standard Input
The stdin file object may be found in sys.stdin. For
example:
>>> a = sys.stdin.readline()
reads the next line of standard input into a.
Exercises
Write the following functions in Python:
- strip_new_line(list): This function should take a list
of strings that you have read in from a file and strip the new
line character from each string. The function should return the
strings in the same list object that was passed in. To test your
function, you should read the file
/home/cs494/notes/Python/roster into a list and
then pass the list to your function.
Hint: (1) use
Python's slice notation to get rid of the new line characters.
(2) use readlines() to read the entire file into a list.
- quicksort(a): This function should take a list of strings
and sort them using the quicksort algorithm. The first 20 characters
of the string should be used as the key on which to sort. To test
your function, you should sort the roster list that you created
with strip_new_line. Hint: When passing the list
to your recursive function, only pass the relevant sublist--do not
pass the entire list.
- binary_search(a, key): This function should take a sorted
list of strings and search for the key. If it finds a string with
the designated key, it should print out the entire string. Otherwise
it should print the key and a message indicating the key could not
be found. You should assume that the first 20 characters of a string
are the string's key.
A sample session using the sorted roster list might
look as follows:
>>> binary_search(roster, 'Copeland Jermaine')
Copeland Jermaine 6.4 QB So Tennessee Harriman, TN
>>> binary_search(roster, 'Vander Zanden Brad')
The key 'Vander Zanden Brad' could not be found
Hint: You should use a while loop with an else clause
attached to the end of it.
If You Want to Learn More
This last exercise gives you some experience with Python's class mechanism,
although only to use it as a record structure. Write the following two
functions to construct and search an unbalanced, binary search tree:
- insert(root, element): This function inserts a string into
the tree in alphabetical order. The key is the first 20 characters
of the string.
- search(root, key): This function should search for the key
in the binary search tree. If it finds a string with
the designated key, it should print out the entire string. Otherwise
it should print the key and a message indicating the key could not
be found. The output of this function should be similar to the output
of the binary_search function described above. You should assume that
the first 20 characters of the string are the string's key.
In order to complete this exercise, you will need to be able to create
a node that has at least three fields: a left child link, a right child
link, and a data field. Although you could use a list or a tuple to
represent a node, the best way is to represent a node as a class and create
instances of this class.