-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: main
Are you sure you want to change the base?
Exercise 2 #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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){ | ||
// 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){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we run your implementation for the input nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 ){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are accessing |
||
second = first | ||
first = this[i+1] | ||
} | ||
else if ((second < this[i+1]) && second < first){ | ||
second = this[i+1] | ||
} | ||
} | ||
return Pair<Int, Int>(first, second) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Also, there is an alternative way to declare a pair, and that is by using |
||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a mistake in the implementation where the TLDR: you should use In this case, the result will be correct, but if we, for example, filter elements of the list after sorting it, the Let's show this visually. In the following example, we have the list |
||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please declare variable names in English. |
||
// println("Ovo je nesto: " + jezik) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) } | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
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.