# level 5: The Hunger Games - Zoo Disaster! (2019-11-25) ###### tags: `Codewars` `python` Story A freak power outage at the zoo has caused all of the electric cage doors to malfunction and swing open... All the animals are out and some of them are eating each other! It's a Zoo Disaster! Here is a list of zoo animals, and what they can eat antelope eats grass big-fish eats little-fish bug eats leaves bear eats big-fish bear eats bug bear eats chicken bear eats cow bear eats leaves bear eats sheep chicken eats bug cow eats grass fox eats chicken fox eats sheep giraffe eats leaves lion eats antelope lion eats cow panda eats leaves sheep eats grass Kata Task INPUT A comma-separated string representing all the things at the zoo TASK Figure out who eats whom until no more eating is possible. OUTPUT A list of strings (refer to the example below) where: The first element is the initial zoo (same as INPUT) The last element is a comma-separated string of what the zoo looks like when all the eating has finished All other elements (2nd to last-1) are of the form X eats Y describing what happened Notes Animals can only eat things beside them Animals always eat to their LEFT before eating to their RIGHT Always the LEFTMOST animal capable of eating will eat before any others Any other things you may find at the zoo (which are not listed above) do not eat anything and are not edible Example Input "fox,bug,chicken,grass,sheep" Working 1 fox can't eat bug "fox,bug,chicken,grass,sheep" 2 bug can't eat anything "fox,bug,chicken,grass,sheep" 3 chicken eats bug "fox,chicken,grass,sheep" 4 fox eats chicken "fox,grass,sheep" 5 fox can't eat grass "fox,grass,sheep" 6 grass can't eat anything "fox,grass,sheep" 7 sheep eats grass "fox,sheep" 8 fox eats sheep "fox" Output ["fox,bug,chicken,grass,sheep", "chicken eats bug", "fox eats chicken", "sheep eats grass", "fox eats sheep", "fox"] my idea: 將食物鏈建成一個字典,在對應字串與字典中重疊的物種。 my code: <pre> def who_eats_who(zoo): whoeatwho = { 'antelope':('grass'), 'cow':('grass'), 'sheep':('grass'), 'giraffe':('leaves'), 'panda':('leaves'), 'bug':('leaves'), 'chicken':('bug'), 'big-fish':('little-fish'), 'bear':('big-fish', 'bug', 'chicken', 'cow', 'leaves', 'sheep'), 'fox':('chicken', 'sheep'), 'lion':('antelope', 'cow') } survival = zoo.split(",") inzoo = zoo.split(",") eats = [] for x in whoeatwho : if x in inzoo: for y in inzoo: if y in whoeatwho[x]: eats.append((x + ' eats ' + y)) survival.remove(y) eats = eats + survival zoo = zoo.split("'") + eats return zoo </pre> output: ['fox,bug,chicken,grass,sheep', **'fox eats chicken', 'fox eats sheep', 'chicken eats bug', 'sheep eats grass'**, 'fox'] 標準答案: ['fox,bug,chicken,grass,sheep', **'chicken eats bug', 'fox eats chicken', 'sheep eats grass', 'fox eats sheep'**, 'fox'] 有先後邏輯上的問題。 參考code: <pre> EATERS = {"antelope": {"grass"}, "big-fish": {"little-fish"}, "bug": {"leaves"}, "bear": {"big-fish", "bug", "chicken", "cow", "leaves", "sheep"}, "chicken": {"bug"}, "cow": {"grass"}, "fox": {"chicken", "sheep"}, "giraffe": {"leaves"}, "lion": {"antelope", "cow"}, "panda": {"leaves"}, "sheep": {"grass"}} def who_eats_who(zoo): ansLst, zooLst, n = [zoo], zoo.split(","), 0 while n < len(zooLst): while n > 0 and zooLst[n-1] in EATERS.get(zooLst[n], set()): # Eats on its left ansLst.append("{} eats {}".format(zooLst[n], zooLst.pop(n-1))) n -= 2 while n >= 0 and n != len(zooLst)-1 and zooLst[n+1] in EATERS.get(zooLst[n], set()): # Eats on its right ansLst.append("{} eats {}".format(zooLst[n], zooLst.pop(n+1))) n += 1 # Nothing to eat, step forward return ansLst + [','.join(zooLst)] </pre>