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
84 lines
2.4 KiB
Markdown
84 lines
2.4 KiB
Markdown
---
|
|
id: 6a26df3e2988bcdded204893
|
|
title: "Challenge 357: Food Chain"
|
|
challengeType: 29
|
|
dashedName: 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"]`.
|
|
|
|
```js
|
|
({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"]`.
|
|
|
|
```js
|
|
({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"]`.
|
|
|
|
```js
|
|
({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"]`.
|
|
|
|
```js
|
|
({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"]`.
|
|
|
|
```js
|
|
({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--
|
|
|
|
```py
|
|
def get_food_chain(pairs):
|
|
|
|
return pairs
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
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
|
|
```
|