Python Tutorial (33) - Example: Joseph the Living and the Dead Game

Time: Column:Python views:272

Problem: Identifying the People Who Disembarked

In this problem, we have 30 people on a boat, and the boat is overloaded. To make room, 15 people need to disembark. People are arranged in a queue, and they are numbered from 1 to 30. Starting from 1, every 9th person in the queue will disembark. This process continues in a loop until only 15 people remain on the boat. The task is to identify the numbers of the people who disembarked.

Example

Here is the Python code to solve the problem:

people = {}
for x in range(1, 31):
    people[x] = 1

# Initialize variables
check = 0
i = 1
j = 0

# Loop until 15 people have disembarked
while i <= 31:
    if i == 31:
        i = 1
    elif j == 15:
        break
    else:
        if people[i] == 0:
            i += 1
            continue
        else:
            check += 1
            if check == 9:
                people[i] = 0
                check = 0
                print("{} disembarked".format(i))
                j += 1
            else:
                i += 1
                continue

Output

Executing the above code will produce the following result:

9 disembarked
18 disembarked
27 disembarked
6 disembarked
16 disembarked
26 disembarked
7 disembarked
19 disembarked
30 disembarked
12 disembarked
24 disembarked
8 disembarked
22 disembarked
5 disembarked
23 disembarked

This result shows the numbers of the people who disembarked the boat.