-
Code def feed_the_primates_v1(animals, food): food = set(food) banana = "🍌" if banana not in food: return 'No bananas!' return [banana for m in animals if m in {"🐒", "🦍"} and banana in food] def feed_the_primates_v2(animals: list, food: list) -> any: return 'No bananas!' if "🍌" not in food else list( map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
Test Cases import timeit import time import codewars_test as test from solution import feed_the_primates_v1, feed_the_primates_v2 .describe("Example") def test_group(): .it("test case") def test_case(): solution_1 = timeit.timeit(stmt='feed_the_primates_v1(["🐒", "🐊", "🐘", "🦍", "🦁", "🦒", "🐒", "🐒"], ["🍌", "🍒", "🥥", ' '"🥝", "🍕", "🍒", "🥥","🍕"])', setup='', timer=time.perf_counter, number=1000, globals=globals()) solution_2 = timeit.timeit(stmt='feed_the_primates_v2(["🐒", "🐊", "🐘", "🦍", "🦁", "🦒", "🐒", "🐒"], ["🍌", "🍒", "🥥", ' '"🥝", "🍕", "🍒", "🥥","🍕"])', setup='', timer=time.perf_counter, number=1000, globals=globals()) test.assert_equals(solution_1 < solution_2, True)
Output:
-
Code - def feed_the_primates_v1(animals, food):
if "🍌" not in food:- food = set(food)
- banana = "🍌"
- if banana not in food:
- return 'No bananas!'
return [f"🍌" for m in animals if m in ["🐒", "🦍"] and "🍌" in food]- return [banana for m in animals if m in {"🐒", "🦍"} and banana in food]
- def feed_the_primates_v2(animals: list, food: list) -> any:
- return 'No bananas!' if "🍌" not in food else list(
- map(lambda a: f"🍌{a}", filter(lambda m: m if m in ["🐒", "🦍"] else None, animals)))
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}