題目的連接java
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 eatapp
import org.junit.Test; import static org.junit.Assert.assertArrayEquals; public class ExampleTest { @Test public void example() { final String input = "fox,bug,chicken,grass,sheep"; final String[] expected = { "fox,bug,chicken,grass,sheep", "chicken eats bug", "fox eats chicken", "sheep eats grass", "fox eats sheep", "fox"}; assertArrayEquals(expected, Dinglemouse.whoEatsWho(input)); } @Test public void example2() { final String input = "bear,banana,banana,grass,busker,panda,antelope,giraffe,cow,big-fish,banana,sheep,grass,lion"; final String[] expected = { "bear,banana,banana,grass,busker,panda,antelope,giraffe,cow,big-fish,banana,sheep,grass,lion", "sheep eats grass", "bear,banana,banana,grass,busker,panda,antelope,giraffe,cow,big-fish,banana,sheep,lion"}; assertArrayEquals(expected, Dinglemouse.whoEatsWho(input)); } }
//https://www.codewars.com/kata/5902bc7aba39542b4a00003d/train/java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Dinglemouse { private static Map<String, String> map = new HashMap<String, String>(); static { map.put("antelope", "grass"); map.put("big-fish", "little-fish"); map.put("bug", "leaves"); map.put("bear", "big-fish,bug,chicken,cow,leaves,sheep"); map.put("chicken", "bug"); map.put("cow", "grass"); map.put("fox", "chicken,sheep"); map.put("giraffe", "leaves"); map.put("lion", "antelope,cow"); map.put("panda", "leaves"); map.put("sheep", "grass"); } private static String canEatLeftOrRight(String subject, String left, String right) { String result = canEat(subject, left); if (result.equals("")) { return canEat(subject, right); } return result; } private static String canEat(String subject, String object) { if (!map.containsKey(subject) || "".equals(object)) { return ""; } if (map.get(subject).indexOf(object) != -1) { return String.format("%s eats %s", subject, object); } return ""; } private static StringBuffer reduce(String[] arr, int subject, String left, String right, String rule) { String target = rule.split(" ")[2]; int purge = -1; if (target.equals(left)) { purge = subject - 1; } if (target.equals(right)) { purge = subject + 1; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < arr.length; i++) { if (i != purge) { sb.append(arr[i]).append(","); } } int len = sb.length(); if (sb.charAt(len - 1) == ',') { sb.deleteCharAt(len - 1); } return sb; } public static String[] whoEatsWho(final String zoo) { List<String> list = new ArrayList<String>(); list.add(zoo); StringBuffer sb = new StringBuffer(zoo); boolean reduceable = true; Label: while (reduceable) { String[] arr = sb.toString().split(","); if (arr.length < 2) { break; } for (int i = 0; i < arr.length; i++) { String subject = arr[i]; String left = i > 0 ? arr[i - 1] : ""; String right = i < arr.length - 1 ? arr[i + 1] : ""; String check = canEatLeftOrRight(subject, left, right); if (!check.equals("")) { list.add(check); sb = reduce(arr, i, left, right, check); break; } if (i == arr.length - 1) { reduceable = false; break Label; } } } list.add(sb.toString()); return list.toArray(new String[0]); // return new String[]{zoo, zoo}; } }