50 XP

58. Last one standing

Intermediate queues simulation

People numbered 1 to n stand in a circle. Starting from person 1, count around the circle; the person counted k-th leaves, and the count starts again at 1 with the next person still in the circle. This goes on until one person is left.

With n = 5 and k = 2: count 1, 2, so person 2 leaves. Count 3, 4: person 4 leaves. Count 5, 1: person 1 leaves. Count 3, 5: person 5 leaves. Person 3 is the last one standing.

Write a function last_standing(n, k) that takes the number of people n and the count k, and returns the number of the last person left. k can be bigger than the number of people still in the circle: the count simply goes round more than once. With k = 1 people leave in order, so person n is last.

Example 1

Inputn = 5, k = 2
Output3
Explanation2, 4, 1 and 5 leave in that order.

Example 2

Inputn = 7, k = 3
Output4
Explanation3, 6, 2, 7, 5 and 1 leave; 4 is left.

Example 3

Inputn = 1, k = 5
Output1
ExplanationAlone from the start.

Constraints

  • n and k are whole numbers from 1 to 100.

Submit also runs 8 more tests you can't see.

Practises Modules and Imports (lesson 22).

Code
def last_standing(n, k):
    """Find the last person left in the counting circle.

    n: int, how many people, numbered 1 to n
    k: int, every k-th person counted leaves
    Returns: int, the number of the last person left
    """
    # Write your code here
    pass