提问人:DDisciple 提问时间:10/4/2020 最后编辑:DDisciple 更新时间:10/4/2020 访问量:1044
对列表中的货币对进行计数
counting pairs in a list
问:
我最近开始使用 HackerRank,我正在尝试“按匹配销售”。我已经找到了一个在利用 Kotlin 的函数编程功能方面感到满意的解决方案。但是,我没有得到预期的答案......
问题摘要:
给定一个数组: ->查找并返回对总数。
即:
输入 -> [10, 20, 20, 10, 10, 30, 50, 10, 20]
对数 -> 3
以下是我的代码和一些解释它的注释:
fun sockMerchant(n: Int, pile: Array<Int>): Int{
var count = 0
mutableMapOf<Int, Int>().withDefault { 0 }.apply {
// the [attempted] logic behind this piece of code here is
// that as we iterate through the list, the 'getOrPut()'
// function will return either the value for the given [key]
// or throw an exception if there is no such key
// in the map. However, since our map was created by
// [withDefault], the function resorts to its `defaultValue` <-> 0
// instead of throwing an exception.
for (e in values) {
// this simplifies our code and inserts a zero [+1] where needed.
// if (key exists)
// // return its associated value and MOD it:
// case: even -> increment counter
// else -> do nothing
// else if (key dne)
// // insert default value <-> [0] + 1
// ....
// ....
// ....
if (getOrPut(e, { getValue(e) + 1 } ) % 2 == 0) count++
}
}
return count
}
fun main(args: Array<String>) {
val scan = Scanner(System.`in`)
val n = scan.nextLine().trim().toInt()
val ar = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()
val result = sockMerchant(n, ar)
println(result)
}
-- 任何帮助或提示都会在这里大有帮助:)
答:
1赞
Jonathan Schoreels
10/4/2020
#1
我稍微修改了一下,使其更容易测试,但这是修复:
import java.util.*
fun sockMerchant(n: Int, pile: Array<Int>): Int{
var count = 0
mutableMapOf<Int, Int>().withDefault { 0 }.apply {
// the [attempted] logic behind this piece of code here is
// that as we iterate through the list, the 'getOrPut()'
// function will return either the value for the given [key]
// or throw an exception if there is no such key
// in the map. However, since our map was created by
// [withDefault], the function resorts to its `defaultValue` <-> 0
// instead of throwing an exception.
for (e in pile) {
// this simplifies our code and inserts a zero [+1] where needed.
// if (key exists)
// // return its associated value and MOD it:
// case: even -> increment counter
// else -> do nothing
// else if (key dne)
// // insert default value <-> [0] + 1
// ....
// ....
// ....
println(e)
put(e, getValue(e) + 1)
if (getValue(e) % 2 == 0) count++
println(entries)
}
}
return count
}
val n = 5
val ar = "10 10 10 10 20 20 30 40".split(" ").map{ it.trim().toInt() }.toTypedArray()
val result = sockMerchant(n, ar)
println(result)
输出:
10
[10=1]
10
[10=2]
10
[10=3]
10
[10=4]
20
[10=4, 20=1]
20
[10=4, 20=2]
30
[10=4, 20=2, 30=1]
40
[10=4, 20=2, 30=1, 40=1]
3
Pair.kts:3:18: warning: parameter 'n' is never used
fun sockMerchant(n: Int, pile: Array<Int>): Int{
^
Process finished with exit code 0
解释:
- 你循环了“values”,它在开始时是空的,所以你从来没有对你的代码做任何事情
- 即使在循环时,您的增量逻辑也不会超过 1,因此从不满足条件,并且计数从未递增。
pile
但背后的主要原因是正确的。
2赞
Todd
10/4/2020
#2
我能够通过将数字组合在一起,获取结果列表并汇总每个数字包含多少对来做到这一点:
fun sockMerchant(n: Int, pile: Array<Int>): Int =
pile.groupBy { it }.values.sumBy { it.size / 2 }
在我们这样做之后,我们有了这个结构:pile.groupBy { it }
{10=[10, 10, 10, 10], 20=[20, 20, 20], 30=[30], 50=[50]}
我们取这些值,然后将它们的大小除以 2 相加。这会将半对向下舍入为 0,将整对向下舍入为 1。
注意:我不完全清楚在这种情况下的目的是什么。n
上一个:单击图像时移动图像的功能?
评论
n