From 402c7d1e3f40174d46618d50ad75326b16047677 Mon Sep 17 00:00:00 2001 From: Slobodanvukojevic Date: Tue, 19 Mar 2024 16:33:57 +0100 Subject: [PATCH 1/3] Added solutions 1-4 --- .idea/.name | 1 + .idea/gradle.xml | 16 ++++++++++++++++ .idea/kotlinc.xml | 6 ++++++ .idea/misc.xml | 5 +++++ .idea/vcs.xml | 6 ++++++ .../exercise2/task1/FindPairOfHighestSum.kt | 19 ++++++++++++++++++- .../task2/FindPairOfHighestSumFunctional.kt | 2 +- .../ConvertMethodToFunctionalApproach.kt | 6 +++--- .../exercise2/task4/ProcessCountriesData.kt | 16 ++++++++++------ 9 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/gradle.xml create mode 100644 .idea/kotlinc.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..4cae352 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +subject-kotlin-programming-language-exercises \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..478f98e --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..8d81632 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..53b7407 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt index ddf425b..524f98b 100644 --- a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt +++ b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt @@ -18,7 +18,24 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference */ internal fun List.findHighestSumPair(): Pair { - TODO("Implement me!!") + var firstMaximal = this[0] + var maximalIndex = -1 + + for((index, num) in this.withIndex()) { + if(num > firstMaximal) { + firstMaximal = num + maximalIndex = index + } + } + + var secondMaximal = this[0] + for((index, num) in this.withIndex()) { + if(num in (secondMaximal + 1)..firstMaximal && index != maximalIndex) { + secondMaximal = num + } + } + + return firstMaximal to secondMaximal } fun main() { diff --git a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt index b1527fa..4ae7050 100644 --- a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt +++ b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt @@ -20,7 +20,7 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference */ internal fun List.findHighestSumPairFunctional(): Pair { - TODO("Implement me!!") + return this.sortedDescending().let { it.first() 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..a379bd8 100644 --- a/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt +++ b/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt @@ -17,8 +17,8 @@ import kotlin.math.abs */ internal fun List.findPairWithBiggestDifference(): Pair { - // TODO refactor me to functional approach and make tests pass!!! - var resultPair: Pair? = null + return this.sorted().let { it.first() to it.last() } + /*var resultPair: Pair? = null var biggestDifference = Int.MIN_VALUE for (i in this.indices) { @@ -34,7 +34,7 @@ internal fun List.findPairWithBiggestDifference(): Pair { } } - return resultPair!! + return resultPair!!*/ } fun main() { diff --git a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt index 291e558..c1ae5f4 100644 --- a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt +++ b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt @@ -59,27 +59,31 @@ 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!!!") + return this.flatMap { it.languages } + .groupingBy { it } + .eachCount() + .maxBy { it.value } + .key } internal fun List.filterCountriesThatSpeakLanguage(language: String): List { - TODO("Implement me!!!") + return this.filter { it.languages.contains(language) } } From 8d0975b8fb0f7a5561b88b940a0a0fb889b75f7f Mon Sep 17 00:00:00 2001 From: Slobodanvukojevic Date: Tue, 19 Mar 2024 16:44:34 +0100 Subject: [PATCH 2/3] Added solutions 1-4 --- .idea/gradle.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 478f98e..622f299 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ +