[关闭]
@aloyschen 2017-10-12T15:22:11.000000Z 字数 1657 阅读 82

CodeWars 刷题总结

CodeWars


The Hunger Games - Zoo Disaster!

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

  1. antelope eats grass
  2. big-fish eats little-fish
  3. bug eats leaves
  4. bear eats big-fish
  5. bear eats bug
  6. bear eats chicken
  7. bear eats cow
  8. bear eats leaves
  9. bear eats sheep
  10. chicken eats bug
  11. cow eats grass
  12. fox eats chicken
  13. fox eats sheep
  14. giraffe eats leaves
  15. lion eats antelope
  16. lion eats cow
  17. panda eats leaves
  18. sheep eats grass
  19. Kata Task

INPUT

A comma-separated string representing all the things at the zoo

TASK

Figure out who eats who 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

  1. INPUT
  2. "fox,bug,chicken,grass,sheep"
  3. 1 fox can't eat bug
  4. "fox,bug,chicken,grass,sheep"
  5. 2 bug can't eat anything
  6. "fox,bug,chicken,grass,sheep"
  7. 3 chicken eats bug
  8. "fox,chicken,grass,sheep"
  9. 4 fox eats chicken
  10. "fox,grass,sheep"
  11. 5 fox can't eat grass
  12. "fox,grass,sheep"
  13. 6 grass can't eat anything
  14. "fox,grass,sheep"
  15. 7 sheep eats grass
  16. "fox,sheep"
  17. 8 fox eats sheep
  18. "fox"
  19. OUTPUT
  20. ["fox,bug,chicken,grass,sheep", "chicken eats bug", "fox eats chicken", "sheep eats grass", "fox eats sheep", "fox"]

知识点

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注