Data Structures in Python: Circular Linked Lists -- Josephus Problem

preview_player
Показать описание
In this video, we investigate how to solve the so-called "Josephus Problem" using the circular linked list data structure.

For more information on the Josephus Problem:

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

As usual, I have coded alongside you and was pretty comfortable...Only after completing, I recalled a problem that I have heard
long ago which simplifies to "100 people are seated in a circular arrangement. The first person kills the second one and hands the knife to the third person and third kills the fourth and hands it to fifth and so on...Who will the last living person...''And I remember the answer to this question is 73.. and I realized that I have dealt with a similar question. To this problem, the answer was vague to me earlier which involved a lot of explanation..and was not clear to me. But it has become easier to be solved..One of my practical application to the problem as of now....I was pretty happy as it worked since I do remember the answer to that question as of Thanks a ton for the effort you put into this playlist...
Code:
clist1 = CircularLinkedList()
for x in range(1, 101):
clist1.append(x)
clist1.josephus_circle(2)
clist1.print_list()

arjunreddy
Автор

currentnode = self.head
count = 1
while len(linkedlist) > 1:
if count%size == 0:
self.deletenode(currentnode)
count +=1
currentnode = currentnode.next




#just a beginner but tried a different way to solve this

MsKshitij
Автор

Nice, Easily understandable. Thanks a lot

mdarifulislamsourov
Автор

how can we use len(self) on the CircularLinkedList object when "len" is nowhere defined?

ankitkraken
Автор

I keep on getting 1 and 3 as output.The code is not being able to remove last element(3).

prakhargurha
Автор

def josephus_problem(self, step_size):
if self.head is None:
return

curr= self.head
count= 1

while curr is not curr.next:
if not count%step_size:
self.remove(curr.data)
curr= curr.next
count+=1

prateekkhade
Автор

if we have removed (cur) in second last step then how are we able to assign new 'cur' using cur = cur.next??
please assist

susmitsingh
Автор

len(self) in my system says syntax error.I am using pycharm . And I have also seen the code in github and verified with it . But it's not working . Can you please help me out.

vantipentachanukya