import java.util.Arrays; public class FeedTheDogs { public static int[] solution(int[] food, int[] dogs) { int[] position = dogs; boolean first = true; while (!checkEmpty(food)) { for (int i = 0; i < position.length; i++) { int j = position[i]; if (food[j] > 0) food[j]--; else { int move = moveDog(food, i, first, position); if (move != position[i]) { position[i] = move; food[move]--; } } first = false; } } return position; } public static int closeDog(int[] position, int dog) { int closestDog = Integer.MAX_VALUE; int closest = Integer.MAX_VALUE; for (int i : position) { int dif = Math.abs(i - dog); if (dif != 0 && dif < closest) { closest = dif; closestDog = i; } else if (dif != 0 && dif == closest) return dog; } return closestDog; } public static int moveDog(int[] food, int i, boolean first, int[] position) { int r = position[i]; int l= position[i]; while(r < food.length -1 || l > 0) { if(r < food.length-1 ) r++; if(l > 0) l--; if (food[r] > 0 && food[l] == 0) return r; if (food[r] == 0 && food[l] > 0) return l; if (food[r] > 0 && food[l] > 0) { int closestDog = closeDog(position, position[i]); if (closestDog < position[i])return r; if (closestDog > position[i] || closestDog == Integer.MAX_VALUE) return l; if (first) { for (int j = 0; j < i; j++) { if(position[j]>position[i])return position[i]; } return l; } return position[i]; } } return position[i]; } public static boolean checkEmpty(int[] food) { for (int f : food) { if (f != 0) return false; } return true; } }
def feedTheDogs(food,dogs):return dogsPosition- import java.util.Arrays;
- public class FeedTheDogs {
- public static int[] solution(int[] food, int[] dogs) {
- int[] position = dogs;
- boolean first = true;
- while (!checkEmpty(food)) {
- for (int i = 0; i < position.length; i++) {
- int j = position[i];
- if (food[j] > 0) food[j]--;
- else {
- int move = moveDog(food, i, first, position);
- if (move != position[i]) {
- position[i] = move;
- food[move]--;
- }
- }
- first = false;
- }
- }
- return position;
- }
- public static int closeDog(int[] position, int dog) {
- int closestDog = Integer.MAX_VALUE;
- int closest = Integer.MAX_VALUE;
- for (int i : position) {
- int dif = Math.abs(i - dog);
- if (dif != 0 && dif < closest) {
- closest = dif;
- closestDog = i;
- } else if (dif != 0 && dif == closest)
- return dog;
- }
- return closestDog;
- }
- public static int moveDog(int[] food, int i, boolean first, int[] position) {
- int r = position[i];
- int l= position[i];
- while(r < food.length -1 || l > 0) {
- if(r < food.length-1 ) r++;
- if(l > 0) l--;
- if (food[r] > 0 && food[l] == 0) return r;
- if (food[r] == 0 && food[l] > 0) return l;
- if (food[r] > 0 && food[l] > 0) {
- int closestDog = closeDog(position, position[i]);
- if (closestDog < position[i])return r;
- if (closestDog > position[i] || closestDog == Integer.MAX_VALUE) return l;
- if (first) {
- for (int j = 0; j < i; j++) {
- if(position[j]>position[i])return position[i];
- }
- return l;
- }
- return position[i];
- }
- }
- return position[i];
- }
- public static boolean checkEmpty(int[] food) {
- for (int f : food) {
- if (f != 0) return false;
- }
- return true;
- }
- }
import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import org.junit.jupiter.api.Test; class SolutionTest { @Test void testCase() { assertTrue(Arrays.equals(new int[]{2,3}, FeedTheDogs.solution(new int[]{1,2,3,6},new int[]{0,3}))); assertTrue(Arrays.equals(new int[]{4,5}, FeedTheDogs.solution(new int[]{1,1,1,6,2,1},new int[]{1,3}))); assertTrue(Arrays.equals(new int[]{1,1}, FeedTheDogs.solution(new int[]{1,6,1},new int[]{0,2}))); assertTrue(Arrays.equals(new int[]{2,4}, FeedTheDogs.solution(new int[]{1,0,1,0,1},new int[]{0,4}))); assertTrue(Arrays.equals(new int[]{0,4}, FeedTheDogs.solution(new int[]{1,0,0,0,1},new int[]{2,4}))); assertTrue(Arrays.equals(new int[]{0,2}, FeedTheDogs.solution(new int[]{4,0,3,1,0},new int[]{1,4}))); assertTrue(Arrays.equals(new int[]{4,4}, FeedTheDogs.solution(new int[]{0,1,2,3,4},new int[]{2,2}))); assertTrue(Arrays.equals(new int[]{2,4}, FeedTheDogs.solution(new int[]{0,0,1,0,0},new int[]{1,4}))); assertTrue(Arrays.equals(new int[]{4,4,4}, FeedTheDogs.solution(new int[]{0,2,1,3,4},new int[]{1,2,3}))); assertTrue(Arrays.equals(new int[]{1,3,2}, FeedTheDogs.solution(new int[]{0,5,1,7,2},new int[]{0,3,4}))); assertTrue(Arrays.equals(new int[]{0,0,0}, FeedTheDogs.solution(new int[]{7,0,5,1,7,2},new int[]{4,2,3}))); assertTrue(Arrays.equals(new int[]{5,5,4}, FeedTheDogs.solution(new int[]{0,2,0,4,1,2},new int[] {3,2,1}))); } }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- import static org.junit.jupiter.api.Assertions.assertTrue;
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(feedTheDogs([1,2,3,6],[0,3]),[2,3])test.assert_equals(feedTheDogs([1,1,1,6,2,1],[1,3]),[4,5])test.assert_equals(feedTheDogs([1,6,1,],[0,2]),[1,1])test.assert_equals(feedTheDogs([1,0,1,0,1],[0,4]),[0,4])test.assert_equals(feedTheDogs([1,0,0,0,1],[2,4]),[0,4])test.assert_equals(feedTheDogs([4,0,3,1,0],[1,4]),[0,2])- import java.util.Arrays;
- import org.junit.jupiter.api.Test;
- class SolutionTest {
- @Test
- void testCase() {
- assertTrue(Arrays.equals(new int[]{2,3}, FeedTheDogs.solution(new int[]{1,2,3,6},new int[]{0,3})));
- assertTrue(Arrays.equals(new int[]{4,5}, FeedTheDogs.solution(new int[]{1,1,1,6,2,1},new int[]{1,3})));
- assertTrue(Arrays.equals(new int[]{1,1}, FeedTheDogs.solution(new int[]{1,6,1},new int[]{0,2})));
- assertTrue(Arrays.equals(new int[]{2,4}, FeedTheDogs.solution(new int[]{1,0,1,0,1},new int[]{0,4})));
- assertTrue(Arrays.equals(new int[]{0,4}, FeedTheDogs.solution(new int[]{1,0,0,0,1},new int[]{2,4})));
- assertTrue(Arrays.equals(new int[]{0,2}, FeedTheDogs.solution(new int[]{4,0,3,1,0},new int[]{1,4})));
- assertTrue(Arrays.equals(new int[]{4,4}, FeedTheDogs.solution(new int[]{0,1,2,3,4},new int[]{2,2})));
- assertTrue(Arrays.equals(new int[]{2,4}, FeedTheDogs.solution(new int[]{0,0,1,0,0},new int[]{1,4})));
- assertTrue(Arrays.equals(new int[]{4,4,4}, FeedTheDogs.solution(new int[]{0,2,1,3,4},new int[]{1,2,3})));
- assertTrue(Arrays.equals(new int[]{1,3,2}, FeedTheDogs.solution(new int[]{0,5,1,7,2},new int[]{0,3,4})));
- assertTrue(Arrays.equals(new int[]{0,0,0}, FeedTheDogs.solution(new int[]{7,0,5,1,7,2},new int[]{4,2,3})));
- assertTrue(Arrays.equals(new int[]{5,5,4}, FeedTheDogs.solution(new int[]{0,2,0,4,1,2},new int[] {3,2,1})));
- }
- }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.TreeMap; public class TreeJava { public static String[] solution(String str) { Map<String, Integer> t = new TreeMap<>(); String[] words = str.split("[,. ]"); List<String> sol = new ArrayList<>(); int max = words[0].length(); for (String string : words) { if (max < string.length()) max = string.length(); } for (int i = 1; i < max; i++) { for (String string : words) { if (i <= string.length()) t.put(string.substring(0, i), i); } } for (Map.Entry<String, Integer> entry : t.entrySet()) { String key = entry.getKey(); Integer val = entry.getValue(); int cont = 0; List<String> l = new ArrayList<>(); for (String string : words) { if (string.startsWith(key)) { cont++; l.add(string); } } if (cont == val) sol.addAll(l); } Comparator<String> compare=new Comparator<String>() { @Override public int compare(String o1, String o2) { if(str.indexOf(o1)<str.indexOf(o2)) return -1; if(str.indexOf(o1)>str.indexOf(o2)) return 1; return 0; } }; sol.sort(compare); System.out.println(sol.toString()); return sol.toArray(new String[sol.size()]); } }
def letras(str):return ["mejorate", "hola", "ho","esto","estas","estreñido"]- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- public class TreeJava {
- public static String[] solution(String str) {
- Map<String, Integer> t = new TreeMap<>();
- String[] words = str.split("[,. ]");
- List<String> sol = new ArrayList<>();
- int max = words[0].length();
- for (String string : words) {
- if (max < string.length())
- max = string.length();
- }
- for (int i = 1; i < max; i++) {
- for (String string : words) {
- if (i <= string.length())
- t.put(string.substring(0, i), i);
- }
- }
- for (Map.Entry<String, Integer> entry : t.entrySet()) {
- String key = entry.getKey();
- Integer val = entry.getValue();
- int cont = 0;
- List<String> l = new ArrayList<>();
- for (String string : words) {
- if (string.startsWith(key)) {
- cont++;
- l.add(string);
- }
- }
- if (cont == val)
- sol.addAll(l);
- }
- Comparator<String> compare=new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- if(str.indexOf(o1)<str.indexOf(o2)) return -1;
- if(str.indexOf(o1)>str.indexOf(o2)) return 1;
- return 0;
- }
- };
- sol.sort(compare);
- System.out.println(sol.toString());
- return sol.toArray(new String[sol.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; import org.junit.jupiter.api.Test; class SolutionTest { @Test void test() { assertTrue(Arrays.equals(new String[]{"hola", "hoy", "me", "mal", "mañana", "mejor"}, TreeJava.solution("hola, hoy me encuentro mal,mañana estare mejor"))); assertTrue(Arrays.equals(new String[]{"Hola", "hoy","encuentro", "encontrado", "encuesta"}, TreeJava.solution("Hola hoy, encuentro encontrado encuesta,persona,pareja"))); } }
import codewars_test as test# TODO Write testsimport solution # or from solution import example- import static org.junit.jupiter.api.Assertions.assertTrue;
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(letras("Hola, ho esto... estas estreñido, mejorate"), ["mejorate", "hola", "ho","esto","estas","estreñido"])- import java.util.Arrays;
- import org.junit.jupiter.api.Test;
- class SolutionTest {
- @Test
- void test() {
- assertTrue(Arrays.equals(new String[]{"hola", "hoy", "me", "mal", "mañana", "mejor"}, TreeJava.solution("hola, hoy me encuentro mal,mañana estare mejor")));
- assertTrue(Arrays.equals(new String[]{"Hola", "hoy","encuentro", "encontrado", "encuesta"}, TreeJava.solution("Hola hoy, encuentro encontrado encuesta,persona,pareja")));
- }
- }