object FeedTheDogs { def feedTheDogs(food: List[Int], dogs: List[Int]): List[Int] = { val position: ArrayBuffer[Int] = ArrayBuffer() position.addAll(dogs) val foodArray: ArrayBuffer[Int] = ArrayBuffer() foodArray.addAll(food) var first: Boolean = true while (!checkEmpty(foodArray)) { for (i <- Range(0, position.length)) { val j = position(i) if (foodArray(j) > 0) foodArray(j) -= 1 else { val move = moveDogs(foodArray, i, first, position) if (move != position(i)) { position(i) = move foodArray(move) -= 1 } } first = false } } return position.toList } def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = { var closestDog = Integer.MAX_VALUE var closest = Integer.MAX_VALUE position foreach { x => val dif = Math.abs(x - dog) if (dif != 0 && dif < closest) { closest = dif closestDog = x } else if (dif != 0 && dif == closest) { return dog } } closestDog } def moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = { var r = position(i) var l = position(i) while (r < food.length - 1 || l > 0) { if (r < food.length - 1) r += 1 if (l > 0) l -= 1 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) { val closestDog = closeDogs(position, position(i)) if (closestDog < position(i)) return r if (closestDog > position(i) || closestDog == Integer.MAX_VALUE) return l if (first) { for (j <- Range(0, i)) { if (position(j) > position(i)) return position(i) } return l } return position(i) } } position(i) } def checkEmpty(food: ArrayBuffer[Int]): Boolean = { food foreach (x => if (x != 0) return false) true } }
def solution(food: ArrayBuffer[Int], dogs: ArrayBuffer[Int]): ArrayBuffer[Int] = {val position: ArrayBuffer[Int] = dogsvar first: Boolean = truewhile(!checkEmpty(food)) {for(i <- Range(0, position.length)) {val j = position(i)if(food(j) > 0) food(j) -= 1else {val move = moveDogs(food, i, first, position)if(move != position(i)) {position(i) = movefood(move) -= 1- object FeedTheDogs {
- def feedTheDogs(food: List[Int], dogs: List[Int]): List[Int] = {
- val position: ArrayBuffer[Int] = ArrayBuffer()
- position.addAll(dogs)
- val foodArray: ArrayBuffer[Int] = ArrayBuffer()
- foodArray.addAll(food)
- var first: Boolean = true
- while (!checkEmpty(foodArray)) {
- for (i <- Range(0, position.length)) {
- val j = position(i)
- if (foodArray(j) > 0) foodArray(j) -= 1
- else {
- val move = moveDogs(foodArray, i, first, position)
- if (move != position(i)) {
- position(i) = move
- foodArray(move) -= 1
- }
- }
- first = false
- }
first = false- }
- return position.toList
- }
return dogs}def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = {var closestDog = Integer.MAX_VALUEvar closest = Integer.MAX_VALUEposition foreach { x =>val dif = Math.abs(x - dog)if(dif != 0 && dif < closest) {closest = difclosestDog = x} else if (dif != 0 && dif == closest) {return dog- def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = {
- var closestDog = Integer.MAX_VALUE
- var closest = Integer.MAX_VALUE
- position foreach { x =>
- val dif = Math.abs(x - dog)
- if (dif != 0 && dif < closest) {
- closest = dif
- closestDog = x
- } else if (dif != 0 && dif == closest) {
- return dog
- }
- }
- closestDog
- }
closestDog}def moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = {var r = position(i)var l = position(i)while ( r < food.length - 1 || l > 0) {if(r < food.length - 1) r += 1if(l > 0) l-= 1if(food(r) > 0 && food(l) == 0) return rif(food(r) == 0 && food(l) > 0) return lif(food(r) > 0 && food(l) > 0) {val closestDog = closeDogs(position, position(i))if(closestDog < position(i)) return rif(closestDog > position(i) || closestDog == Integer.MAX_VALUE) return lif(first) {for(j <- Range(0, i)) {if(position(j) > position(i)) return position(i)- def moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = {
- var r = position(i)
- var l = position(i)
- while (r < food.length - 1 || l > 0) {
- if (r < food.length - 1) r += 1
- if (l > 0) l -= 1
- 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) {
- val closestDog = closeDogs(position, position(i))
- if (closestDog < position(i)) return r
- if (closestDog > position(i) || closestDog == Integer.MAX_VALUE) return l
- if (first) {
- for (j <- Range(0, i)) {
- if (position(j) > position(i)) return position(i)
- }
- return l
- }
return l- return position(i)
- }
return position(i)- }
- position(i)
- }
position(i)}def checkEmpty(food: ArrayBuffer[Int]): Boolean = {food foreach( x => if(x != 0) return false)return true- def checkEmpty(food: ArrayBuffer[Int]): Boolean = {
- food foreach (x => if (x != 0) return false)
- true
- }
- }
import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers
import static org.junit.jupiter.api.Assertions.assertTrue;- import org.scalatest.flatspec.AnyFlatSpec
- import org.scalatest.matchers.should.Matchers
import java.util.Arrays;import org.junit.jupiter.api.Test;class SolutionTest {@Testvoid 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})));}}
def feedTheDogs(food, dogs): position = dogs first = True while not checkEmpty(food): for i in range(len(position)): j = position[i] if food[j] > 0: food[j] -= 1 else: move = moveDog(food, i, first, position) if move != position[i]: position[i] = move food[move] -= 1 first = False return position def closeDog(position, dog): closestDog = 9999999999999 closest = 9999999999999 for i in position: dif = abs(i - dog) if dif != 0 and dif < closest: closest = dif closestDog = i elif dif != 0 and dif == closest: return dog return closestDog def moveDog(food, i, first, position): r = position[i] l = position[i] while r < len(food) - 1 or l > 0: if r < len(food) - 1: r += 1 if l > 0: l -= 1 if food[r] > 0 and food[l] == 0: return r if food[r] == 0 and food[l] > 0: return l if food[r] > 0 and food[l] > 0: closestDog = closeDog(position, position[i]) if closestDog < position[i]: return r if closestDog > position[i] or closestDog == 9999999999999: return l return position[i] return position[i] def checkEmpty(food): for i in food: if i != 0: return False return True
def solution(food, dogs):- def feedTheDogs(food, dogs):
- position = dogs
- first = True
- while not checkEmpty(food):
- for i in range(len(position)):
- j = position[i]
- if food[j] > 0:
- food[j] -= 1
- else:
- move = moveDog(food, i, first, position)
- if move != position[i]:
- position[i] = move
- food[move] -= 1
- first = False
- return position
- def closeDog(position, dog):
- closestDog = 9999999999999
- closest = 9999999999999
- for i in position:
- dif = abs(i - dog)
- if dif != 0 and dif < closest:
- closest = dif
- closestDog = i
- elif dif != 0 and dif == closest:
- return dog
- return closestDog
- def moveDog(food, i, first, position):
- r = position[i]
- l = position[i]
- while r < len(food) - 1 or l > 0:
- if r < len(food) - 1: r += 1
- if l > 0: l -= 1
- if food[r] > 0 and food[l] == 0: return r
- if food[r] == 0 and food[l] > 0: return l
- if food[r] > 0 and food[l] > 0:
- closestDog = closeDog(position, position[i])
- if closestDog < position[i]: return r
- if closestDog > position[i] or closestDog == 9999999999999: return l
- return position[i]
- return position[i]
- def checkEmpty(food):
- for i in food:
- if i != 0: return False
- return True
import codewars_test as test # TODO Write tests import solution # or from solution import example # 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]),[2,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 static org.junit.jupiter.api.Assertions.assertTrue;- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
import java.util.Arrays;import org.junit.jupiter.api.Test;class SolutionTest {@Testvoid 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})));}}- # 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]),[2,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])
def solution(food, dogs): position = dogs first = True while not checkEmpty(food): for i in range(len(position)): j = position[i] if food[j] > 0: food[j] -= 1 else: move = moveDog(food, i, first, position) if move != position[i]: position[i] = move food[move] -= 1 first = False return position def closeDog(position, dog): closestDog = 9999999999999 closest = 9999999999999 for i in position: dif = abs(i - dog) if dif != 0 and dif < closest: closest = dif closestDog = i elif dif != 0 and dif == closest: return dog return closestDog def moveDog(food, i, first, position): r = position[i] l = position[i] while r < len(food) - 1 or l > 0: if r < len(food) - 1: r += 1 if l > 0: l -= 1 if food[r] > 0 and food[l] == 0: return r if food[r] == 0 and food[l] > 0: return l if food[r] > 0 and food[l] > 0: closestDog = closeDog(position, position[i]) if closestDog < position[i]: return r if closestDog > position[i] or closestDog == 9999999999999: return l return position[i] return position[i] def checkEmpty(food): for i in food: if i != 0: return False return True
def solution(food: ArrayBuffer[Int], dogs: ArrayBuffer[Int]): ArrayBuffer[Int] = {val position: ArrayBuffer[Int] = dogsvar first: Boolean = truewhile(!checkEmpty(food)) {for(i <- Range(0, position.length)) {val j = position(i)if(food(j) > 0) food(j) -= 1else {val move = moveDogs(food, i, first, position)if(move != position(i)) {position(i) = movefood(move) -= 1}}first = false}}return dogs}def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = {var closestDog = Integer.MAX_VALUEvar closest = Integer.MAX_VALUEposition foreach { x =>val dif = Math.abs(x - dog)if(dif != 0 && dif < closest) {closest = difclosestDog = x} else if (dif != 0 && dif == closest) {return dog}}closestDog}def moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = {var r = position(i)var l = position(i)while ( r < food.length - 1 || l > 0) {if(r < food.length - 1) r += 1if(l > 0) l-= 1if(food(r) > 0 && food(l) == 0) return rif(food(r) == 0 && food(l) > 0) return lif(food(r) > 0 && food(l) > 0) {val closestDog = closeDogs(position, position(i))if(closestDog < position(i)) return rif(closestDog > position(i) || closestDog == Integer.MAX_VALUE) return lif(first) {for(j <- Range(0, i)) {if(position(j) > position(i)) return position(i)}return l}return position(i)}}position(i)}def checkEmpty(food: ArrayBuffer[Int]): Boolean = {food foreach( x => if(x != 0) return false)return true}- def solution(food, dogs):
- position = dogs
- first = True
- while not checkEmpty(food):
- for i in range(len(position)):
- j = position[i]
- if food[j] > 0:
- food[j] -= 1
- else:
- move = moveDog(food, i, first, position)
- if move != position[i]:
- position[i] = move
- food[move] -= 1
- first = False
- return position
- def closeDog(position, dog):
- closestDog = 9999999999999
- closest = 9999999999999
- for i in position:
- dif = abs(i - dog)
- if dif != 0 and dif < closest:
- closest = dif
- closestDog = i
- elif dif != 0 and dif == closest:
- return dog
- return closestDog
- def moveDog(food, i, first, position):
- r = position[i]
- l = position[i]
- while r < len(food) - 1 or l > 0:
- if r < len(food) - 1: r += 1
- if l > 0: l -= 1
- if food[r] > 0 and food[l] == 0: return r
- if food[r] == 0 and food[l] > 0: return l
- if food[r] > 0 and food[l] > 0:
- closestDog = closeDog(position, position[i])
- if closestDog < position[i]: return r
- if closestDog > position[i] or closestDog == 9999999999999: return l
- return position[i]
- return position[i]
- def checkEmpty(food):
- for i in food:
- if i != 0: return False
- return True
def solution(food: ArrayBuffer[Int], dogs: ArrayBuffer[Int]): ArrayBuffer[Int] = { val position: ArrayBuffer[Int] = dogs var first: Boolean = true while(!checkEmpty(food)) { for(i <- Range(0, position.length)) { val j = position(i) if(food(j) > 0) food(j) -= 1 else { val move = moveDogs(food, i, first, position) if(move != position(i)) { position(i) = move food(move) -= 1 } } first = false } } return dogs } def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = { var closestDog = Integer.MAX_VALUE var closest = Integer.MAX_VALUE position foreach { x => val dif = Math.abs(x - dog) if(dif != 0 && dif < closest) { closest = dif closestDog = x } else if (dif != 0 && dif == closest) { return dog } } closestDog } def moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = { var r = position(i) var l = position(i) while ( r < food.length - 1 || l > 0) { if(r < food.length - 1) r += 1 if(l > 0) l-= 1 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) { val closestDog = closeDogs(position, position(i)) if(closestDog < position(i)) return r if(closestDog > position(i) || closestDog == Integer.MAX_VALUE) return l if(first) { for(j <- Range(0, i)) { if(position(j) > position(i)) return position(i) } return l } return position(i) } } position(i) } def checkEmpty(food: ArrayBuffer[Int]): Boolean = { food foreach( x => if(x != 0) return false) return true }
import java.util.Arrays;- def solution(food: ArrayBuffer[Int], dogs: ArrayBuffer[Int]): ArrayBuffer[Int] = {
- val position: ArrayBuffer[Int] = dogs
- var first: Boolean = true
- while(!checkEmpty(food)) {
- for(i <- Range(0, position.length)) {
- val j = position(i)
- if(food(j) > 0) food(j) -= 1
- else {
- val move = moveDogs(food, i, first, position)
- if(move != position(i)) {
- position(i) = move
- food(move) -= 1
- }
- }
- first = false
- }
- }
- return dogs
- }
- def closeDogs(position: ArrayBuffer[Int], dog: Int): Int = {
- var closestDog = Integer.MAX_VALUE
- var closest = Integer.MAX_VALUE
- position foreach { x =>
- val dif = Math.abs(x - dog)
- if(dif != 0 && dif < closest) {
- closest = dif
- closestDog = x
- } else if (dif != 0 && dif == closest) {
- return dog
- }
- }
- closestDog
- }
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 moveDogs(food: ArrayBuffer[Int], i: Int, first: Boolean, position: ArrayBuffer[Int]): Int = {
- var r = position(i)
- var l = position(i)
- while ( r < food.length - 1 || l > 0) {
- if(r < food.length - 1) r += 1
- if(l > 0) l-= 1
- 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) {
- val closestDog = closeDogs(position, position(i))
- if(closestDog < position(i)) return r
- if(closestDog > position(i) || closestDog == Integer.MAX_VALUE) return l
- if(first) {
- for(j <- Range(0, i)) {
- if(position(j) > position(i)) return position(i)
- }
- return l
- }
- return position(i)
- }
- }
- position(i)
- }
- def checkEmpty(food: ArrayBuffer[Int]): Boolean = {
- food foreach( x => if(x != 0) return false)
- return true
- }