Skip to content

Commit

Permalink
All errors are fixed (Attempt Kotlin-Polytech#2).
Browse files Browse the repository at this point in the history
  • Loading branch information
Iksburg committed Oct 23, 2020
1 parent af2a598 commit 7d7e1ad
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/lesson5/task1/Map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fun buildWordSet(text: List<String>): MutableSet<String> {
fun buildGrades(grades: Map<String, Int>): MutableMap<Int, List<String>> {
val result = mutableMapOf<Int, List<String>>()
for ((key, value) in grades) {
result[value] = result[value]?.plus(listOf(key)) ?: listOf(key)
result[value] = (result[value] ?: emptyList()) + key
}
return result
}
Expand Down Expand Up @@ -156,8 +156,9 @@ fun subtractOf(a: MutableMap<String, String>, b: Map<String, String>) {
*/
fun whoAreInBoth(a: List<String>, b: List<String>): List<String> {
val duplicateNames = mutableSetOf<String>()
val secondList = b.toSet()
for (element in a.toSet()) {
if (element in b.toSet()) duplicateNames.add(element)
if (element in secondList) duplicateNames.add(element)
}
return duplicateNames.toList()
}
Expand Down Expand Up @@ -209,8 +210,8 @@ fun averageStockPrice(stockPrices: List<Pair<String, Double>>): Map<String, Doub
val averageCost = mutableMapOf<String, Double>()
val count = mutableMapOf<String, Int>()
for ((key, value) in stockPrices) {
averageCost[key] = averageCost[key]?.plus(value) ?: value
count[key] = count[key]?.plus(1) ?: 1
averageCost[key] = (averageCost[key] ?: value) + value
count[key] = (count[key] ?: 1) + 1
}
for ((key) in averageCost) {
val sharePrice = averageCost[key]
Expand Down Expand Up @@ -343,11 +344,10 @@ fun findSumOfTwo(list: List<Int>, number: Int): Pair<Int, Int> {
var result = Pair(-1, -1)
val indexedList = mutableMapOf<Int, Int>()
for (i in list.indices) {
indexedList[i] = list[i]
}
for ((key, value) in indexedList) {
if (indexedList.containsValue(number - value) && key != list.indexOf(number - value)) {
result = Pair(key, list.indexOf(number - value))
indexedList[list[i]] = i
if (indexedList.contains(number - list[i]) && i != indexedList[number - list[i]]) {
val secondNumber = indexedList[number - list[i]]
if (secondNumber != null) result = Pair(i, secondNumber)
break
}
}
Expand Down Expand Up @@ -375,4 +375,4 @@ fun findSumOfTwo(list: List<Int>, number: Int): Pair<Int, Int> {
* 450
* ) -> emptySet()
*/
fun bagPacking(treasures: Map<String, Pair<Int, Int>>, capacity: Int): Set<String> = TODO()
fun bagPacking(treasures: Map<String, Pair<Int, Int>>, capacity: Int): Set<String> = TODO()

0 comments on commit 7d7e1ad

Please sign in to comment.