Skip to main content

Python library for solving the McKinsey Solve Game

Project description

McKinsey Solve Game

This Python package helps solving the Ecosystem Building game from the McKinsey Solve Game. In this game, you need to find a sustainable chain of 8 species among many species (39 in total).

This package provides a function called find_sustainable_food_chain, which takes a list of species as input and returns a solution that optimizes the creation of sustainable food chains, maximizing the count of species involved.

Rules of the Game

A set of 39 species is given. From this set, you need to build a sustainable food chain. Species are divided into producers and animals. Producers do not need calories to survive and consume only natural resources. Animals consume other animals and producers and need calories to survive. Animals follow the following rules:

  • The species with the highest calories provided eats first.
  • It eats the species providing the highest calories provided as a food source. In case of a tie, it will eat half from each of the two species with the same calories.
  • Eating consumes calories from the food source by the amount needed by the predator. You need calories provided greater than zero for a species to survive and all calories needed equal to zero after they eat.

If a species does not get enough calories or reaches zero with its own calories provided, the food chain is not sustainable.

Installation

To install mckinseysolvegame, simply use pip:

pip install mckinseysolvegame

Usage

Define the input species

from mckinseysolvegame import Species

my_species = [
    Species(name="Widgeon Grass",
            calories_provided=4950,
            calories_needed=0,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[]),
    Species(name="Fire Coral",
            calories_provided=5850,
            calories_needed=0,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[]),
    Species(name="Common Eel Grass",
            calories_provided=4950,
            calories_needed=0,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[]),
    Species(name="Yellow Tube Sponge",
            calories_provided=4600,
            calories_needed=0,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[]),
    Species(name="Green Zoanthid",
            calories_provided=5050,
            calories_needed=0,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[]),
    Species(name="Peacock's Tail Algae",
            calories_provided=4100,
            calories_needed=0,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[]),
    Species(name="Stalked Kelp",
            calories_provided=4650,
            calories_needed=0,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[]),
    Species(name="Rock Weed",
            calories_provided=4600,
            calories_needed=0,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[]),
    Species(name="Purple Hydrocoral",
            calories_provided=5600,
            calories_needed=0,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[]),
    Species(name="Blue Striped Angelfish",
            calories_provided=3750,
            calories_needed=4550,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=["Common Eel Grass"]),
    Species(name="Blue Shark",
            calories_provided=4650,
            calories_needed=2150,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Bicolour Angelfish",
                "Majestic Angelfish",
                "Northern Red Snapper",
                "Coral Trout",
                "Swordfish",
                "Sailfin Tang",
                "Powder Blue Tang",
                "Flame Angelfish",
                "Eyestripe Surgeonfish"
                ]),
    Species(name="Bicolour Angelfish",
            calories_provided=1900,
            calories_needed=440,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Powder Blue Tang",
                "Flame Angelfish",
                "Eyestripe Surgeonfish",
                "Stalked Kelp",
                "Purple Hydrocoral"
                ]),
    Species(name="Bicolour Parrotfish",
            calories_provided=3400,
            calories_needed=8800,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Widgeon Grass",
                "Common Eel Grass",
                "Fire Coral"
                ]),
    Species(name="Wahoo",
            calories_provided=1700,
            calories_needed=2500,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Short-tail Stingray",
                "Blue Striped Angelfish"
                ]),
    Species(name="Swordfish",
            calories_provided=2000,
            calories_needed=500,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Bicolour Angelfish"
                ]),
    Species(name="Short-tail Stingray",
            calories_provided=1450,
            calories_needed=2050,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Common Dolphinfish",
                "Foxface Rabbitfish"
                ]),
    Species(name="Sailfin Tang",
            calories_provided=2500,
            calories_needed=4800,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Rock Weed"
                ]),
    Species(name="Queen Parrotfish",
            calories_provided=3800,
            calories_needed=4700,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=["Fire Coral"]),
    Species(name="Queen Angelfish",
            calories_provided=2600,
            calories_needed=3100,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Blue Striped Angelfish",
                "Queen Parrotfish",
                "Common Eel Grass"
                ]),
    Species(name="Powder Blue Tang",
            calories_provided=3600,
            calories_needed=4350,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Stalked Kelp",
                "Purple Hydrocoral"
                ]),
    Species(name="Porcupine Pufferfish",
            calories_provided=2600,
            calories_needed=3800,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Peacock's Tail Algae",
                "Passer Angelfish"
                ]),
    Species(name="Passer Angelfish",
            calories_provided=2250,
            calories_needed=4600,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Peacock's Tail Algae"
                ]),
    Species(name="Pacific Tripletail",
            calories_provided=2150,
            calories_needed=2900,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Coral Beauty"
                ]),
    Species(name="Olive Ridley Turtle",
            calories_provided=3550,
            calories_needed=4400,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Green Zoanthid",
                "Pacific Tripletail"
                ]),
    Species(name="Northern Red Snapper",
            calories_provided=1200,
            calories_needed=2150,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Majestic Angelfish",
                "Sailfin Tang",
                "Flame Angelfish",
                "Eyestripe Surgeonfish"
                ]),
    Species(name="Majestic Angelfish",
            calories_provided=1150,
            calories_needed=3200,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Sailfin Tang",
                "Powder Blue Tang",
                "Eyestripe Surgeonfish",
                "Stalked Kelp"
                ]),
    Species(name="Long Finned Pilot Whale",
            calories_provided=3100,
            calories_needed=950,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Pacific Tripletail"
                ]),
    Species(name="Indo-Pacific Sailfish",
            calories_provided=1600,
            calories_needed=5600,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Pacific Tripletail",
                "Coral Beauty",
                "Flameback Angelfish"
                ]),
    Species(name="Hawksbill Sea Turtle",
            calories_provided=2800,
            calories_needed=4950,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Blue Striped Angelfish",
                "Queen Parrotfish",
                "Foxface Rabbitfish",
                "Common Eel Grass",
                "Fire Coral"
                ]),
    Species(name="Gem Tang",
            calories_provided=1250,
            calories_needed=4900,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=["Widgeon Grass"]),
    Species(name="Foxface Rabbitfish",
            calories_provided=800,
            calories_needed=4050,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=[
                "Widgeon Grass",
                "Fire Coral"
                ]),
    Species(name="Flameback Angelfish",
            calories_provided=2900,
            calories_needed=4750,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Green Zoanthid"
                ]),
    Species(name="Flame Angelfish",
            calories_provided=2200,
            calories_needed=4000,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Stalked Kelp",
                "Rock Weed"
                ]),
    Species(name="Eyestripe Surgeonfish",
            calories_provided=1050,
            calories_needed=3900,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Stalked Kelp",
                "Rock Weed"
                ]),
    Species(name="Cuvier Beaked Whale",
            calories_provided=1400,
            calories_needed=2250,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Coral Beauty",
                "Flameback Angelfish"
                ]),
    Species(name="Coral Trout",
            calories_provided=1850,
            calories_needed=3000,
            depth_range="61-90m",
            temperature_range="25-26.6",
            food_sources=[
                "Bicolour Angelfish",
                "Powder Blue Tang",
                "Flame Angelfish"
                ]),
    Species(name="Coral Beauty",
            calories_provided=2650,
            calories_needed=4900,
            depth_range="31-60m",
            temperature_range="26.7-28.2",
            food_sources=[
                "Yellow Tube Sponge",
                "Peacock's Tail Algae"
                ]),
    Species(name="Common Dolphinfish",
            calories_provided=2150,
            calories_needed=2100,
            depth_range="0-30m",
            temperature_range="28.3-30",
            food_sources=["Queen Parrotfish"])
]

