Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Josephus Problem please write in Python There is a group of soldiers surrounded by an overwhelming enemy force. There is no hope for victory without

Josephus Problem please write in Python

There is a group of soldiers surrounded by an overwhelming enemy force. There is no hope for victory without reinforcements, so they make a pact to commit suicide.

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.

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.

josephus.txt : 40

1

3

Code(Please write in Python, the most important part is the def deleteAfter function):

class Link(object): def __init__(self, data, n = None): self.data = data self.next = n def get_next(self): return self.next def set_next(self, n): self.next = n def get_data(self): return self.data def set_data(self): self.data = d def __str__(self): return str(self.data) class CircularList(object): def __init__(self): self.head = None self.end = None self.size = 0 def insert(self, item): temp = Link(item) if self.head == None: temp.set_next(self.head) self.head = temp #self.end = self.head self.size = self.size + 1 temp.set_next(self.head) self.head = temp self.size = self.size + 1 def delete(self, key): if key < 0 | key > size: print("Invalid Key") return elif key == 0: if self.head == None: print("Empty List") return head = head.get_next() return elif (key == self.size): temp1 = self.head temp2 = self.head count = 1 while count < self.size - 1: temp3 = temp1.get_next() count = count + 1 temp3.set_next(temp3.get_next().get_next()) def find(self, key): if (self.head == None): return "Empty List" current = self.head count = 1 while count < self.size: if count == key: print("value is", current.get_data) return current = current.get_next() count = count + 1 # Delete the nth link starting from the Link start # Return the next link from the deleted Link def deleteAfter(self, start, n): def __str__(self): if self.head == None: return "Empty List" count = 1 current = self.head out = str(current.get_data()) + " " while count < self.size - 1: current = current.get_next() out += str(current) + " " count = count + 1 return out def main(): main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Differentiate the function. r(z) = 2-8 - 21/2 r'(z) =

Answered: 1 week ago