|
| 1 | +"""Kata - Find the Strongest Apes |
| 2 | +
|
| 3 | +completed at: 2024-05-14 18:45:48 |
| 4 | +by: Jakub Červinka |
| 5 | +
|
| 6 | +Imagine that you went to a zoo and some zoologists asked you for help. |
| 7 | +They want you to find the strongest ape of its own kind and there are 4 types of apes in the zoo. (Gorilla, Gibbon, Orangutan, Chimpanzee) |
| 8 | +
|
| 9 | +* There will be only one parameter which is a list containing lots of dictionaries. |
| 10 | +* Each dictionary will be like this: `{"name": name, "weight": weight, "height": height, "type": type}` |
| 11 | +* To find the strongest ape, you need to compare the sum of weight and height of each apes of that kind. |
| 12 | +* The ape with the highest weight and height will be the strongest ape. |
| 13 | +* You need to return a dictionary which contains the names of the strongest apes of all kinds.`{"Gorilla": strongest_gorilla, "Gibbon": strongest_gibbon, "Orangutan": strongest_orangutan, "Chimpanzee": strongest_chimpanzee}` |
| 14 | +* There can be 2 or more apes which has the same highest weight and height. In that case, you need to sort their names by alphabet and then choose the first one as the strongest ape. |
| 15 | +* If there are no apes for a kind (e.g. Gorilla), you need to return a dictionary like this: `{"Gorilla": None, "Gibbon": strongest_gibbon, "Orangutan": strongest_orangutan, "Chimpanzee": strongest_chimpanzee}` |
| 16 | +
|
| 17 | +__Example 1:__ |
| 18 | +``` |
| 19 | +find_the_strongest_apes( |
| 20 | + [{"name": "aba", "weight": 101, "height": 99, "type": "Gorilla"}, |
| 21 | + {"name": "abb", "weight": 99, "height": 101, "type": "Gorilla"}, |
| 22 | + {"name": "abc", "weight": 101, "height": 99, "type": "Orangutan"}, |
| 23 | + {"name": "abd", "weight": 99, "height": 101, "type": "Orangutan"}]) |
| 24 | +``` |
| 25 | +Should return `{'Gorilla': 'aba', 'Gibbon': None, 'Orangutan': 'abc', 'Chimpanzee': None}` |
| 26 | +
|
| 27 | +__Example 2:__ |
| 28 | +``` |
| 29 | +find_the_strongest_apes( |
| 30 | + [{"name": "aba", "weight": 140, "height": 120, "type": "Gorilla"}, |
| 31 | + {"name": "abb", "weight": 20, "height": 50, "type": "Gibbon"}, |
| 32 | + {"name": "abc", "weight": 75, "height": 110, "type": "Orangutan"}, |
| 33 | + {"name": "abd", "weight": 50, "height": 100, "type": "Chimpanzee"}]) |
| 34 | +``` |
| 35 | +Should return `{'Gorilla': 'aba', 'Gibbon': 'abb', 'Orangutan': 'abc', 'Chimpanzee': 'abd'}` |
| 36 | +
|
| 37 | +__Example 3:__ |
| 38 | +``` |
| 39 | +find_the_strongest_apes( |
| 40 | + [{"name": "aba", "weight": 140, "height": 120, "type": "Gorilla"}, |
| 41 | + {"name": "abb", "weight": 150, "height": 120, "type": "Gorilla"}, |
| 42 | + {"name": "abc", "weight": 75, "height": 110, "type": "Orangutan"}, |
| 43 | + {"name": "abd", "weight": 50, "height": 100, "type": "Chimpanzee"}]) |
| 44 | +``` |
| 45 | +Should return `{'Gorilla': 'abb', 'Gibbon': None, 'Orangutan': 'abc', 'Chimpanzee': 'abd'}` |
| 46 | +
|
| 47 | +_*English is not my native language, so some sentences may not be descriptive enough or even give incorrect information. If you find a wrong sentence, please feel free to comment._ |
| 48 | +""" |
| 49 | + |
| 50 | +from dataclasses import dataclass |
| 51 | +from dataclasses import field |
| 52 | +from collections import defaultdict |
| 53 | +from itertools import groupby |
| 54 | +from operator import attrgetter |
| 55 | + |
| 56 | + |
| 57 | +@dataclass |
| 58 | +class Ape: |
| 59 | + type: str |
| 60 | + power: int = field(init=False) |
| 61 | + weight: int |
| 62 | + height: int |
| 63 | + name: str |
| 64 | + |
| 65 | + def __post_init__(self): |
| 66 | + self.power = self.weight + self.height |
| 67 | + |
| 68 | + def __str__(self): |
| 69 | + return f'{self.type} {self.name!r} p: {self.power}' |
| 70 | + |
| 71 | + __repr__ = __str__ |
| 72 | + |
| 73 | + |
| 74 | +sort_fn = lambda x: (x.type, -x.power, x.name) |
| 75 | + |
| 76 | + |
| 77 | +def find_the_strongest_apes(_list): |
| 78 | + types = ('Gorilla', 'Gibbon', 'Orangutan', 'Chimpanzee') |
| 79 | + result = dict.fromkeys(types, None) |
| 80 | + |
| 81 | + monkes = sorted((Ape(**data) for data in _list), key=sort_fn) |
| 82 | + |
| 83 | + for _, apes in groupby(monkes, key=attrgetter('type')): |
| 84 | + ape = next(apes) |
| 85 | + result[ape.type] = ape.name |
| 86 | + |
| 87 | + return result |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments