Practice Time: canAddFish()

 

Exercise: Fit More Fish

Create a function that checks if we can add another fish into a tank that already has fish in it.

How many fish in a tank?

The most widely known rule for stocking a tank is the one-inch-per-fish-per-gallon-of-water rule. However that's assuming the tank doesn't have any decorations in it.

Typically, a tank with decorations can contain a total length of fish (in inches) less than or equal to 80% of the tank size (in gallons). A tank without decorations can contain a total length of fish up to 100% of the tank size.

For example:

  • A 10 gallon tank with decorations can hold up to 8 inches of fish, for example 4 x 2-inch-long fish.
  • A 20 gallon tank without decorations can hold up to 20 inches of fish, for example 6 x 1-inch-long fish and 2 x 2-inch-long fish.


canAddFish function

Create a function that takes these arguments:

  • tankSize (in gallons)
  • currentFish (a list of Ints representing the length of each fish currently in the tank)
  • fishSize (the length of the new fish we want to add to the tank)
  • hasDecorations (true if the the tank has decorations, false if not)

You can assume that typically a tank has decorations, and that a typical fish is 2 inches long. That means you can set those values as default parameters.


Output

Make sure you test your code against the following calls, and that you get the correct output for each.

canAddFish(10.0, listOf(3,3,3)) ---> false canAddFish(8.0, listOf(2,2,2), hasDecorations = false) ---> true canAddFish(9.0, listOf(1,1,3), 3) ---> false canAddFish(10.0, listOf(), 7, true) ---> true



// File:            Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/15/2023
// Email: nolvertou@gmail.com
// Description: Create a function that checks if we can add another fish into a tank
// that already has fish in it.

fun main(args: Array<String>) {
println("Can we add another fish into a tank that already has fish in it?")

println(canAddFish(10.0, listOf(3,3,3))) //---> false
println(canAddFish(8.0, listOf(2,2,2), hasDecorations = false)) //---> true
println(canAddFish(9.0, listOf(1,1,3), 3)) //---> false
println( canAddFish(10.0, listOf(), 7, true)) //---> true
}

/**
*
* @param:tankSize (it is in gallons)
* @param: currentFish (it is a list of integers)
* @param: fishSize (it is the length in inches of the new fish we want to add to the tank)
* @param: hasdecorations (it indicates if the tank has decorations)
*/
fun canAddFish(tankSize: Double, currentFish: List<Int>,
fishSize: Int = 2, hasDecorations: Boolean = true): Boolean{
var totalCurrentLengthFish: Double = 0.0

for (currentLengthFish in currentFish){
totalCurrentLengthFish += currentLengthFish
}

/**
* Rule 1 (with decorations):
* A tank with decorations can contain a total length of fish (in inches) less than
* or equal to 80% of the tank size (in gallons).
*
* Rule 2 (without decorations):
* A tank without decorations can contain a total length of fish up to 100% of the tank size
* (one-inch-per-fish-per-gallon-of-water rule).
*/

val LimitLengthFish: Double = if(hasDecorations) { tankSize * 0.80} else tankSize // its in inches

return (totalCurrentLengthFish+fishSize) <= LimitLengthFish
}

Comments

Popular posts from this blog