Skip to content

Exercise 5 & 6 #25

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 8 commits 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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/main/kotlin/exercise2/task1/FindPairOfHighestSum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference
*/

internal fun List<Int>.findHighestSumPair(): Pair<Int, Int> {
TODO("Implement me!!")

var first = this[0]
var second = Integer.MIN_VALUE
var nextNum = 0
for (i in 0 ..< this.size -1){
nextNum = i+1
if (this[nextNum] >= first ){
second = first
first = this[nextNum]
}
else if ((second < this[nextNum]) && second < first){
second = this[nextNum]
}
}
return first to second
}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference
*/

internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> {
TODO("Implement me!!")
return this.sortedDescending().let { it[0] to it[1] }
}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,8 @@ 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 this.sorted().let { it.first() to it[it.lastIndex] }
}

fun main() {
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/exercise2/task4/ProcessCountriesData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ 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 }.groupingBy { it }.eachCount().maxBy { it.value}.key
}

internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> {
TODO("Implement me!!!")
return this.filter { it.languages.contains(language) }
}


Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/exercise2/task5/CreateUserDSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ internal data class Address(
*/

internal fun user(initUser: User.() -> Unit): User {
TODO("Implement me!!!")
val newUser = User()
newUser.initUser()
return newUser
}

internal fun User.address(initAddress: Address.() -> Unit): User {
TODO("Implement me!!!")
val newAddress = Address()
newAddress.initAddress()
address = newAddress
return this
}

fun main() {
Expand Down
36 changes: 35 additions & 1 deletion 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.Stack

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


internal fun isExpressionBalanced(expression: String): Boolean {
TODO("Implement me!!!")

if (expression.length %2 == 1) return false

val pair1 = '(' to ')'
val pair2 = '[' to ']'
val pair3 = '{' to '}'
val pairs = arrayOf(pair1, pair2, pair3)

val open = listOf('(', '[', '{')
val stack = Stack<Char>()

for (i in expression){
if(i in open){
stack.push(i)
}
else {
if (stack.isEmpty()) return false
val tmpPair = stack.pop() to i
if (tmpPair !in pairs) return false
}
}
if(stack.isNotEmpty()) return false

return true
}

/*
{[()]()}

if i in isOpen add to stack
else is
else is i == sa prvim sa stacka

*/

fun main() {
val expressions = listOf(
"{[()]}" to true,
Expand Down
28 changes: 27 additions & 1 deletion src/main/kotlin/exercise3/task2/ParenthesesClusters.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package exercise3.task2

import java.util.*

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

internal fun String.splitToBracketsClusters(): List<String> {
TODO("Implement me!!!")

if (this.length %2 == 1) return listOf()

val open = listOf('(', '[', '{')
val stack = Stack<Char>()

var tempString = ""
val returnList = mutableListOf<String>()

for (i in this){
if(i in open){
stack.push(i)
tempString += i
}
else {
if (stack.isEmpty()) return listOf()
tempString += i
stack.pop()
}
if (stack.isEmpty()){
returnList += listOf(tempString)
tempString = ""
}
}
return returnList
}

fun main() {
Expand Down
40 changes: 38 additions & 2 deletions src/main/kotlin/exercise3/task3/SherlockValidatesString.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package exercise3.task3

import kotlin.math.abs

/**
* Task 3: Sherlock Validates the Words
*
Expand Down Expand Up @@ -29,11 +31,45 @@ package exercise3.task3
*/

internal fun isSherlockValid(s: String): String {
TODO("Implement me!!!")

val grpByChars = s.groupingBy { it }.eachCount()

val valuesOfGrouping = grpByChars.values.groupingBy { it }.eachCount()

if (valuesOfGrouping.size > 2) return "NO"
if (valuesOfGrouping.size == 1) return "YES"

val firstKey = valuesOfGrouping.keys.first()
val firstValue = valuesOfGrouping.values.first()
val secondKey = valuesOfGrouping.keys.last()
val secondValue = valuesOfGrouping.values.last()

if((secondValue == 1) and (secondKey== 1)) return "YES"
if((firstValue == 1) and (firstKey== 1)) return "YES"

val minusKeys = abs(firstKey - secondKey)
val minusValues = abs(firstValue - secondValue)

/*
Mislim da sa ovim ne pokrivam sve krajnje slucajeve
i da kod nije bas efikasan. Prolazi testove.
*/
if ( (minusKeys <=1) or (minusValues <= 1) ){

if( (firstKey > secondKey) and (firstValue > secondValue) ) return "NO"
if( (firstKey < secondKey) and (firstValue < secondValue) ) return "NO"
if(firstValue >=2 && secondValue >= 2) return "NO"

return "YES"
}
else{
return "NO"
}

}

fun main() {
val stringsToValidityCatalog = mapOf("abc" to "YES", "abcc" to "YES", "abccc" to "NO")
val stringsToValidityCatalog = mapOf("abc" to "YES", "abcc" to "YES", "aabbccccc" to "NO")

stringsToValidityCatalog.forEach { (string, expectedIsValid) ->
val actualIsValid = isSherlockValid(string)
Expand Down
18 changes: 14 additions & 4 deletions src/main/kotlin/exercise3/task4/TaxiParkTask.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exercise3.task4


/**
* Task 4: Taxi park
*
Expand All @@ -10,29 +11,38 @@ package exercise3.task4
* Find all the drivers who performed no trips.
*/
internal fun TaxiPark.findFakeDrivers(): Set<Driver> {
TODO("Implement me!!!")
return this.allDrivers.filter { driver -> trips.none { it.driver == driver } }.toSet()
}

/**
* 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!!!")
return this.allPassengers.filter { passenger ->
trips.count { it.passengers.contains(passenger) } >= minTrips }.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.allPassengers.filter { passenger ->
trips.count { (it.driver == driver) and (it.passengers.contains(passenger)) } > 1 }.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!!!")
/*
Kako bih mogao da recikliram kod "it.passengers.contains(passenger)",
da ne brojim dva puta?
*/
return this.allPassengers
.filter { passenger ->
trips.count { ( it.passengers.contains(passenger) ) and ( it.discount != null) } >
trips.count{ it.passengers.contains(passenger) } / 2 }.toSet()
}
Loading