gpt4 book ai didi

java - 无法找出 Kata Direction Reduction 问题错误

转载 作者:行者123 更新时间:2023-11-30 05:21:02 27 4
gpt4 key购买 nike

我正在研究代码 war 中的方向减少问题,但我无法弄清楚它给我带来的错误。我知道也有类似的情况,但是当我在 Visual Studio Code 上测试我的代码时,它工作得完美无缺,所以我不确定为什么 codewars 会给我这个错误。我收到的错误是:“NORTH”、“SOUTH”、“SOUTH”、“EAST”、“WEST”、“NORTH”:数组长度不同,expected.length=0actual.length=6

这是我的代码。请记住,codewars 会为您测试它,因此实际上不需要我的 main 方法:

import java.lang.*;

public class DirReduction {

public static String[] dirReduc(String[] arr) {
int directionNS = 0;
int directionEW = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] == "NORTH"){
directionNS++;
} else if(arr[i] == "SOUTH"){
directionNS--;
} else if(arr[i] == "EAST"){
directionEW++;
} else if(arr[i] == "WEST"){
directionEW--;
} else {
System.out.println("Invalid Direction.");
}
}


String[] reducArray;
if(directionNS == 0 && directionEW == 0){
reducArray = new String[arr.length];
System.arraycopy(arr, 0, reducArray, 0, arr.length);

} else {
reducArray = new String[Math.abs(directionNS + directionEW)];
if(directionNS > 0){
for(int i = 0; i < directionNS; i++){
reducArray[i] = "NORTH";
}
} else if(directionNS < 0){
for(int i = 0; i > directionNS; i--){
reducArray[i] = "SOUTH";
}
}


if(directionEW > 0){
for(int i = 0; i < directionEW; i++){
reducArray[i] = "EAST";
}
} else if(directionEW < 0){
for(int i = 0; i > directionEW; i--){
reducArray[i] = "WEST";
}
}
}
return reducArray;
}

public static void main(String[] args){
String[] a = {"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH","WEST"};

String[] result = dirReduc(a);
for(int i = 0; i < result.length; i++){
System.out.println(result[i]);
}
}
}

最佳答案

我发现了四个错误。

1) 案例 "NORTH","SOUTH","SOUTH","EAST","WEST","NORTH"应该回到你开始的地方,所以数组长度应该是 0,按照 Codewars 的要求。为了让它发挥作用,我摆脱了两个方向计数均为 0 的特殊情况,并通过添加 0 和 0 来获取数组大小,让其他情况来处理它。 [此错误是您问题中提到的错误]

2)您对数组大小的计算有点偏差。例如,对于“SOUTH”“EAST”,它计算的大小为 0,因为它们抵消了。相反,您需要对绝对值求和,而不是取总和的绝对值。

3) 缩减数组中的 EAST/WEST 从位置 0 开始,因此覆盖 NORTH/SOUTH。在执行这些操作之前,我确保偏移到数组中。

4) 如果您有 SOUTH、EAST、SOUTH 之类的内容,则在 for 循环中变为负值的策略将尝试写入负索引。我使用 Math.abs 保持积极的态度

这是生成的方法。

public static String[] dirReduc(String[] arr) {
int directionNS = 0;
int directionEW = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == "NORTH") {
directionNS++;
} else if (arr[i] == "SOUTH") {
directionNS--;
} else if (arr[i] == "EAST") {
directionEW++;
} else if (arr[i] == "WEST") {
directionEW--;
} else {
System.out.println("Invalid Direction.");
}
}


String[] reducArray;
//removed special case for ending up back where one started, that will be made a 0 length array as it should be
reducArray = new String[Math.abs(directionNS) + Math.abs(directionEW)]; //note have to take abs of each so one does not cancel out the other
if (directionNS > 0) {
for (int i = 0; i < directionNS; i++) {
reducArray[i] = "NORTH";
}
} else if (directionNS < 0) {
for(int i = 0; i < Math.abs(directionNS); i++){//keep the i's positive so they work in the array easily
reducArray[i] = "SOUTH";
}
}


if (directionEW > 0) {
for (int i = 0; i < directionEW; i++) {
reducArray[i + Math.abs(directionNS)] = "EAST"; //note have to start where north south left off
}
} else if (directionEW < 0) {
for(int i = 0; i < Math.abs(directionEW); i++){
reducArray[i + Math.abs(directionNS)] = "WEST";
}
}

return reducArray;
}`

关于java - 无法找出 Kata Direction Reduction 问题错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59571279/

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