Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a26df3e2988bcdded204893 Challenge 357: Food Chain 29 challenge-357

--description--

Given an array of [predator, prey] pairs, return the food chain from the apex predator down to the bottom.

  • The apex predator is the animal that is never prey to another animal.
  • Return the chain as an array of strings.

--hints--

get_food_chain([["cat", "mouse"]]) should return ["cat", "mouse"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_food_chain([["cat", "mouse"]]), ["cat", "mouse"])`)
}})

get_food_chain([["wolf", "deer"], ["deer", "grass"]]) should return ["wolf", "deer", "grass"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_food_chain([["wolf", "deer"], ["deer", "grass"]]), ["wolf", "deer", "grass"])`)
}})

get_food_chain([["hawk", "snake"], ["snake", "frog"], ["frog", "fly"]]) should return ["hawk", "snake", "frog", "fly"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_food_chain([["hawk", "snake"], ["snake", "frog"], ["frog", "fly"]]), ["hawk", "snake", "frog", "fly"])`)
}})

get_food_chain([["rabbit", "grass"], ["fox", "rabbit"], ["eagle", "fox"]]) should return ["eagle", "fox", "rabbit", "grass"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_food_chain([["rabbit", "grass"], ["fox", "rabbit"], ["eagle", "fox"]]), ["eagle", "fox", "rabbit", "grass"])`)
}})

get_food_chain([["seal", "salmon"], ["herring", "shrimp"], ["orca", "seal"], ["shrimp", "plankton"], ["salmon", "herring"]]) should return ["orca", "seal", "salmon", "herring", "shrimp", "plankton"].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_food_chain([["seal", "salmon"], ["herring", "shrimp"], ["orca", "seal"], ["shrimp", "plankton"], ["salmon", "herring"]]), ["orca", "seal", "salmon", "herring", "shrimp", "plankton"])`)
}})

--seed--

--seed-contents--

def get_food_chain(pairs):

    return pairs

--solutions--

def get_food_chain(pairs):
    prey_set = {p for _, p in pairs}
    chain_map = dict(pairs)
    apex = next(predator for predator, _ in pairs if predator not in prey_set)
    chain = [apex]
    while chain[-1] in chain_map:
        chain.append(chain_map[chain[-1]])
    return chain