Menu

[solved]-Class Node Def Init Self Value Next Selfvalue Value Selfnext Next Def Str Self Return Str Q39062399

6.4 Josephus Problem In the Josephus problem from antiquity, N people are in dire straits and agree to the following strategy

class Node:
def __init__(self, value, next):
self._value = value
self._next = next
  
def __str__(self):
return ‘[ ‘ + str(self._value) + ‘ ]’
  
def getNext(self):
return self._next
  
def setNext(self, n):
self._next = n
  
def getValue(self):
return self._value
  
def setValue(self, v):
self._value = v

#You MUST implement using your node class
class Queue:
def __init__(self):
self._head = None
self._tail = None
  
def __str__(self):
if self._head is None:
return ‘Queue Empty’
  
start = self._head
result = []
while start is not None:
result.append(str(start.getValue()))
start = start._next
return “[ 1 ]”
  
  
def front(self):
if self.empty():
raise Exception(‘Queue is empty’)
return self._head.getValue()
  
  
def empty(self):
if self._head == None:
return True
else:
return False
  
def enqueue(self, x):
x = Node(x, None)
  
if self.empty():
self._head = x
self._tail = x
else:
self._tail.setNext(x)
self._tail = x
  
def dequeue(self):
if self.empty():
raise Exception(‘Queue is empty’)
x = self._head
self._head = self._head.getNext()
return x.getValue()

6.4 Josephus Problem In the Josephus problem from antiquity, N people are in dire straits and agree to the following strategy to reduce the population. They arrange themselves in a circle (at positions numbered from 0 to N-1) and proceed around the circle, eliminating every Mth person until only one person is left. Legend has it that Josephus figured out where to sit to avoid being eliminated. Use your Queue Class to solve this problem. Write a python program that takes two input N and M. Your program should print out the order in which people are eliminated (and thus would show Josephus where to sit in the circle) You must use your Queue class from the previous problem to solve this problem. The following is an example execution The Josephus Problem Enter the Number of people in the Group: Enter value for Mth Person to be killed: Order in which people are killed: 2 5 1 6 4 0 3 Show transcribed image text 6.4 Josephus Problem In the Josephus problem from antiquity, N people are in dire straits and agree to the following strategy to reduce the population. They arrange themselves in a circle (at positions numbered from 0 to N-1) and proceed around the circle, eliminating every Mth person until only one person is left. Legend has it that Josephus figured out where to sit to avoid being eliminated. Use your Queue Class to solve this problem. Write a python program that takes two input N and M. Your program should print out the order in which people are eliminated (and thus would show Josephus where to sit in the circle) You must use your Queue class from the previous problem to solve this problem. The following is an example execution The Josephus Problem Enter the Number of people in the Group: Enter value for Mth Person to be killed: Order in which people are killed: 2 5 1 6 4 0 3

Expert Answer


Answer to class Node: def __init__(self, value, next): self._value = value self._next = next def __str__(self): return ‘[ ‘ + str(… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *