Skip to content

Solutions for exercises No.3 #8

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 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/kotlin/exercise3/task1/BalancedBrackets.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package exercise3.task1

import java.util.*

/**
* Task1: Balanced Brackets (Parentheses) Problem
*
Expand All @@ -24,9 +26,21 @@ package exercise3.task1
*
*/

val brackets: Map<Char, Char> = mapOf(']' to '[', '}' to '{', ')' to '(')

internal fun isExpressionBalanced(expression: String): Boolean {
TODO("Implement me!!!")
fun isExpressionBalanced(expression: String): Boolean {
val stack = LinkedList<Char>()
expression.forEach {
when (it) {
in brackets.values -> stack.push(it)
Copy link
Owner

Choose a reason for hiding this comment

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

This is the correct implementation. 👏🏻

One potential nitpick here is that you are creating a new list of values and keys on every item you iterate, which can be improved if we assigned brackets.values and brackets.keys to variables before we start iterating over the chars in the expression.

in brackets.keys -> {
if (stack.isEmpty() || brackets[it] != stack.pop()) {
return false
}
}
}
}
return stack.isEmpty()
}

fun main() {
Expand Down
25 changes: 24 additions & 1 deletion src/main/kotlin/exercise3/task2/ParenthesesClusters.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package exercise3.task2

import exercise3.task1.brackets
import java.util.*

/**
* Task 2: Split Expression To Parentheses Clusters
*
Expand All @@ -24,7 +27,27 @@ package exercise3.task2
*/

internal fun String.splitToBracketsClusters(): List<String> {
TODO("Implement me!!!")
val stack = LinkedList<Char>()
val returnListOfBrackets = mutableListOf<String>()

var currBalancedBrackets = ""
this.forEach {
currBalancedBrackets += it
when (it) {
in brackets.values -> stack.push(it)
in brackets.keys -> {
if (stack.isEmpty() || brackets[it] != stack.pop()) {
return listOf()
}
}
}
if (stack.isEmpty()) {
returnListOfBrackets.add(currBalancedBrackets)
currBalancedBrackets = ""
}
}
if (!stack.isEmpty()) return listOf()
return returnListOfBrackets
}

fun main() {
Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/exercise3/task3/SherlockValidatesString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ package exercise3.task3
*/

internal fun isSherlockValid(s: String): String {
TODO("Implement me!!!")
val freqOfFreqMap: Map<Int, Int> = s.groupingBy { it }.eachCount().values.groupingBy { it }.eachCount()

return when (freqOfFreqMap.size) {
2 -> {
val (minFrequency, maxFrequency) = freqOfFreqMap.keys.sorted()
if ((freqOfFreqMap[minFrequency] == 1 && minFrequency == 1) || (freqOfFreqMap[maxFrequency] == 1 && maxFrequency - minFrequency == 1)) "YES" else "NO"
}
1 -> "YES"
else -> "NO"
}
}

fun main() {
Expand All @@ -40,8 +49,11 @@ fun main() {
val errorMessageFactory = { answer: String -> if (answer == "YES") "is valid" else "is not valid" }

require(expectedIsValid == actualIsValid) {
"String \"$string\" is ${errorMessageFactory(expectedIsValid)}," +
" but actual value was ${errorMessageFactory(actualIsValid)}."
"String \"$string\" is ${errorMessageFactory(expectedIsValid)}," + " but actual value was ${
errorMessageFactory(
actualIsValid
)
}."
}
}
}
28 changes: 24 additions & 4 deletions src/main/kotlin/exercise3/task4/TaxiParkTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,49 @@ package exercise3.task4
* Find all the drivers who performed no trips.
*/
internal fun TaxiPark.findFakeDrivers(): Set<Driver> {
TODO("Implement me!!!")
return this.allDrivers.minus(this.trips.map { it.driver }
.intersect(this.allDrivers))
Copy link
Owner

Choose a reason for hiding this comment

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

We do not need an intersect call here.

Its enough to subtract allDrivers - trips.map { it.drivers } to get the result.

}

/**
* 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!!!")
println(this.trips.flatMap { it.passengers }
Copy link
Owner

Choose a reason for hiding this comment

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

nit: clean up unnecessary println

.groupingBy { it }
.eachCount())

return this.trips.flatMap { it.passengers }
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! 👏🏻

.groupingBy { it }
.eachCount()
.filter { it.value >= minTrips }
.map { it.key }
.toSet()
}

/**
* Subtask 3:
* Find all the passengers, who were taken by a given driver more than once.
*/
internal fun TaxiPark.findFrequentPassengers(driver: Driver): Set<Passenger> {
TODO("Implement me!!!")
return this.trips
.filter { it.driver == driver }
.flatMap { it.passengers }
.groupingBy { it }
.eachCount()
.filter { it.value > 1 }
.map { it.key }
.toSet()
}

/**
* Subtask 4:
* Find the passengers who had a discount for the majority of their trips.
*/
internal fun TaxiPark.findSmartPassengers(): Set<Passenger> {
TODO("Implement me!!!")
return this.trips
.filter { it.discount != null }
.flatMap { it.passengers }
Copy link
Owner

Choose a reason for hiding this comment

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

Your solution returns a set of passengers who had at least one trip with the discount.
The assignment request is to return a list of passengers who had more than half of the trips with the discount.

.toSet()
}