Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

LX002
Copy link

@LX002 LX002 commented Mar 21, 2024

Rešeni zadaci sa prvih i drugih praktičnih vežbi i kreiranje pull request-a.

@@ -40,13 +40,25 @@ import java.util.Scanner
*/

internal fun calculateGrade(score: Int): Int {
TODO("Implement me!!!")
// require(score in 0..100) {
Copy link
Owner

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

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

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

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:
Copy link
Owner

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 :)

Comment on lines 83 to 84
.map{ it -> it.first }
.first()
Copy link
Owner

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:

Suggested change
.map{ it -> it.first }
.first()
.first()
.first

Copy link
Owner

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

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

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:

Suggested change
val user = User()
return User().apply(initUser)

}

internal fun User.address(initAddress: Address.() -> Unit): User {
TODO("Implement me!!!")
val address = Address()
Copy link
Owner

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.

Suggested change
val address = Address()
return apply { address = Address().apply(initAddress) }

@vuksa
Copy link
Owner

vuksa commented Mar 26, 2024

Well done, it seems your feeling for the Kotlin programming approach really starts to kick in!
Please pay attention to cleaning up the code from any commented-out code blocks before you push the changes.

@LX002 LX002 closed this Mar 30, 2024
@LX002 LX002 reopened this Mar 30, 2024
@vuksa vuksa added the exercise3 label Apr 6, 2024

for(char in expression) {
when(char) {
in openBrackets -> brackets.add(char)
Copy link
Owner

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("()"),
Copy link
Owner

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"
Copy link
Owner

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
Copy link
Owner

@vuksa vuksa Apr 6, 2024

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.

Comment on lines +41 to +46
.map { it.passengers }
.forEach {
it.forEach {
allPassengers.add(it)
}
}
Copy link
Owner

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
Copy link
Owner

Choose a reason for hiding this comment

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

Well done! :)

@vuksa vuksa force-pushed the main branch 2 times, most recently from 6106c56 to 8296ece Compare May 20, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants