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..622f299 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ 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..2189e58 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ 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) } } diff --git a/src/main/kotlin/exercise3/task1/BalancedBrackets.kt b/src/main/kotlin/exercise3/task1/BalancedBrackets.kt index 7a6958d..063a534 100644 --- a/src/main/kotlin/exercise3/task1/BalancedBrackets.kt +++ b/src/main/kotlin/exercise3/task1/BalancedBrackets.kt @@ -26,7 +26,25 @@ package exercise3.task1 internal fun isExpressionBalanced(expression: String): Boolean { - TODO("Implement me!!!") + val stack = mutableListOf() + + for (char in expression) { + when (char) { + '(', '{', '[' -> stack.add(char) + ')', '}', ']' -> { + if (stack.isEmpty()) return false + val last = stack.removeAt(stack.size - 1) + if ((char == ')' && last != '(') || + (char == '}' && last != '{') || + (char == ']' && last != '[') + ) { + return false + } + } + } + } + + return stack.isEmpty() } fun main() { diff --git a/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt b/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt index 0e75083..6854d0b 100644 --- a/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt +++ b/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt @@ -24,9 +24,39 @@ package exercise3.task2 */ internal fun String.splitToBracketsClusters(): List { - TODO("Implement me!!!") + val stack = mutableListOf() + val clusters = mutableListOf() + var currentCluster = "" + + for (char in this) { + when (char) { + '(' -> { + stack.add(char) + currentCluster += char + } + ')' -> { + if (stack.isEmpty()) + return emptyList() + val last = stack.removeAt(stack.size - 1) + if (last == '(') + currentCluster += char + else + return emptyList() + if (stack.isEmpty()) { + clusters.add(currentCluster) + currentCluster = "" + } + } + } + } + if (stack.isNotEmpty()) + return emptyList() + + return clusters + } + fun main() { val expressionsToClustersCatalog = mapOf( "()()()" to listOf("()", "()", "()"), diff --git a/src/main/kotlin/exercise3/task4/TaxiParkTask.kt b/src/main/kotlin/exercise3/task4/TaxiParkTask.kt index 0dfe831..fe570d8 100755 --- a/src/main/kotlin/exercise3/task4/TaxiParkTask.kt +++ b/src/main/kotlin/exercise3/task4/TaxiParkTask.kt @@ -10,7 +10,8 @@ package exercise3.task4 * Find all the drivers who performed no trips. */ internal fun TaxiPark.findFakeDrivers(): Set { - TODO("Implement me!!!") + return allDrivers.filter { driver -> trips.none { it.driver == driver } } + .toSet() } /** @@ -18,15 +19,28 @@ internal fun TaxiPark.findFakeDrivers(): Set { * Find all the clients who completed at least the given number of trips. */ internal fun TaxiPark.findFaithfulPassengers(minTrips: Int): Set { - TODO("Implement me!!!") -} + val passengerTripsCount = trips + .flatMap { it.passengers } + .groupingBy { it } + .eachCount() + + return passengerTripsCount + .filter { it.value >= minTrips } + .keys} /** * Subtask 3: * Find all the passengers, who were taken by a given driver more than once. */ internal fun TaxiPark.findFrequentPassengers(driver: Driver): Set { - TODO("Implement me!!!") + val frequentPassengers = trips + .filter { it.driver == driver } + .flatMap { it.passengers } + .groupingBy { it } + .eachCount() + .filter { it.value > 1 } + + return frequentPassengers.keys } /** @@ -34,5 +48,18 @@ internal fun TaxiPark.findFrequentPassengers(driver: Driver): Set { * Find the passengers who had a discount for the majority of their trips. */ internal fun TaxiPark.findSmartPassengers(): Set { - TODO("Implement me!!!") + val passengerDiscountTripsCount = trips + .flatMap { trip -> + trip.passengers.map { passenger -> + passenger to (if (trip.discount != null && trip.discount > 0) 1 else 0) + } + } + .groupBy({ it.first }, { it.second }) + .mapValues { (_, discounts) -> discounts.sum() } + + return passengerDiscountTripsCount + .filter { (_, discounts) -> + discounts >= trips.size / 2 + } + .keys }