< Back

Test #3


If you do not currently have a directory for tests, create one in your cs170 directory, and also create a directory for test3

$ mkdir ~/cs170/tests
$ mkdir ~/cs170/tests/test3
$ cd ~/cs170/tests/test3

This portion of the test is worth 30 total points. You may only access the Python documentation website. NO OTHER WEBSITES ARE PERMITTED. You are also not allowed to access any files other than those specified below. You should also follow all coding standards discussed thus far.


Question 8

Download the linked_list.py file into your directory. Add a method called double_all, which creates a duplicate node for every node in the linked list, and inserts it immediately after the original node.

>>> my_linked_list = LinkedList()
>>> my_linked_list.double_all()
>>> print(my_linked_list)
head -> None
>>> my_linked_list.append("Hello")
>>> my_linked_list.double_all()
>>> print(my_linked_list)
head -> "Hello" -> "Hello" -> None
>>> my_linked_list.append("World")
>>> my_linked_list.double_all()
>>> print(my_linked_list)
head -> "Hello" -> "Hello" -> "Hello" -> "Hello" -> "World" -> "World" -> None

(15 points)


Question 9

In a file called question_9.py, create a class called ListIterator. This class will be used to "iterate" over a linked list. Your class should store attributes for the linked list being iterated over, as well as the "current" node.

This class should also contain methods called get_next, and has_next. The first method should return THE DATA from the current node, and move the current reference to the next node in the list. has_next should return a boolean value, on whether or not there is any more data in the linked list. Keep in mind, if there are no more elements in the list, your get_next method should not crash. It should print an error message, and return None

>>> my_linked_list = LinkedList()
>>> my_linked_list.append("Hello")
>>> my_linked_list.append("World")
>>> my_linked_list.append("!")
>>> print(my_linked_list)
head -> "Hello" -> "World" -> "!" -> None
>>> my_iterator = ListIterator(my_linked_list)
>>> print(my_iterator.has_next())
True
>>> print(my_iterator.get_next())
"Hello"
>>> print(my_iterator.get_next())
"World"
>>> print(my_iterator.get_next())
"!"
>>> print(my_iterator.has_next())
False
>>> print(my_iterator.get_next())
None

(15 points)

 

Challenge

In a file called list_coloring.py, create a function called color_list(list_size, colors), which takes an integer representing the size of an \(n \times n\) 2-dimensional list, and an integer representing a number of colors. Your function should return True if the given list can be "colored" using the specified number of colors. A graph is considered colored if every location in the list has a different number than its cardinal neighbors. For example:

	  [[0, 1],
	  [1, 0]]
	
Is colored using only 2 colors, because each location has a different value than its neighbors. However:
	  [[0, 1],
	  [1, 1]]
	
is not colored, because location \((1, 1)\) has the same color as \((0, 1)\) and \((1, 0)\).

Your program should return a valid coloring of the list.

(8 points)


Submission

When you have finished, create a tar file of your