Collecting Halloween candy

Question

Let's assume it's Halloween, and you're looking to go door to door to collect some candy. You've scoped out the area from prior years, and compiled a list of specific houses that you want to collect candy from.

You don't want to have to visit every house by yourself, and each house only needs to be visited once, so you decide to delegate the work amongst a group. For your Halloween empire, you've set up the following recursive algorithm to delegate the work:

  • Appoint a candy collector, and give all the work to them

  • Assign titles and responsibilities based on the number of houses they are responsible for

  • If the candy collector is responsible for > 1 house, then they are a manager and can then appoint two candy collectors under them to divide the work

  • If the candy collector is responsible for 1 house, then they are a worker and will collect the candy from the houses assigned to them

For example, if your master list then has 4 houses to collect from, the responsibilities and collection process would look as follows:

Collecting Halloween candy - 1

Given this information along with your master array of houses, called 'houses', write a function to delegate the work and collect the candy.

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now