From 293fbdec86902feda06268a5d127255b5b5b5438 Mon Sep 17 00:00:00 2001 From: DusanRile <70352064+DusanRile@users.noreply.github.com> Date: Mon, 25 Mar 2024 04:56:06 +0100 Subject: [PATCH 1/5] Domaci --- .../exercise2/task1/FindPairOfHighestSum.kt | 55 ++++++++++++++++++- .../task2/FindPairOfHighestSumFunctional.kt | 5 +- .../ConvertMethodToFunctionalApproach.kt | 35 ++++++------ .../exercise2/task4/ProcessCountriesData.kt | 22 ++++++-- .../kotlin/exercise2/task5/CreateUserDSL.kt | 9 ++- 5 files changed, 99 insertions(+), 27 deletions(-) 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() { From 2cac8cc07b56d3692be51d8c6f2634e898c268be Mon Sep 17 00:00:00 2001 From: DusanRile <70352064+DusanRile@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:20:55 +0100 Subject: [PATCH 2/5] Domaci --- .../exercise2/task1/FindPairOfHighestSum.kt | 55 +++---------------- .../task2/FindPairOfHighestSumFunctional.kt | 3 - .../ConvertMethodToFunctionalApproach.kt | 19 +------ .../exercise2/task4/ProcessCountriesData.kt | 12 +--- .../exercise3/task1/BalancedBrackets.kt | 36 +++++++++++- .../exercise3/task2/ParenthesesClusters.kt | 28 +++++++++- .../task3/SherlockValidatesString.kt | 51 ++++++++++++++++- .../kotlin/exercise3/task4/TaxiParkTask.kt | 21 +++++-- .../task1/FindPairOfHighestSumTest.kt | 3 +- 9 files changed, 139 insertions(+), 89 deletions(-) diff --git a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt index 2595e41..65d5ead 100644 --- a/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt +++ b/src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt @@ -19,59 +19,20 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference internal fun List.findHighestSumPair(): Pair { - -// 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] + var second = Integer.MIN_VALUE + var nextNum = 0 for (i in 0 ..< this.size -1){ - if (this[i+1] >= first ){ + nextNum = i+1 + if (this[nextNum] >= first ){ second = first - first = this[i+1] + first = this[nextNum] } - else if ((second < this[i+1]) && second < first){ - second = this[i+1] + else if ((second < this[nextNum]) && second < first){ + second = this[nextNum] } } - return Pair(first, second) + return first to second } fun main() { diff --git a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt index ad39886..a7daba2 100644 --- a/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt +++ b/src/main/kotlin/exercise2/task2/FindPairOfHighestSumFunctional.kt @@ -20,9 +20,6 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference */ internal fun List.findHighestSumPairFunctional(): Pair { -// 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] } } diff --git a/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt b/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt index c2890c6..f57ed21 100644 --- a/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt +++ b/src/main/kotlin/exercise2/task3/ConvertMethodToFunctionalApproach.kt @@ -18,24 +18,7 @@ import kotlin.math.abs internal fun List.findPairWithBiggestDifference(): Pair { -// 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] } + return this.sorted().let { it.first() to it[it.lastIndex] } } fun main() { diff --git a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt index 2a7f90b..cc75e41 100644 --- a/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt +++ b/src/main/kotlin/exercise2/task4/ProcessCountriesData.kt @@ -75,20 +75,10 @@ internal fun List.findCountryWithLowestPopulationDensity(): Country { } internal fun List.findLanguageSpokenInMostCountries(): String { - val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key -// println("Ovo je nesto: " + jezik) - return jezik + return this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key } internal fun List.filterCountriesThatSpeakLanguage(language: String): List { - -// 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/exercise3/task1/BalancedBrackets.kt b/src/main/kotlin/exercise3/task1/BalancedBrackets.kt index 7a6958d..4f1bb54 100644 --- a/src/main/kotlin/exercise3/task1/BalancedBrackets.kt +++ b/src/main/kotlin/exercise3/task1/BalancedBrackets.kt @@ -1,5 +1,7 @@ package exercise3.task1 +import java.util.Stack + /** * Task1: Balanced Brackets (Parentheses) Problem * @@ -26,9 +28,41 @@ package exercise3.task1 internal fun isExpressionBalanced(expression: String): Boolean { - TODO("Implement me!!!") + + if (expression.length %2 == 1) return false + + val pair1 = '(' to ')' + val pair2 = '[' to ']' + val pair3 = '{' to '}' + val pairs = arrayOf(pair1, pair2, pair3) + + val open = listOf('(', '[', '{') + val stack = Stack() + + for (i in expression){ + if(i in open){ + stack.push(i) + } + else { + if (stack.isEmpty()) return false + val tmpPair = stack.pop() to i + if (tmpPair !in pairs) return false + } + } + if(stack.isNotEmpty()) return false + + return true } +/* + {[()]()} + + if i in isOpen add to stack + else is + else is i == sa prvim sa stacka + + */ + fun main() { val expressions = listOf( "{[()]}" to true, diff --git a/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt b/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt index 0e75083..2b94669 100644 --- a/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt +++ b/src/main/kotlin/exercise3/task2/ParenthesesClusters.kt @@ -1,5 +1,7 @@ package exercise3.task2 +import java.util.* + /** * Task 2: Split Expression To Parentheses Clusters * @@ -24,7 +26,31 @@ package exercise3.task2 */ internal fun String.splitToBracketsClusters(): List { - TODO("Implement me!!!") + + if (this.length %2 == 1) return listOf() + + val open = listOf('(', '[', '{') + val stack = Stack() + + var tempString = "" + val returnList = mutableListOf() + + for (i in this){ + if(i in open){ + stack.push(i) + tempString += i + } + else { + if (stack.isEmpty()) return listOf() + tempString += i + stack.pop() + } + if (stack.isEmpty()){ + returnList += listOf(tempString) + tempString = "" + } + } + return returnList } fun main() { diff --git a/src/main/kotlin/exercise3/task3/SherlockValidatesString.kt b/src/main/kotlin/exercise3/task3/SherlockValidatesString.kt index c8d1c5b..5825726 100644 --- a/src/main/kotlin/exercise3/task3/SherlockValidatesString.kt +++ b/src/main/kotlin/exercise3/task3/SherlockValidatesString.kt @@ -1,5 +1,7 @@ package exercise3.task3 +import kotlin.math.abs + /** * Task 3: Sherlock Validates the Words * @@ -17,7 +19,7 @@ package exercise3.task3 * s = abcc * This is a valid string because we can remove one `c` and have `1` of each character in the remaining string. * - * s = abcc + * s = abccc * This string is not valid as we can only remove `1` occurrence of `c`. That leaves character frequencies of * {`a`: 1, `b`: 1, `c`: 2}. * ``` @@ -29,11 +31,54 @@ package exercise3.task3 */ internal fun isSherlockValid(s: String): String { - TODO("Implement me!!!") + + println("String s: " + s) + + val proba = s.groupingBy { it }.eachCount().all { ( maxOf(it.value) - minOf(it.value) ) <=1 } + + val proba4 = s.groupingBy { it }.eachCount() + + val proba5 = proba4.values.groupingBy { it }.eachCount() + println("Proba 5: " + proba5) + + val proba6 = proba5 + println("Proba 6: " + proba6) + + if (proba5.size > 2) return "No" + if (proba5.size == 1) return "YES" + + + val firstKey = proba5.keys.first() + val firstValue = proba5.values.first() + val secondKey = proba5.keys.last() + val secondValue = proba5.values.last() + + + if((secondValue == 1) and (secondKey== 1)) return "YES" + if((firstKey == 1) and (firstKey== 1)) return "YES" + + + println("Abs: " + abs(firstKey - secondKey)) + if ( (abs(firstKey - secondKey) <=1) ){ +// if((abs(firstValue - secondValue) <= 1 ) and (firstKey > 2 || secondKey > 2)) +// if((firstKey > 2 || secondKey > 2)) + if (abs(firstValue - secondValue ) <= 1){ + return "YES" + } + println("Vratilo YES") + return "NO" + } + else{ + println("Vratilo NO") + return "NO" + } + + + } fun main() { - val stringsToValidityCatalog = mapOf("abc" to "YES", "abcc" to "YES", "abccc" to "NO") + val stringsToValidityCatalog = mapOf("abc" to "YES", "abcc" to "YES", "aabbccccc" to "NO") stringsToValidityCatalog.forEach { (string, expectedIsValid) -> val actualIsValid = isSherlockValid(string) diff --git a/src/main/kotlin/exercise3/task4/TaxiParkTask.kt b/src/main/kotlin/exercise3/task4/TaxiParkTask.kt index 0dfe831..7d84552 100755 --- a/src/main/kotlin/exercise3/task4/TaxiParkTask.kt +++ b/src/main/kotlin/exercise3/task4/TaxiParkTask.kt @@ -1,5 +1,7 @@ package exercise3.task4 +import kotlin.math.min + /** * Task 4: Taxi park * @@ -10,7 +12,7 @@ package exercise3.task4 * Find all the drivers who performed no trips. */ internal fun TaxiPark.findFakeDrivers(): Set { - TODO("Implement me!!!") + return this.allDrivers.filter { driver -> trips.none { it.driver == driver } }.toSet() } /** @@ -18,7 +20,9 @@ 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!!!") + + return this.allPassengers.filter { passenger -> + trips.count { it.passengers.contains(passenger) } >= minTrips }.toSet() } /** @@ -26,7 +30,8 @@ internal fun TaxiPark.findFaithfulPassengers(minTrips: Int): Set { * Find all the passengers, who were taken by a given driver more than once. */ internal fun TaxiPark.findFrequentPassengers(driver: Driver): Set { - TODO("Implement me!!!") + return this.allPassengers.filter { passenger -> + trips.count { (it.driver == driver) and (it.passengers.contains(passenger)) } > 1 }.toSet() } /** @@ -34,5 +39,13 @@ 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!!!") + + /* + Kako bih mogao da recikliram kod "it.passengers.contains(passenger)", + da ne brojim dva puta? + */ + return this.allPassengers + .filter { passenger -> + trips.count { ( it.passengers.contains(passenger) ) and ( it.discount != null) } > + trips.count{ it.passengers.contains(passenger) } / 2 }.toSet() } diff --git a/src/test/kotlin/exercise2/task1/FindPairOfHighestSumTest.kt b/src/test/kotlin/exercise2/task1/FindPairOfHighestSumTest.kt index 49b6d53..e423962 100644 --- a/src/test/kotlin/exercise2/task1/FindPairOfHighestSumTest.kt +++ b/src/test/kotlin/exercise2/task1/FindPairOfHighestSumTest.kt @@ -22,7 +22,8 @@ class HighestSumPairTest { TestEntry(listOf(227, 891, -41, 666, 906, 201, -121, -100, 268, -215, -746, -712), Pair(906, 891)), TestEntry(listOf(-46, 943, 572, 15, 996, 632, 997), Pair(996, 997)), TestEntry(listOf(-453, -214, -593, 315, 497, -90, -631, 706, -950, -666, -1000, -379), Pair(706, 497)), - TestEntry(listOf(3, 1, 2), Pair(3, 2)) + TestEntry(listOf(3, 1, 2), Pair(3, 2)) , + TestEntry(listOf(3, 2, 1), Pair(3, 2)) ) testData.forEach { (input: List, expected: Pair) -> From 7a3cedcc207583914412828eeff599a962d264ab Mon Sep 17 00:00:00 2001 From: DusanRile <70352064+DusanRile@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:23:04 +0100 Subject: [PATCH 3/5] Domaci --- .idea/.name | 1 + .idea/gradle.xml | 16 ++++++++++++++++ .idea/kotlinc.xml | 6 ++++++ .idea/misc.xml | 5 +++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 34 insertions(+) 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..ce1c62c --- /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..4add044 --- /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 From e3eec5d84a20784ce25c78a6c6eeb35c83a77912 Mon Sep 17 00:00:00 2001 From: Dusan Date: Sun, 31 Mar 2024 23:44:26 +0200 Subject: [PATCH 4/5] Exercise 3 --- .idea/gradle.xml | 1 + .../task3/SherlockValidatesString.kt | 51 ++++++++----------- .../kotlin/exercise3/task4/TaxiParkTask.kt | 3 -- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index ce1c62c..1cbcde6 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -5,6 +5,7 @@