Answer the question
In order to leave comments, you need to log in
How to count in a circle in Python?
Greetings
I'm trying to figure out how to count in a circle in python, but it's a bit tight.
Task :
There are 30 people in the circle. The leader kicks out every n-th person in a circle. It is necessary to determine the number of the person who will remain in the end and, if possible, the serial numbers of those excluded from the circle.
Answer the question
In order to leave comments, you need to log in
The incomparable wiki helps again:
https://en.wikipedia.org/wiki/%D0%97%D0%B0%D0%B4%D...
The task of Josephus. And there is an algorithm too :)
>>> import collections
>>>
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> n = 5
>>>
>>> deq = collections.deque(enumerate(lst))
>>>
>>> while len(deq) > 1:
... deq.rotate(-n)
... print(deq.pop())
...
(4, 5)
(0, 1)
(6, 7)
(3, 4)
(2, 3)
(5, 6)
(8, 9)
(1, 2)
>>> print(deq)
deque([(7, 8)])
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question