diff --git a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt index ddf425b..2595e41 100644 --- a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt +++ b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt @@ -18,7 +18,60 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference */ internal fun List.findHighestSumPair(): Pair { - TODO("Implement me!!") + + +// for ( i in 0 ..this.size - 1){ +// if(this[i] >= first){ +// first = this[i] +// } +// } +// +// for ( i in 0 .. this.size - 1){ +// if(this[i] > second && this[i] < first){ +// second = this[i] +// } +// } + +// var first = this[0] +// var second = this[0] +// println("Velicina " + this.size) +// for (i in 0 ..< this.size -1){ +// if (this[i+1] >= first ) first = this[i+1] +// if ((this[i] > second) && (this[i] < first)) second = this[i] +// } +// if ((this[lastIndex] > second) && (this[lastIndex] < first)) second = this[lastIndex] +// println("Prvi: " + first) +// println("Drugi: " + second) + + +// Prlazak kroz dve petlje +// var first = this[1] +// var second = this[0] +// for ( i in indices){ +// if(this[i] >= first){ +// first = this[i] +// } +// } +// +// for ( i in indices){ +// if((this[i] > second) && (this[i] < first)){ +// second = this[i] +// } +// } + +// Prolazak kroz jednu petlju + var first = this[0] + var second = this[0] + for (i in 0 ..< this.size -1){ + if (this[i+1] >= first ){ + second = first + first = this[i+1] + } + else if ((second < this[i+1]) && second < first){ + second = this[i+1] + } + } + return Pair(first, second) } fun main() { diff --git a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt index b1527fa..ad39886 100644 --- a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt +++ b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt @@ -20,7 +20,10 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference */ internal fun List.findHighestSumPairFunctional(): Pair { - TODO("Implement me!!") +// var par = Pair(0, 0) +// par = this.sortedDescending().let { it[0] to it[1] } +// return par + return this.sortedDescending().let { it[0] to it[1] } } fun main() { diff --git a/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt b/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt index 47f814b..c2890c6 100644 --- a/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt +++ b/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt @@ -17,24 +17,25 @@ import kotlin.math.abs */ internal fun List.findPairWithBiggestDifference(): Pair { - // TODO refactor me to functional approach and make tests pass!!! - var resultPair: Pair? = null - var biggestDifference = Int.MIN_VALUE - - for (i in this.indices) { - for (j in (i + 1) until this.size) { - val first = this[i] - val second = this[j] - val absDifference = abs(first - second) - - if (absDifference >= biggestDifference) { - biggestDifference = absDifference - resultPair = Pair(first, second) - } - } - } - return resultPair!! +// var resultPair: Pair? = null +// var biggestDifference = Int.MIN_VALUE +// +// for (i in this.indices) { +// for (j in (i + 1) until this.size) { +// val first = this[i] +// val second = this[j] +// val absDifference = abs(first - second) +// +// if (absDifference >= biggestDifference) { +// biggestDifference = absDifference +// resultPair = Pair(first, second) +// } +// } +// } +// return resultPair!! + + return this.sorted().let { it.first() to it[lastIndex] } } fun main() { diff --git a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt index 291e558..2a7f90b 100644 --- a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt +++ b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt @@ -59,27 +59,37 @@ internal val countries = listOf( */ internal fun List.findCountryWithBiggestTotalArea(): Country { - TODO("Implement me!!!") + return this.maxBy { it.totalAreaInSquareKilometers } } internal fun List.findCountryWithBiggestPopulation(): Country { - TODO("Implement me!!!") + return this.maxBy { it.population } } internal fun List.findCountryWithHighestPopulationDensity(): Country { - TODO("Implement me!!!") + return this.maxBy { it.population/it.totalAreaInSquareKilometers } } internal fun List.findCountryWithLowestPopulationDensity(): Country { - TODO("Implement me!!!") + return this.minBy { it.population/it.totalAreaInSquareKilometers } } internal fun List.findLanguageSpokenInMostCountries(): String { - TODO("Implement me!!!") + val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key +// println("Ovo je nesto: " + jezik) + return jezik } internal fun List.filterCountriesThatSpeakLanguage(language: String): List { - TODO("Implement me!!!") + +// var lista = listOf(this.first()) +// println("eo") +// this.flatMap { it.languages }.find { it.equals(language) }?.forEach { println(it) } +// var a = this.map { it.name to it.languages }.filter { it.second.contains(language) } +// lista = this.filter { it.languages.contains(language) } +// var a = this.flatMap { it.languages }.contains(language) + + return this.filter { it.languages.contains(language) } } diff --git a/src/main/kotlin/exercise2/task5/CreateUserDSL.kt b/src/main/kotlin/exercise2/task5/CreateUserDSL.kt index 855c0cb..6471402 100644 --- a/src/main/kotlin/exercise2/task5/CreateUserDSL.kt +++ b/src/main/kotlin/exercise2/task5/CreateUserDSL.kt @@ -49,11 +49,16 @@ internal data class Address( */ internal fun user(initUser: User.() -> Unit): User { - TODO("Implement me!!!") + val newUser = User() + newUser.initUser() + return newUser } internal fun User.address(initAddress: Address.() -> Unit): User { - TODO("Implement me!!!") + val newAddress = Address() + newAddress.initAddress() + address = newAddress + return this } fun main() {