-
Notifications
You must be signed in to change notification settings - Fork 12
Finished exercise 7 #4
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?
Changes from 1 commit
78b76b4
60b3c4e
0c78f9d
8d25698
19d1051
a4776fb
07440fd
18ec279
4e93b98
e453625
88016b2
7c6a7e2
8bdd093
1fc52dd
d5a54ce
61d298b
aec1727
79ca3ef
3b67293
9624d88
c95e5ea
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 | ||||
---|---|---|---|---|---|---|
|
@@ -20,7 +20,9 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference | |||||
*/ | ||||||
|
||||||
internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> { | ||||||
TODO("Implement me!!") | ||||||
return Pair( | ||||||
this.sorted().get(this.lastIndex), this.sorted().get(this.lastIndex-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. This is not a functional approach :) The idea of the functional approach is that you compose function calls that operate on data to get the desired result. Functional approach would be following:
Suggested change
In this case, we are using the result of the |
||||||
) | ||||||
} | ||||||
|
||||||
fun main() { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,24 +17,29 @@ 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!! | ||
return Pair( | ||
this.sorted().get(this.size-1), this.sorted().get(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. The solution is correct, but it is written in Java-style code. Please consider my suggestion above to implement it in a Kotlin, functional style. :) Also, |
||
) | ||
|
||
|
||
// 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 the code before pushing the changes. |
||
// 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!! | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,27 +59,31 @@ internal val countries = listOf( | |
*/ | ||
|
||
internal fun List<Country>.findCountryWithBiggestTotalArea(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { it.totalAreaInSquareKilometers } | ||
} | ||
|
||
internal fun List<Country>.findCountryWithBiggestPopulation(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { it.population } | ||
} | ||
|
||
internal fun List<Country>.findCountryWithHighestPopulationDensity(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { it.population/it.totalAreaInSquareKilometers } | ||
} | ||
|
||
internal fun List<Country>.findCountryWithLowestPopulationDensity(): Country { | ||
TODO("Implement me!!!") | ||
return countries.minBy { it.population/it.totalAreaInSquareKilometers } | ||
} | ||
|
||
internal fun List<Country>.findLanguageSpokenInMostCountries(): String { | ||
TODO("Implement me!!!") | ||
return countries.flatMap { it.languages } | ||
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. Very nice solution! |
||
.groupingBy { it } | ||
.eachCount() | ||
.maxBy { it.value } | ||
.key | ||
} | ||
|
||
internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> { | ||
TODO("Implement me!!!") | ||
return countries.filter { country -> country.languages.contains(language) } | ||
} | ||
|
||
|
||
|
@@ -88,7 +92,7 @@ fun main() { | |
println("Country with a biggest population is a ${countries.findCountryWithBiggestPopulation().name}") | ||
println("Country with a biggest population density is a ${countries.findCountryWithHighestPopulationDensity().name}") | ||
println("Country with a lowest population density is a ${countries.findCountryWithLowestPopulationDensity().name}") | ||
println("Language spoken in most countries is a ${countries.findLanguageSpokenInMostCountries()}") | ||
println("Language spoken in most countries is ${countries.findLanguageSpokenInMostCountries()}") | ||
val countriesThatSpeakEnglish = countries.filterCountriesThatSpeakLanguage("English") | ||
println("Countries that speak English language are ${countriesThatSpeakEnglish.joinToString { it.name }}") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,11 +49,15 @@ internal data class Address( | |
*/ | ||
|
||
internal fun user(initUser: User.() -> Unit): User { | ||
TODO("Implement me!!!") | ||
val user = User() | ||
return user.apply(initUser) | ||
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. nit: this can be a one-line expression :) |
||
} | ||
|
||
internal fun User.address(initAddress: Address.() -> Unit): User { | ||
TODO("Implement me!!!") | ||
internal fun User.address (initAddress: Address.() -> Unit): User { | ||
val adr = Address() | ||
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. That's it! Why not using |
||
adr.initAddress() | ||
this.address = adr | ||
return this | ||
} | ||
|
||
fun main() { | ||
|
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.
There is no need to
sort
the array twice. Please name the variables in English, as that is a widely accepted convention.