W
W
weranda2016-06-16 11:16:56
Python
weranda, 2016-06-16 11:16:56

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.

Please tell me the options for solving this problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir, 2016-06-16
@n1cew0lf

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 :)

A
abcd0x00, 2016-06-16
@abcd0x00

>>> 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)])
>>>

A
abs0lut, 2016-06-16
@abs0lut

how to count in a circle in python

>>> import itertools
>>> foo = itertools.cycle((1,2,3))
>>> for i in range(7):
       next(foo)	
1
2
3
1
2
3
1
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question