gpt4 book ai didi

algorithm - 计算列表中的对

转载 作者:行者123 更新时间:2023-12-02 13:10:31 25 4
gpt4 key购买 nike

我最近开始使用HackerRank,并且正在尝试“按比赛销售”。就利用Kotlin的函数编程功能而言,我已经找到了满意的解决方案。但是,我没有得到预期的答案...
问题总结:

Given an Array:-> find and return the total number of pairs.


i.e:

input -> [10, 20, 20, 10, 10, 30, 50, 10, 20]

number of pairs -> 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)
}
-
任何帮助或提示将在这里大有帮助:)

最佳答案

我对其进行了一些修改,使其更易于测试,但这是修复方法:

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
说明:
  • 您遍历了“值”,该值在开始时为空,因此您从未对代码
  • 做任何操作
  • 即使循环遍历pile,您的增量逻辑也不会超过1,因此条件从未满足且计数也从未增加。

  • 但是背后的主要理由是正确的。

    关于algorithm - 计算列表中的对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64195718/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com