Skip to content

Exercise 2 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,60 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference
*/

internal fun List<Int>.findHighestSumPair(): Pair<Int, Int> {
TODO("Implement me!!")


// for ( i in 0 ..this.size - 1){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

// 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){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we run your implementation for the input listOf(3, 1, 2), we would get the incorrect result Pair(3, 3).
Are the tests green for you?

nit: this.size -1 can be written as this.lastIndex

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test data for this method seems incomplete, and your implementation has a false positive in the tests, meaning the tests are green, but the actual implementation is incorrect.

In the above example for the input listOf(3, 1, 2) the expected output should be Pair(3, 2), and you implementation returns Pair(3, 3) which is incorrect. This is because the test didn't cover the case where the highest number in the list is at the first index.

I'm very sorry for the inconvenience with incomplete test data.

Please add a missing case to the test class and fix the bug in your implementation.

if (this[i+1] >= first ){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are accessing this[i + 1] many times, hence we assign this to variable nextNum and use nextNum variable everywhere else. This will improve readability.

second = first
first = this[i+1]
}
else if ((second < this[i+1]) && second < first){
second = this[i+1]
}
}
return Pair<Int, Int>(first, second)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The <Int, Int> can be omitted since the compiler is smart enough to infer the generic type.

Also, there is an alternative way to declare a pair, and that is by using to infix extension function:
first to second

}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference
*/

internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> {
TODO("Implement me!!")
// var par = Pair<Int, Int>(0, 0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

// par = this.sortedDescending().let { it[0] to it[1] }
// return par
return this.sortedDescending().let { it[0] to it[1] }
}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ import kotlin.math.abs
*/

internal fun List<Int>.findPairWithBiggestDifference(): Pair<Int, Int> {
// TODO refactor me to functional approach and make tests pass!!!
var resultPair: Pair<Int, Int>? = 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<Int, Int>? = null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

// 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] }
Copy link
Owner

@vuksa vuksa Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a mistake in the implementation where the lastIndex is stated. In the code you wrote, the lastIndex is the last index of the receiver object list. It's the same as if we wrote this.lastIndex.

TLDR: you should use it.lastIndex instead of this.lastIndex

In this case, the result will be correct, but if we, for example, filter elements of the list after sorting it, the lastIndex of the list we are operating on would change, and you would get an IndexOutOfBounds error.

Let's show this visually. In the following example, we have the list [3, 2, 4, 5, 1].
After calling sorted() on the list, we would get a new list [1, 2, 3, 4, 5], with the last index value being 4.
If after that, on the result list we call filter { it > 2 }, the new result list would be [3, 4, 5], where now the value of the last index is 2, and if we would call it[this.lastIndex] as in your code, we would get IndexOutOfBoundsException because this.lastIndex value is 4, which is greater than current last index.

}

fun main() {
Expand Down
22 changes: 16 additions & 6 deletions src/main/kotlin/exercise2/task4/ProcessCountriesData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,37 @@ internal val countries = listOf(
*/

internal fun List<Country>.findCountryWithBiggestTotalArea(): Country {
TODO("Implement me!!!")
return this.maxBy { it.totalAreaInSquareKilometers }
}

internal fun List<Country>.findCountryWithBiggestPopulation(): Country {
TODO("Implement me!!!")
return this.maxBy { it.population }
}

internal fun List<Country>.findCountryWithHighestPopulationDensity(): Country {
TODO("Implement me!!!")
return this.maxBy { it.population/it.totalAreaInSquareKilometers }
}

internal fun List<Country>.findCountryWithLowestPopulationDensity(): Country {
TODO("Implement me!!!")
return this.minBy { it.population/it.totalAreaInSquareKilometers }
}

internal fun List<Country>.findLanguageSpokenInMostCountries(): String {
TODO("Implement me!!!")
val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please declare variable names in English.

// println("Ovo je nesto: " + jezik)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

return jezik
}

internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> {
TODO("Implement me!!!")

// var lista = listOf<Country>(this.first())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up all the comments before pushing the changes and creating the PR.

// 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) }
}


Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/exercise2/task5/CreateUserDSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down