-
Notifications
You must be signed in to change notification settings - Fork 12
Finished excercises 1 and 2 (Aleksa Martinović) #3
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
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 |
---|---|---|
|
@@ -31,8 +31,18 @@ package org.jetbrains.exercise1.task3 | |
* | ||
*/ | ||
|
||
val charMap = mapOf( | ||
'A' to 1, 'E' to 1, 'I' to 1, 'O' to 1, 'U' to 1, 'L' to 1, 'N' to 1, 'R' to 1, 'S' to 1, 'T' to 1, | ||
'D' to 2, 'G' to 2, | ||
'B' to 3, 'C' to 3, 'M' to 3, 'P' to 3, | ||
'F' to 4, 'H' to 4, 'V' to 4, 'W' to 4, 'Y' to 4, | ||
'K' to 5, | ||
'J' to 8, 'X' to 8, | ||
'Q' to 10, 'Z' to 10, | ||
) | ||
|
||
internal fun calculateWordScrabbleScore(word: String): Int { | ||
TODO("Implement me!!!") | ||
return word.sumOf { charMap.getOrDefault(it.uppercaseChar(), 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. Very nice usage of |
||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package org.jetbrains.exercise1.task5 | ||
|
||
import kotlin.math.abs | ||
|
||
/** | ||
* Task 5: Given an integer x, return x with its digits reversed. | ||
* | ||
|
@@ -27,10 +29,10 @@ package org.jetbrains.exercise1.task5 | |
*/ | ||
|
||
internal fun reverseInteger(x: Int): Int { | ||
TODO("Implement me!!!") | ||
require(x in -1000000..1000000) { "Number must be integer in range -1000000..1000000" } | ||
return if (x < 0) ("-" + abs(x).toString().reversed()).toInt() else x.toString().reversed().toInt() | ||
|
||
} | ||
|
||
fun main() { | ||
val integer = -321 | ||
val integer = -78952 | ||
println("Reverse integer of number $integer is ${reverseInteger(integer)}") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package exercise2.task1 | ||
|
||
import exercise2.task2.findHighestSumPairFunctional | ||
import org.jetbrains.exercise2.common.isEqualsTo | ||
import org.jetbrains.exercise2.task3.findPairWithBiggestDifference | ||
|
||
|
@@ -18,7 +19,20 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference | |
*/ | ||
|
||
internal fun List<Int>.findHighestSumPair(): Pair<Int, Int> { | ||
TODO("Implement me!!") | ||
require(this.size >= 2) { "List must have at least two integers." } | ||
require(this.all { it in -1000..1000 }) | ||
var maxSum = Integer.MIN_VALUE | ||
var pair: Pair<Int, Int>? = null | ||
for((index, item) in this.dropLast(1).withIndex()) { | ||
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. Well-used |
||
for(j in (index + 1) until this.size) { | ||
val sum = item + this[j] | ||
if(sum >= maxSum) { | ||
maxSum = sum | ||
pair = Pair(item, this[j]) | ||
} | ||
} | ||
} | ||
return pair!! | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.jetbrains.exercise2.task3 | ||
|
||
import exercise2.task2.findHighestSumPairFunctional | ||
import org.jetbrains.exercise2.common.isEqualsTo | ||
import kotlin.math.abs | ||
|
||
|
@@ -17,24 +18,26 @@ 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!! | ||
// STANDARD WAY: | ||
|
||
// 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) | ||
// } | ||
// } | ||
// } | ||
// | ||
// FUNCTIONAL APPROACH: | ||
require(this.size >= 2) { "List must have at least two integers." } | ||
return this.sortedDescending().let { sortedList -> sortedList.first() to sortedList.last() } | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -59,27 +59,33 @@ 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!!!") | ||||||||||
return this.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. Good job! 👏🏻 |
||||||||||
.groupingBy { it } | ||||||||||
.eachCount() | ||||||||||
.toList() | ||||||||||
.sortedByDescending { it.second } | ||||||||||
.map{ it -> it.first } | ||||||||||
.first() | ||||||||||
|
.map{ it -> it.first } | |
.first() | |
.first() | |
.first |
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.
Also please note that first()
function call will throw an exception if the receiver list object is empty.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -49,11 +49,16 @@ internal data class Address( | |||||
*/ | ||||||
|
||||||
internal fun user(initUser: User.() -> Unit): User { | ||||||
TODO("Implement me!!!") | ||||||
val user = User() | ||||||
|
val user = User() | |
return User().apply(initUser) |
Outdated
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.
Same as in an exemple above, we can use scoped functions.
val address = Address() | |
return apply { address = Address().apply(initAddress) } |
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.
nit: Please remove the commented-out code before pushing the changes.