-
Notifications
You must be signed in to change notification settings - Fork 12
Finished exercise 7 #4
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
78b76b4
60b3c4e
0c78f9d
8d25698
19d1051
a4776fb
07440fd
18ec279
4e93b98
e453625
88016b2
7c6a7e2
8bdd093
1fc52dd
d5a54ce
61d298b
aec1727
79ca3ef
3b67293
9624d88
c95e5ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1 @@ | ||
# Kotlin Programming Language Course - Faculty of Sciences, Novi Sad | ||
|
||
Welcome to the Kotlin Programming Language Course at the Faculty of Sciences, Novi Sad! | ||
|
||
## Project Overview | ||
|
||
This project contains a set of exercises designed to help you learn Kotlin programming concepts. Each exercise is provided with instructions and corresponding Kotlin files where you can implement your solutions. | ||
|
||
## How to Load Project from GitHub in IntelliJ IDEA | ||
|
||
### 1. Clone the Repository | ||
|
||
- Open IntelliJ IDEA. | ||
- Go to `File` > `New` > `Project from Version Control` > `Git`. | ||
- In the `URL` field, enter the URL of the GitHub repository: https://github.com/vuksa/kotlin-programming-language-course.git | ||
- Click `Clone` to clone the repository to your local machine. | ||
bojan ludajic |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package common | ||
|
||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.io.path.readLines | ||
import kotlin.io.path.toPath | ||
|
||
object FileReader { | ||
/** | ||
* Reads the contents of a file located at the specified path. | ||
* | ||
* @param path The path of the file to read. | ||
* @return A list of strings representing the lines of the file. | ||
* @throws NullPointerException if the resource at the specified path is null. | ||
*/ | ||
fun readFileInResources(path: String): List<String> { | ||
val normalizedPath = path.takeIf { it.startsWith("/") } ?: "/$path" | ||
return requireNotNull(this.javaClass.getResource(normalizedPath.toString())?.toURI()?.toPath()) { | ||
"Unresolved path." | ||
}.readLines() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,7 +20,9 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference | |||||
*/ | ||||||
|
||||||
internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> { | ||||||
TODO("Implement me!!") | ||||||
return Pair( | ||||||
this.sorted().get(this.lastIndex), this.sorted().get(this.lastIndex-1) | ||||||
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 is not a functional approach :) The idea of the functional approach is that you compose function calls that operate on data to get the desired result. Functional approach would be following:
Suggested change
In this case, we are using the result of the |
||||||
) | ||||||
} | ||||||
|
||||||
fun main() { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,24 +17,29 @@ 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 Pair( | ||
this.sorted().get(this.size-1), this.sorted().get(0) | ||
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, but it is written in Java-style code. Please consider my suggestion above to implement it in a Kotlin, functional style. :) Also, |
||
) | ||
|
||
|
||
// var resultPair: Pair<Int, Int>? = null | ||
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. Please clean up the code before pushing the changes. |
||
// 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!! | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,27 +59,35 @@ internal val countries = listOf( | |
*/ | ||
|
||
internal fun List<Country>.findCountryWithBiggestTotalArea(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { it.totalAreaInSquareKilometers } | ||
} | ||
|
||
internal fun List<Country>.findCountryWithBiggestPopulation(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { it.population } | ||
} | ||
|
||
internal fun List<Country>.findCountryWithHighestPopulationDensity(): Country { | ||
TODO("Implement me!!!") | ||
return countries.maxBy { | ||
it.population/it.totalAreaInSquareKilometers | ||
} | ||
} | ||
|
||
internal fun List<Country>.findCountryWithLowestPopulationDensity(): Country { | ||
TODO("Implement me!!!") | ||
return countries.minBy { | ||
it.population/it.totalAreaInSquareKilometers | ||
} | ||
} | ||
|
||
internal fun List<Country>.findLanguageSpokenInMostCountries(): String { | ||
TODO("Implement me!!!") | ||
return countries.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. Very nice solution! |
||
.groupingBy { it } | ||
.eachCount() | ||
.maxBy { it.value } | ||
.key | ||
} | ||
|
||
internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> { | ||
TODO("Implement me!!!") | ||
return countries.filter { country -> country.languages.contains(language) } | ||
} | ||
|
||
|
||
|
@@ -88,7 +96,7 @@ fun main() { | |
println("Country with a biggest population is a ${countries.findCountryWithBiggestPopulation().name}") | ||
println("Country with a biggest population density is a ${countries.findCountryWithHighestPopulationDensity().name}") | ||
println("Country with a lowest population density is a ${countries.findCountryWithLowestPopulationDensity().name}") | ||
println("Language spoken in most countries is a ${countries.findLanguageSpokenInMostCountries()}") | ||
println("Language spoken in most countries is ${countries.findLanguageSpokenInMostCountries()}") | ||
val countriesThatSpeakEnglish = countries.filterCountriesThatSpeakLanguage("English") | ||
println("Countries that speak English language are ${countriesThatSpeakEnglish.joinToString { it.name }}") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,11 +49,15 @@ internal data class Address( | |
*/ | ||
|
||
internal fun user(initUser: User.() -> Unit): User { | ||
TODO("Implement me!!!") | ||
val user = User() | ||
return user.apply(initUser) | ||
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. nit: this can be a one-line expression :) |
||
} | ||
|
||
internal fun User.address(initAddress: Address.() -> Unit): User { | ||
TODO("Implement me!!!") | ||
internal fun User.address (initAddress: Address.() -> Unit): User { | ||
val adr = Address() | ||
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. That's it! Why not using |
||
adr.initAddress() | ||
this.address = adr | ||
return this | ||
} | ||
|
||
fun main() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
package exercise3.task1 | ||
import java.util.Stack | ||
|
||
/** | ||
* Task1: Balanced Brackets (Parentheses) Problem | ||
|
@@ -26,9 +27,32 @@ package exercise3.task1 | |
|
||
|
||
internal fun isExpressionBalanced(expression: String): Boolean { | ||
TODO("Implement me!!!") | ||
val chars = Stack<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. 👏🏻 |
||
val opening = "({[" | ||
val closing = ")}]" | ||
|
||
for(char in expression) { | ||
when { | ||
char in opening -> chars.add(char) | ||
char in closing -> { | ||
if(chars.isEmpty() || !valid(chars.lastElement(), char)) { | ||
return false | ||
} | ||
chars.removeLast() | ||
} | ||
} | ||
} | ||
return chars.isEmpty() | ||
} | ||
|
||
fun valid(opening: Char, closing: Char): Boolean { | ||
return (opening == '(' && closing == ')') || | ||
(opening == '[' && closing == ']') || | ||
(opening == '{' && closing == '}') | ||
} | ||
|
||
|
||
|
||
fun main() { | ||
val expressions = listOf( | ||
"{[()]}" to true, | ||
|
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.
There is no need to
sort
the array twice. Please name the variables in English, as that is a widely accepted convention.