-
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?
Conversation
@@ -40,13 +40,25 @@ import java.util.Scanner | |||
*/ | |||
|
|||
internal fun calculateGrade(score: Int): Int { | |||
TODO("Implement me!!!") | |||
// require(score in 0..100) { |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice usage of Map#getOrDefault
function. Great job!
@@ -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() |
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.
In this particular case,
abs(x).toString().reversed()).toInt()
and x.toString().reversed().toInt()
expressions always provide the same result, so it's a safe to store a abs(x).toString().reversed()).toInt()
value to a variable and only prefix the -
sign in case number is negative.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Well-used withIndex
extension function. Good job!
} | ||
|
||
return resultPair!! | ||
// STANDARD WAY: |
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: Same thing a in the first task, please keep your code clean and do not push commented out code :)
.map{ it -> it.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.
Here, we can apply a small optimization and avoid unnecessary map
calls that operate over the entire collection. Since we are only interested in the single language that is used in most countries, instead of map { ... }.first()
call, we can do the following:
.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.
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Good job! 👏🏻
@@ -49,11 +49,16 @@ internal data class Address( | |||
*/ | |||
|
|||
internal fun user(initUser: User.() -> Unit): User { | |||
TODO("Implement me!!!") | |||
val user = User() |
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.
That is it! We can also write this with the scoped functions:
val user = User() | |
return User().apply(initUser) |
} | ||
|
||
internal fun User.address(initAddress: Address.() -> Unit): User { | ||
TODO("Implement me!!!") | ||
val address = Address() |
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) } |
Well done, it seems your feeling for the Kotlin programming approach really starts to kick in! |
9db668d
to
189c667
Compare
|
||
for(char in expression) { | ||
when(char) { | ||
in openBrackets -> brackets.add(char) |
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.
The solution is correct. Readability can be improved by using the map as a data structure.
} | ||
|
||
fun main() { | ||
val expressionsToClustersCatalog = mapOf( | ||
"()(" to listOf("()"), |
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.
This actually should return the empty list since one cluster isn't closed.
return when { | ||
cMin == cMax -> "YES" | ||
cMax - cMin == 1 && occurrences.count { it == cMax } == 1 -> "YES" | ||
cMin == 1 && occurrences.count { it == cMin } == 1 && occurrences.count { it == cMax } == occurrences.size - 1 -> "YES" |
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.
If I'm not mistaken, your solution isn't fully correct.
for the test case as
, your function would return YES
even though the expected result should be NO
.
} | ||
|
||
/** | ||
* Subtask 2: | ||
* Find all the clients who completed at least the given number of trips. | ||
*/ | ||
internal fun TaxiPark.findFaithfulPassengers(minTrips: Int): Set<Passenger> { | ||
TODO("Implement me!!!") | ||
//test ovde ne prolazi, nisam siguran zasto | ||
return this.trips |
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.
The solution seems correct; the issue was in the test I pushed afterward.
Please sync with the latest main
branch state.
.map { it.passengers } | ||
.forEach { | ||
it.forEach { | ||
allPassengers.add(it) | ||
} | ||
} |
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.
Instead of this you can simply use flatmap
which will convert list of lists to list.
return passengersMap.filterValues { discounts -> | ||
val withDiscount = discounts.count { it } | ||
val noDiscount = discounts.size - withDiscount | ||
withDiscount > noDiscount |
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.
Well done! :)
6106c56
to
8296ece
Compare
Rešeni zadaci sa prvih i drugih praktičnih vežbi i kreiranje pull request-a.