首页 > 解决方案 > 无法弄清楚 Kata Direction Reduction 问题错误

问题描述

我正在研究代码战中的方向缩减问题,但我无法弄清楚它给我的错误。我知道有类似的情况,但是当我在 Visual Studio Code 上测试我的代码时,它可以完美运行,所以我不确定为什么 codewars 会给我这个错误。我得到的错误是: "NORTH","SOUTH","SOUTH","EAST","WEST","NORTH": array lengths different, expected.length=0 actual.length=6

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

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]);
    }
}
}

标签: javaarrays

解决方案


我发现了四个错误。

1) "NORTH","SOUTH","SOUTH","EAST","WEST","NORTH" 的情况应该回到你开始的地方,所以数组长度应该是 0,正如 Codewars 所要求的那样。为了让它工作,我摆脱了你的两个方向计数都是 0 的特殊情况,并让你的 else 情况通过添加 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;
}`

推荐阅读