Photograph problem , subset problem


Joined
Dec 21, 2021
Messages
1
Reaction score
0
N number of people are present in a meeting. Both Male and Female ,

The photographer will only click picture if there is no Male of Female in a group and group shall have more than 3 people.

For example , if people present are. : M F M F M , then only possible photographs are : M F M F , F M F M , M F M F M (3 photos possible)

Two photos will be considered different iff either their star or end is different.

How to solve this kind of question. Any suggestion ?
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
174
Reaction score
19
It's a pretty simple nested loop problem. I'm not 100% on the instructions ("no Male of Female in a group" was confusing), but just iterate through your list with a start position and an end position. At each start position, find all pictures of length greater than three, then go to the next position. Stop your first loop before the end (so it doesn't try to take smaller pictures) and start your second loop four spaces away from your start (for the same reason). This code should do the trick, but you might need to test the validity of lineup[i:j] before adding it to your return value.

Python:
#!/usr/bin/env python3

def photos(lineup):
        ret = []
        for i in range(0, len(lineup) - 3):
                for j in range(i + 4, len(lineup) + 1):
                        ret.append(lineup[i:j])
        return ret

print(photos("MFMFM"))
print(photos(["M", "F", "M", "F", "M"]))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top