Skip to content

Instantly share code, notes, and snippets.

@ivalexandru
Created May 31, 2018 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivalexandru/80c3f1955009bd4f11cb3e76cc09bcbc to your computer and use it in GitHub Desktop.
Save ivalexandru/80c3f1955009bd4f11cb3e76cc09bcbc to your computer and use it in GitHub Desktop.
FitMoreFish.kt
//cerinta exercitiului era:
//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.
//
//fitMoreFish 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
//
//Your reflection
//fun fitMoreFish(tankSize: Double, currentFish: List<Int>, fishSize: Int = 2, hasDecorations: Boolean = true){
// println("")
//}
//Things to think about
//
//Again, there are so many ways you can do this, this is one of them:
//
//fun canAddFish(tankSize: Double, currentFish: List<Int>, fishSize: Int = 2, hasDecorations: Boolean = true): Boolean {
// return (tankSize * if (hasDecorations) 0.8 else 1.0) >= (currentFish.sum() + fishSize)
//}
//
//Notice how you can use the .sum() function in the list? This is a way to add up all elements in a list without having to use loops.
import java.util.*
fun main(args: Array<String>) {
println("Hello, ${args[0]}!")
feedTheFish()
println(canAddFish(10.0, listOf(3,3,3)))
println(canAddFish(8.0, listOf(2,2,2), hasDecorations = false))
println(canAddFish(9.0, listOf(1,1,3), 3))
println(canAddFish( 10.0, listOf(), 7, true))
}
fun feedTheFish(){
val day = randomDay() //we call the function here
val food = fishFood(day)
println("Today is $day and the fish eat $food")
}
//de unde vin 0.8 si 1.0 ? de aici:
//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.
fun canAddFish(tankSize: Double,
currentFish: List<Int>,
fishSize: Int = 2,
hasDecorations: Boolean = true): Boolean{
return (tankSize * if (hasDecorations) 0.8 else 1.0) >= (currentFish.sum() + fishSize)
}
//Notice how you can use the .sum() function in the list? This is a way to add up all elements in a list without having to use loops.
fun randomDay():String {
val week = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
return week[Random().nextInt(7)] //randomly chose index in our list
}
fun fishFood(day: String) :String {
var food = "fasting"
//since EVERYTHING in kotlin has a value, when also has a value. this value will be the last expression of the branch that was picked.asa ca poti sa return direct la when, fara sa mai pui o alta variabila
//adica nu mai tre sa pui "Monday" -> food = "flakes"
return when (day) {
"Monday" -> "flakes"
"Tuesday" -> "pellets"
"Wednesday" ->"redworms"
"Thursday" -> "granules"
"Friday" -> "mosquitoes"
"Saturday" -> "lettuce"
"Sunday" -> "plankton"
else -> "fasting"
}
return food
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment