import scala.collection.mutable.ArrayBuffer 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 } }
- import scala.collection.mutable.ArrayBuffer
- 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
- }
- }
import org.scalatest.funsuite.AnyFunSuite class TestCases extends AnyFunSuite{ test("Bowls: (1,2,3,6), Dogs: (0,3) should return List(2,4)") { assert(FeedTheDogs.feedTheDogs(List(1,2,3,6), List(0,3)) == List(2,3)) } test("Bowls: (1,1,1,6,2,1), Dogs: (1,3) should return List(4,5)") { assert(FeedTheDogs.feedTheDogs(List(1,1,1,6,2,1), List(1,3)) == List(4,5)) } test("Bowls: (1,6,1), Dogs: (0,2) should return List(1,1)") { assert(FeedTheDogs.feedTheDogs(List(1,6,1), List(0,2)) == List(1,1)) } test("Bowls: (1,0,1,0,1), Dogs: (0,4) should return List(2,4)") { assert(FeedTheDogs.feedTheDogs(List(1,0,1,0,1), List(0,4)) == List(2,4)) } test("Bowls: (1,0,0,0,1), Dogs: (2,4) should return List(0,4)") { assert(FeedTheDogs.feedTheDogs(List(1,0,0,0,1), List(2,4)) == List(0,4)) } test("Bowls: (4,0,3,1,0), Dogs: (1,4) should return List(0,2)") { assert(FeedTheDogs.feedTheDogs(List(4,0,3,1,0), List(1,4)) == List(0,2)) } test("Bowls: (0,1,2,3,4), Dogs: (2,2) should return List(4,4)") { assert(FeedTheDogs.feedTheDogs(List(0,1,2,3,4), List(2,2)) == List(4,4)) } test("Bowls: (0,0,1,0,0), Dogs: (1,4) should return List(2,4)") { assert(FeedTheDogs.feedTheDogs(List(0,0,1,0,0), List(1,4)) == List(2,4)) } test("Bowls: (0,2,1,3,4), Dogs: (1,2,3) should return List(4,4,4)") { assert(FeedTheDogs.feedTheDogs(List(0,2,1,3,4), List(1,2,3)) == List(4,4,4)) } test("Bowls: (0,5,1,7,2), Dogs: (0,3,4) should return List(1,3,2)") { assert(FeedTheDogs.feedTheDogs(List(0,5,1,7,2), List(0,3,4)) == List(1,3,2)) } test("Bowls: (7,0,5,1,7,2), Dogs: (4,2,3) should return List(0,0,0)") { assert(FeedTheDogs.feedTheDogs(List(7,0,5,1,7,2), List(4,2,3)) == List(0,0,0)) } test("Bowls: (0,2,0,4,1,2), Dogs: (3,2,1) should return List(5,5,4)") { assert(FeedTheDogs.feedTheDogs(List(0,2,0,4,1,2), List(3,2,1)) == List(5,5,4)) } } class RandomTests extends AnyFunSuite{ import scala.collection.mutable.ListBuffer for (_ <- Range(0, 500)) { val amountFood = scala.util.Random.between(3, 100) val amountDogs = scala.util.Random.between(0, amountFood) val arrFood = ListBuffer[Int]() val arrDogs = ListBuffer[Int]() for (f <- Range(0, amountFood + 1)) { arrFood.append(scala.util.Random.between(0, 7)) } for (d <- Range(0, amountDogs + 1)) { arrDogs.append(scala.util.Random.between(0, amountFood)) } /* test("Bowls: (1,6,1), Dogs: (0,2) should return List(1,1)") { assert(FeedTheDogs.feedTheDogs(List(1,6,1), List(0,2)) == List(1,1)) } */ } } object FeedTheDogsSolution { import scala.collection.mutable.ArrayBuffer 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 } }
import org.scalatest.flatspec.AnyFlatSpecimport org.scalatest.matchers.should.Matchers- import org.scalatest.funsuite.AnyFunSuite
- class TestCases extends AnyFunSuite{
- test("Bowls: (1,2,3,6), Dogs: (0,3) should return List(2,4)") {
- assert(FeedTheDogs.feedTheDogs(List(1,2,3,6), List(0,3)) == List(2,3))
- }
- test("Bowls: (1,1,1,6,2,1), Dogs: (1,3) should return List(4,5)") {
- assert(FeedTheDogs.feedTheDogs(List(1,1,1,6,2,1), List(1,3)) == List(4,5))
- }
- test("Bowls: (1,6,1), Dogs: (0,2) should return List(1,1)") {
- assert(FeedTheDogs.feedTheDogs(List(1,6,1), List(0,2)) == List(1,1))
- }
- test("Bowls: (1,0,1,0,1), Dogs: (0,4) should return List(2,4)") {
- assert(FeedTheDogs.feedTheDogs(List(1,0,1,0,1), List(0,4)) == List(2,4))
- }
- test("Bowls: (1,0,0,0,1), Dogs: (2,4) should return List(0,4)") {
- assert(FeedTheDogs.feedTheDogs(List(1,0,0,0,1), List(2,4)) == List(0,4))
- }
- test("Bowls: (4,0,3,1,0), Dogs: (1,4) should return List(0,2)") {
- assert(FeedTheDogs.feedTheDogs(List(4,0,3,1,0), List(1,4)) == List(0,2))
- }
- test("Bowls: (0,1,2,3,4), Dogs: (2,2) should return List(4,4)") {
- assert(FeedTheDogs.feedTheDogs(List(0,1,2,3,4), List(2,2)) == List(4,4))
- }
- test("Bowls: (0,0,1,0,0), Dogs: (1,4) should return List(2,4)") {
- assert(FeedTheDogs.feedTheDogs(List(0,0,1,0,0), List(1,4)) == List(2,4))
- }
- test("Bowls: (0,2,1,3,4), Dogs: (1,2,3) should return List(4,4,4)") {
- assert(FeedTheDogs.feedTheDogs(List(0,2,1,3,4), List(1,2,3)) == List(4,4,4))
- }
- test("Bowls: (0,5,1,7,2), Dogs: (0,3,4) should return List(1,3,2)") {
- assert(FeedTheDogs.feedTheDogs(List(0,5,1,7,2), List(0,3,4)) == List(1,3,2))
- }
- test("Bowls: (7,0,5,1,7,2), Dogs: (4,2,3) should return List(0,0,0)") {
- assert(FeedTheDogs.feedTheDogs(List(7,0,5,1,7,2), List(4,2,3)) == List(0,0,0))
- }
- test("Bowls: (0,2,0,4,1,2), Dogs: (3,2,1) should return List(5,5,4)") {
- assert(FeedTheDogs.feedTheDogs(List(0,2,0,4,1,2), List(3,2,1)) == List(5,5,4))
- }
- }
- class RandomTests extends AnyFunSuite{
- import scala.collection.mutable.ListBuffer
- for (_ <- Range(0, 500)) {
- val amountFood = scala.util.Random.between(3, 100)
- val amountDogs = scala.util.Random.between(0, amountFood)
- val arrFood = ListBuffer[Int]()
- val arrDogs = ListBuffer[Int]()
- for (f <- Range(0, amountFood + 1)) {
- arrFood.append(scala.util.Random.between(0, 7))
- }
- for (d <- Range(0, amountDogs + 1)) {
- arrDogs.append(scala.util.Random.between(0, amountFood))
- }
- /*
- test("Bowls: (1,6,1), Dogs: (0,2) should return List(1,1)") {
- assert(FeedTheDogs.feedTheDogs(List(1,6,1), List(0,2)) == List(1,1))
- }
- */
- }
- }
- object FeedTheDogsSolution {
- import scala.collection.mutable.ArrayBuffer
- 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
- }
- }