Python solve Joseph ring problem

Source: Internet
Author: User

Story:

Joseph's question (Josephus problem) is said to have been the story of a famous Jewish historian Josephus: After the Romans occupied Chotapat, 39 Jews and Josephus and his friends hid in a hole, and 39 Jews decided that they would rather die than be enemies. So decided a suicide way, 41 people into a circle, from the 1th person began to count, each count to the 3rd person must commit suicide, and then again from the next re-count, until everyone killed himself.
However, Josephus and his friends did not want to follow, Josephus to his friends to pretend to obey, he put friends and himself in the 16th and 31st position, so escaped the game of death.

Original question:

User input M,n value, n person in a ring, starting from No. 0 number of people, count to M, that person quit the game until the last person
What's the last one left?
(Joseph Ring question Josephus)

First of all, it's easier to read.

Python code
    1. def Josephus (n,m):
    2. #自动身成列表, starting from 0 to n-1
    3. People = List (range (n))
    4. M-=1
    5. k=m%n;
    6. while (Len (People) >1):
    7. Del List[k]
    8. k= (k+m)% len (people)
    9. return people [0]

To tell the truth, the feeling is still because Python data structure is powerful, List del drop auto-complement, other languages estimated to be more laborious

Python solve Joseph ring problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.