In computer science (Links to an external site.)Links to an external site. and m
ID: 3732209 • Letter: I
Question
In computer science (Links to an external site.)Links to an external site. and mathematics (Links to an external site.)Links to an external site., the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game (Links to an external site.)Links to an external site..
People are standing in a circle (Links to an external site.)Links to an external site. waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.
The problem — given the number of people, starting point, direction, and number to be skipped — is to choose the position in the initial circle to avoid execution.
Write a main program that inputs m (the number of people to skip) and n (the number of people in a circle) from and write and use a class that implements the Queue ADT with linked lists. not with Python lists.
I have the code written below this but i have an error on line 71
class Node(object):
""" node for the circular queue adt"""
def __init__(self, data = None, next_node = None, prev_node=None):
super(Node, self).__init__()
self.data = data
self.next_node = self
class QueueADT(object):
"""Implementing queue using List Node"""
def __init__(self, head = None):
super(QueueADT, self).__init__()
self.head = None;
def set_head(self, head):
self.head = head;
def insert(self, data):
new_node = Node(data)
if (self.head == None):
self.head = new_node;
else:
temp = self.head.next_node
while(temp.next_node != self.head):
temp = temp.next_node
temp.next_node = new_node
new_node.next_node = self.head
def delete(self, prev,curr):
prev.next_node = curr.next_node
def size(self):
length = 1
temp = self.head.next_node
while(temp != self.head):
length += 1;
temp = temp.next_node
return length
def main():
circle = QueueADT();
m = input("Enter m: ")
n = input("Enter n: ")
for i in range(1,n+1):
circle.insert(i)
pos = 2
prev = circle.head
nxt = prev.next_node
while(prev != nxt):
if(pos == m+1): # delete the m+1 node i.e executing m+1 the person after skiping m
pos = 1;
prev.next_node = nxt.next_node;
nxt = nxt.next_node
pos += 1
prev = nxt
nxt = nxt.next_node
print (prev.data) #print the saved person
return prev.data
if __name__=="__main__":
main();
The error is
for i in range(1,n+1):
TypeError: must be str, not int
This is the code I can't format at as a file or as a word document. This is the actual code python 3. I need help fixing this error.
Explanation / Answer
#Since the code is not aligned i cant able to execute it. I guess you are inserting integer numbers.
#Actually for iterating loop, the range should be in int format. So, I have modified the inputs as integer. If you want n+1 as string then you can cast it as str(n+1) or str(i), check the below sample to insert into circle as string:
#Lemme know if you have any queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.