gpt4 book ai didi

kotlin - 是否有更优雅或更短的方法将列表元素转换为 int、递增它并将其转换回字符串?

转载 作者:行者123 更新时间:2023-12-05 03:28:23 24 4
gpt4 key购买 nike

代码工作得很好,它只是很难看,特别是在我的情况下,我不仅必须这样做,而且还必须在 2d 列表中的上 3、下 3,所以它变得非常冗长。有没有更短的方法来做到这一点,也许使用“它”?

val nums = mutableListOf("0", "X", "0", "X", "0")

for (num in 0 until nums.size) {
if (nums[num] == "X") {
var a = nums[num - 1]
if ( nums[num - 1] != "X") nums[num - 1] = (nums[num - 1].toInt() + 1).toString()
if ( nums[num + 1] != "X") nums[num + 1] = (nums[num + 1].toInt() + 1).toString()
}
}

最佳答案

到目前为止,您的代码是完成它的最短方法,但也是最难理解和维护的方法,可能在编写后 2 个月。举个例子;您的代码目前适用于序列 ("0", "X", "0", "X", "0") 但不适用于 ("X", "0", "X", "0", "X); 它仍然是相同的序列,但只是在后者中有不同的排列。尝试让它适用于后者,您将需要大量时间,而且很可能会更改循环中的每一行。

从提供 it 的方法,最常见的 list.forEach,不允许您从 it block 中修改列表,因为迭代器会遇到 ConcurrentModificationException;但在这种情况下,列表需要在每次迭代中修改,因此除非您创建一个可以从 block 内修改的临时列表,否则这将不起作用,但是不,内存是有限的。

从编程原则中提取,如果您发现自己每次都需要相同的代码并且每次只修改一小部分,那么您需要的是一个函数,可能是函数重载。在这种情况下,创建一个接受列表nums、转换它并返回转换后的列表的函数将解决您的冗长问题;但为什么不呢,这就是 println 函数的工作原理。

Long Elegant Solution,通过函数缩短为一行:根据您的输入,做出以下假设:

  1. 列表中的“X”就像一个“魔数(Magic Number)”,用于文件 MIME 类型;即,一旦我们找到“X”,就将有效值递增到它的左右。
  2. 上面的“魔数(Magic Number)”的左侧或右侧可能存在一个字符串数字,如果它不是“X”,则 Int.toString() 不会抛出 NumberFormatException。
  3. ("0", "X", "0", "X", "0") 和 ("X", "0", "X", "0", "X) 被认为是有效输入,为什么不呢?

注释说明:

val nums = mutableListOf( "0", "X", "0", "X", "0")

// Let's see what is contained in [nums] before we transform it
println("\tNums Before: $nums")

/*
This keeps the valid range of indices available in [nums],
anything out of this range would produce an IndexOutOfBounds exception
*/
val numsRange = IntRange(0, nums.size - 1)
for (num in nums.withIndex()) {
val currentIndex = num.index // The current index in [nums] the loop is currently processing
val nextIndex = currentIndex + 1 // The next index in [nums] relative to [currentIndex]
val previousIndex = currentIndex - 1 // The previous index in [nums] relative to [currentIndex]

// The current value in [num], maybe "X" or "0"
val currentNum = num.value
/* Try getting the number to the right of [currentNum] in the list, [num], or null if it does not exist */
val nextNum = if (numsRange.contains(nextIndex)) nums[nextIndex] else null
/* Try getting the number to the left of [currentNum] in the list, [num], or null if it does not exist */
val previousNum = if (numsRange.contains(previousIndex)) nums[previousIndex] else null

if (currentNum == "X") {
// Found the magic number
if (nextNum != "X" && nextNum != null)
// Only increment the number to the right of current "X" if it exists
nums[nextIndex] = (nextNum.toInt() + 1).toString()
if (previousNum != "X" && previousNum != null)
// Only increment the number to the left of current "X" if it exists
nums[previousIndex] = (previousNum.toInt() + 1).toString()
}

}
// Let's see what is contained in [nums] after the transformation
println("\tNums After: $nums")

现在作为一个函数;为简洁起见删除评论:

fun increment(nums: MutableList<String>) {
val numsRange = IntRange(0, nums.size - 1)
for (num in nums.withIndex()) {
val currentIndex = num.index
val nextIndex = currentIndex + 1
val previousIndex = currentIndex - 1

val currentNum = num.value
val nextNum = if (numsRange.contains(nextIndex)) nums[nextIndex] else null
val previousNum = if (numsRange.contains(previousIndex)) nums[previousIndex] else null

if (currentNum == "X") {
if (nextNum != "X" && nextNum != null)
nums[nextIndex] = (nextNum.toInt() + 1).toString()
if (previousNum != "X" && previousNum != null)
nums[previousIndex] = (previousNum.toInt() + 1).toString()
}
}
}

让我们调用该函数 10 次,看看它在每次调用时对列表的影响:

 val numsFunc = mutableListOf("0", "X", "0", "X", "0")

for (i in 0..9){
println("loop: $i")
println("\tNums Func Before: $numsFunc")
increment(numsFunc)
println("\tNums Func After: $numsFunc")
}

输出:

loop: 0
Nums Func Before: [0, X, 0, X, 0]
Nums Func After: [1, X, 2, X, 1]
loop: 1
Nums Func Before: [1, X, 2, X, 1]
Nums Func After: [2, X, 4, X, 2]
loop: 2
Nums Func Before: [2, X, 4, X, 2]
Nums Func After: [3, X, 6, X, 3]
...
...

关于kotlin - 是否有更优雅或更短的方法将列表元素转换为 int、递增它并将其转换回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71274517/

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