Find the species that form a sustainable food chain

from mckinseysolvegame import Solver

Solver().find_sustainable_food_chain(my_species)

The API will return a JSON object with the following format:

{
        "Fire Coral": {
                "calories_needed": 0,
                "calories_provided": 1150
        },
        "Widgeon Grass": {
                "calories_needed": 0,
                "calories_provided": 50
        },
        "Common Eel Grass": {
                "calories_needed": 0,
                "calories_provided": 400
        },
        "Queen Parrotfish": {
                "calories_needed": 0,
                "calories_provided": 700,
                "eats": ["Fire Coral"]
        },
        "Blue Striped Angelfish": {
                "calories_needed": 0,
                "calories_provided": 1250,
                "eats": ["Common Eel Grass"]
        },
        "Queen Angelfish": {
                "calories_needed": 0,
                "calories_provided": 2600,
                "eats": ["Queen Parrotfish"]
        },
        "Wahoo": {
                "calories_needed": 0,
                "calories_provided": 1700,
                "eats": ["Blue Striped Angelfish"]
        },
        "Gem Tang": {
                "calories_needed": 0,
                "calories_provided": 1250,
                "eats": ["Widgeon Grass"]
        }
}

This dictionary contains the species name in order of eating, the food eaten as well as their final calories provided and needed.

