How Many People Are Visible

Python Puzzles

Back to the Python! homepage

There are 5 people, each with a different height. They all stand in a straight line, and you stand directly in front of them looking at them.

How many different arrangements of those 5 people are possible that would have exactly 3 people visible to you?

There are 35 different arrangements.

#creates the permutations
import itertools
permutations = list(itertools.permutations([1, 2, 3, 4, 5]))

#use below for testing specific tuples
#permutations = []

my_list = []

#Uncomment the print statements to follow the logic
for my_tuple in permutations:
    i = 0
    visible = 1
    #initialize the largest value in the tuple to the first value
    largest_value = my_tuple[0]
    #loop through each element of the tuple
    while i < len(my_tuple) - 1:
        if largest_value < my_tuple[i+1]:        
            visible = visible + 1
            largest_value = my_tuple[i+1]
        i = i + 1
    if visible == 3: my_list.append(my_tuple)

print('The number of different arrangements is', len(my_list))