Please place your programs in seperate files named as described in the problem. Improperly named programs will not count. Turn in your files by placing them in the dropbox. Please be mindful of the late policy.
Correct | 80% |
Commented | 10% |
Attempted | 10% |
Create a program that turns nested lists into tree objects and vice-versa.
TreeNode
makeTree(nestedList)
that takes
lists of the form [value, childList]
and recursively
creates a tree out of TreeNode
s which it then returns.printTree(root, level)
which prints
out a tree recursively, with level
number of
*
before each entry.dumpToList(root)
that takes
a tree object and returns it converted into a list.Hint: You can multiply a string by an integer to get multiple copies
of a string. '*'*5
is the same as '*****'
You should be able to create a list in the specified format, convert it into a tree, convert that tree into a list, and then convert the list into a tree again. When printed, the trees should look the same.
Include the following test code in your file:
root = MakeTree([1,[[2,[[3,[]],[4,[]]]],[5,[[6,[]],[7,[]]]]]])
asList = DumpToList(root)
PrintTree(root,0)
PrintTree(MakeTree(asList),0)
It should print out the following:
1
* 2
** 3
** 4
* 5
** 6
** 7
1
* 2
** 3
** 4
* 5
** 6
** 7
Modify your program from 4.1:
analyzeSafe.py
TypeError
for when the user gives the wrong
number of arguments.ValueError
for when the user gives something
that is not a number when it should be (i.e., something like 'cow'
instead of 5)savedisk
and loaddisk
. Each one
should expect the user to give the name of the file.
loaddisk
should have its own try...except
to
catch IOError
if the file does not exist.
? range 1-
Bad value
? range 10 11
Wrong number of arguments
? range 10
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? savedisk ten
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
? quit
Goodbye!
######### NEW RUN OF PROGRAM ########
? loaddisk cow
No such file: cow
Result: []
? loaddisk ten
Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]