-
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 all commits
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 |
---|---|---|
@@ -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 |
---|---|---|
|
@@ -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 } | ||
.first() | ||
.first | ||
} | ||
|
||
internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> { | ||
TODO("Implement me!!!") | ||
return this.filter { it.languages.contains(language) } | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,21 @@ package exercise3.task1 | |
|
||
|
||
internal fun isExpressionBalanced(expression: String): Boolean { | ||
TODO("Implement me!!!") | ||
val openBrackets = listOf('(', '[', '{') | ||
val closedBrackets = listOf(')', ']', '}') | ||
var brackets = mutableListOf<Char>() | ||
|
||
|
||
for(char in expression) { | ||
when(char) { | ||
in openBrackets -> brackets.add(char) | ||
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. The solution is correct. Readability can be improved by using the map as a data structure. |
||
')' -> if(brackets.isEmpty() || brackets.removeAt(brackets.size - 1) != '(') return false | ||
']' -> if(brackets.isEmpty() || brackets.removeAt(brackets.size - 1) != '[') return false | ||
'}' -> if(brackets.isEmpty() || brackets.removeAt(brackets.size - 1) != '{') return false | ||
} | ||
} | ||
|
||
return brackets.isEmpty() | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,35 @@ package exercise3.task2 | |
*/ | ||
|
||
internal fun String.splitToBracketsClusters(): List<String> { | ||
TODO("Implement me!!!") | ||
var clusters = mutableListOf<String>() | ||
var open = 0 | ||
var builder = StringBuilder() | ||
this.forEach { | ||
when { | ||
it == '(' -> { | ||
open++ | ||
builder.append('(') | ||
} | ||
it == ')' -> { | ||
open-- | ||
builder.append(')') | ||
} | ||
else -> throw IllegalArgumentException("Invalid character") | ||
} | ||
|
||
if(open == 0) { | ||
clusters.add(builder.toString()) | ||
builder = StringBuilder() | ||
} | ||
|
||
if(open < 0) return emptyList() | ||
} | ||
if(open != 0) return emptyList() else return clusters | ||
} | ||
|
||
fun main() { | ||
val expressionsToClustersCatalog = mapOf( | ||
"()(" to listOf("()"), | ||
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. This actually should return the empty list since one cluster isn't closed. |
||
"()()()" to listOf("()", "()", "()"), | ||
"((()))" to listOf("((()))"), | ||
"((()))(())()()(()())" to listOf("((()))", "(())", "()", "()", "(()())"), | ||
|
@@ -41,4 +65,6 @@ fun main() { | |
"Expression $expression should be split to $expression clusters, but actual value was $actualClusters." | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ package exercise3.task3 | |
* s = abcc | ||
* This is a valid string because we can remove one `c` and have `1` of each character in the remaining string. | ||
* | ||
* s = abccc | ||
* s = abcc | ||
* This string is not valid as we can only remove `1` occurrence of `c`. That leaves character frequencies of | ||
* {`a`: 1, `b`: 1, `c`: 2}. | ||
* ``` | ||
|
@@ -29,7 +29,19 @@ package exercise3.task3 | |
*/ | ||
|
||
internal fun isSherlockValid(s: String): String { | ||
TODO("Implement me!!!") | ||
require(s.length in 1..100000) { "String length must be in range from 1 to 10^5" } | ||
require(s.all{ it in 'a'..'z' }) { "String must only contain small letters (a..z)" } | ||
|
||
val occurrences = s.groupingBy { it }.eachCount().map { it.value } | ||
val cMin = occurrences.min() | ||
val cMax = occurrences.max() | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, your solution isn't fully correct. |
||
else -> "NO" | ||
} | ||
} | ||
|
||
fun main() { | ||
|
@@ -38,7 +50,6 @@ fun main() { | |
stringsToValidityCatalog.forEach { (string, expectedIsValid) -> | ||
val actualIsValid = isSherlockValid(string) | ||
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)}." | ||
|
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!