Solve from a DataFrame

You can also solve the game using a DataFrame via a csv file. Here's an example:

Csv file species.csv:

name,calories_provided,calories_needed,depth_range,temperature_range,food_sources
Red Moss,3000,0,0-10m,26.7-28.2,
Sea Fan,3500,0,0-10m,26.7-28.2,
Sea Lettuce,3000,0,0-10m,26.7-28.2,
Blue Jellyfish,4500,3000,0-10m,26.7-28.2,Sea Lettuce;Red Moss
Glass Squid,3850,3750,0-10m,26.7-28.2, Shrimp
Great White Shark,6000,4250,0-10m,26.7-28.2,Green Sea Turtle;Loggerhead Sea Turtle;Lanternfish
Green Sea Turtle,4400,3000,0-10m,26.7-28.2,Lanternfish;Sea Lettuce;Sea Urchin;Spadefish
Lanternfish,3300,2700,0-10m,26.7-28.2, Shrimp
Loggerhead Sea Turtle,4400,4350,0-10m,26.7-28.2,Blue Jellyfish;Lanternfish;Sea Lettuce;Spadefish;Sea Urchin
Sea Urchin,2100,3000,0-10m,26.7-28.2,Sea Fan;Sea Lettuce
Shrimp,2750,1450,0-10m,26.7-28.2,Red Moss;Sea Lettuce
Spadefish,2100,2400,0-10m,26.7-28.2,Sea Fan
Swordfish,5250,3750,0-10m,26.7-28.2,Glass Squid;Lanternfish;Shrimp
import pandas as pd
from mckinseysolvegame import Solver

# read the CSV file into a DataFrame
df = pd.read_csv('species.csv')

solver = Solver()
result = solver.solve_from_dataframe(df)
print(result)

Contributing

We welcome contributions to mckinseysolvegame! If you find a bug or would like to request a new feature, please open an issue on the Github repository. If you would like to contribute code, please submit a pull request.

License

mckinseysolvegame is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mckinseysolvegame-1.0.8.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mckinseysolvegame-1.0.8-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file mckinseysolvegame-1.0.8.tar.gz.

File metadata

  • Download URL: mckinseysolvegame-1.0.8.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for mckinseysolvegame-1.0.8.tar.gz
Algorithm Hash digest
SHA256 39c4ca83d223e4daf827136341caffa093ec7e7d22455f196c3fa4e864adc169
MD5 1da26e35c4b4438ae274db587b02a7b8
BLAKE2b-256 76fac3cbeece06a266e397891ab15fe73cd731bc1153aa032ed3830212406caa

See more details on using hashes here.

File details

Details for the file mckinseysolvegame-1.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for mckinseysolvegame-1.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 c41a52f586c09b2f7f2d79f3e8f10d6d9bca4f8e853fd8de218630b92065caaf
MD5 7eeb45aaf476b062d38afa4ec1323a69
BLAKE2b-256 2a8928672080209f501b37fe0e2bb839e6e0b018ae5a2bdd32196045f3e2645f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page