Using itertools.cycle for Josephus problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using itertools.cycle for Josephus problem

I wrote this code to efficiently implement the josephus problem. I wanted to try new python concepts, but I did not get how I can use cycle, and if it would be helpful. I thought that it can help me to iterate through the list (alive) until there is only one last person (1 true value in Boolean list). Or should I just stick to a normal loop? Note: If you have other feedback, how it could work more efficient, please go ahead :) Thanks! https://code.sololearn.com/cHmAMNDOyFqE/?ref=app

11th Mar 2020, 9:33 AM
Ditus
Ditus - avatar
1 Answer
+ 2
There are many ways to solve this, one possible example with cycle() could be like this, based a little on your own code with the list of bool (I think that's a great approach): https://code.sololearn.com/cPzc3cUQHlNF/?ref=app But my favorite is the recursive solution. https://code.sololearn.com/cAlMbk5dNgsm/?ref=app https://code.sololearn.com/cD4hqMVjVZBQ/?ref=app
14th Mar 2020, 3:05 PM
Tibor Santa
Tibor Santa - avatar