They form a circle and a number n is picked from a hat. One of their names is also picked from a hat. Beginning with the soldier whose name is picked, they begin to count clockwise around the circle. When the count reaches n, that soldier is executed, and the count begins again with the next man. The process continues so that each time the count reaches n, a man is removed from the circle. Once a soldier is removed from the circle he is no longer counted. The last soldier remaining was supposed to take his own life. According to legend the last soldier remaining was Josephus and instead of taking his own life he joined the enemy forces and survived.
The problem is: given a number n, the ordering of the men in the circle, and the man from whom the count begins, to determine the order in which the men are eliminated from the circle and which man escapes. For example, suppose that n equals 3 and there are five men named A, B, C, D, and E. We count three men, starting at A, so that C is eliminated first. We then begin at D and count D, E, and back to A, so that A is eliminated next. Then we count B, D, and E (C has already been eliminated) and finally B, D, and B, so that D is the man who escapes.
You will use a circular linked list. You have worked on the linear linked list in your previous home work. To make a circular linked list you need to make the next field in the last link of the linked list point back to the first link instead of being null. From any point in a circular list it is possible to reach any other point in the list. Thus any link can be the first or last link. One useful convention is to let the external pointer to the circular list point to the last link and to allow the following link be the first link. We also have the convention that a null pointer represents an empty circular list.
Instead of giving the soldiers names you will assign them numbers serially starting from 1 (one). This way you can use the Link class that we discussed. In your program you will read the data from a file called josephus.txt. The first line gives the number of soldiers. The second line gives the soldier from where the counting starts. The third line gives the elimination number.
You will create a circular linked list having the number of soldier specified. Your program will print out the order in which the soldiers get eliminated. The template for your program will look like this:
class Link(object): class CircularList(object): # Constructor def __init__ ( self ): # Insert an element in the list def insert ( self, item ): # Find the link with the given key def find ( self, key ): # Delete a link with a given key def delete ( self, key ): # Delete the nth link starting from the Link start # Return the next link from the deleted Link def deleteAfter ( self, start, n ): # Return a string representation of a Circular List def __str__ ( self ): def main(): main()
You will modify the functions insert(), find(), and delete() from the LinkedList class. The main workhorse of the program will be the deleteAfter() method. This takes the starting Link and the elimination number n and deletes the nth Link from the starting Link. It returns the next Link after the Link that was deleted.
The file that you will be turning in will be called Josephus.py. The file will have a header of the following form:
# File: Josephus.py # Description: # Student Name: # Student UT EID: # Course Name: CS 313E # Unique Number: 53260 # Date Created: # Date Last Modified:
Use the turnin program to submit your Josephus.py file. The proctor should receive your work by 11 PM on Friday, 22 Mar 2013. There will be substantial penalties if you do not adhere to the guidelines.