【问题标题】:calculation of a check key in Java first and then in Javascript先在 Java 中计算校验键,然后在 Javascript 中计算
【发布时间】:2021-09-15 12:51:01
【问题描述】:

我有一个关于检查键的评估,如下:

为了检测数字标识符中的错误,通常会在此标识符中添加一个检查键。

实现 public static int computeCheckDigit (String IdentificationNumber) 函数,该函数将标识符(以字符串的形式)作为输入,并使用返回校验码以下算法:

  • 在偶数位置(位置 0、2、4 等)添加数字。
  • 将结果乘以三。
  • 将奇数位的数字之和加到这个数字上 (位置 1、3、5 等...)
  • 取此结果的最后一位;
  • 如果该数字不为 0,则从 10 中减去。
  • 否则,保留 0。
  • 返回结果图

(我们假设左边第一个数字的位置是0)

例子:

考虑标识符 39847:

  • 对偶数位求和:3 + 8 + 7 = 18
  • 乘以三:18 * 3 = 54
  • 将奇数位置的数字相加:54+ (9 + 4) = 67
  • 最后一位是7
  • 从 10 中减去 7:10 - 7 = 3

39847 的预期结果是 3

约束:

identificationNumber 的长度可以在 1 到 12 个字符之间变化。

重复相同的练习,但这次是在 javascript 中,也就是说 实现computeCheckDigit(identificationNumber)函数

所以我做了下面的代码:

//Java implementation of the algorithm
public class Solution {
    // Function to return the reverse of a number n
    static int reverse(int n) {
        int rev = 0;
        while (n != 0) {
            rev = (rev * 10) + (n % 10);
            n /= 10;
        }
        return rev;
    }
    //Function to find the sum of the even positioned digits in a number n
    private static int sumEvenD(int n) {
        n = reverse(n);
        int sum = 0, c = 0;
        while (n != 0) {
            if (c % 2 == 0) {
                sum += n % 10;
            }
            n /= 10;
            c++;
        }
        return sum;
    }
    //Function to find the sum of the odd positioned digits in a number n
    private static int sumOddD(int n) {
        n = reverse(n);
        int sum = 0, c = 0;
        while (n != 0) {
            if (c % 2 == 1) {
                sum += n % 10;
            }
            n /= 10;
            c++;
        }
        return sum;
    }
    public static int computeCheckDigit(String identifcationNumber) {
        Integer idNumber = Integer.valueOf(identifcationNumber);
        int evenD = sumEvenD(idNumber);
        int oddD = sumOddD(idNumber);
        int sumAlgo = evenD * 3 + oddD;
        int lastDigit = sumAlgo % 10;
        int checkDigit = 0;
        if (lastDigit != 0) {
            checkDigit = 10 - lastDigit;
        } else {
            return 0;
        }
        return checkDigit;
    }
    // Driver code
    public static void main(String args[]) {
        String idS = "39847";
        System.out.println(Solution.computeCheckDigit(idS));
    }
}

所以我的代码打印出3,这是预期的输出, 但我的问题是关于代码冗余,我的意思是,我在 sumEvenDsumOddD 方法中重复同一行代码。

如何修复该代码以获得干净的代码(我的意思是没有冗余的代码)。 基本上,我需要帮助来改进我的代码,因为我的解决方案太重了。 你能帮帮我吗?

关于javascript实现,我的解决方案是:

//JavaScript implementation of the approach

// Function to return the
// reverse of a number
function reverse(n) {
  let rev = 0;
  while (n != 0) {
    rev = (rev * 10) + (n % 10);
    n = Math.floor(n / 10);
  }
  return rev;
}

// Function to find the sum of the even
// positioned digits in a number
function sumEvenD(n) {
  n = reverse(n);
  let sumEven = 0, c = 0;
  while (n !== 0) {
    // If c is even number then it means
    // digit extracted is at even place
    if (c % 2 === 0) {
      sumEven += n % 10;
    }
    n = Math.floor(n / 10);
    c++;
  }
  return sumEven;
}

function sumOddD(n) {
  n = reverse(n);
  let sumOdd = 0, c = 0;
  while (n !== 0) {
    // If c is odd number then it means
    // digit extracted is at odd place
    if (c % 2 === 1) {
      sumOdd += n % 10;
    }
    n = Math.floor(n / 10);
    c++;
  }
  return sumOdd;
}

function computeCheckDigit(identificationNumber) {
  let evenD = sumEvenD(n);
  let oddD = sumOddD(n);
  let sumAlgo = evenD * 3 + oddD;
  let lastDigit = sumAlgo % 10;
  let checkDigit = 0;
  if (lastDigit !== 0) {
    checkDigit = 10 - lastDigit;
  } else {
    return 0;
  }
  return checkDigit;
}

let n = 39847;

console.log(computeCheckDigit(n));

预期的输出是3,这是正确的。

所以我也想改进它。

请问你有什么想法吗?

【问题讨论】:

    标签: javascript java string


    【解决方案1】:

    您描述了问题:

    但我的问题是关于代码冗余,我的意思是,我在 sumEvenD 和 sumOddD 方法中重复同一行代码

    很好!就像您之前写的那样,这两种方法之间只有一个区别(我使用 Java 代码,您可以将其转移到 JavaScript - 我希望这没问题):当您比较 if (c % 2 == 0) {if (c % 2 == 1) { 时,唯一的区别是01。首先我想到了一个像calcForOdd 这样的标志参数,但我了解到,这种方式在干净的代码方面不好("Clean Code Explained – A Practical Introduction to Clean Coding for Beginners" by Yiğit Kemal Erinç on freecodecamp.org(其他消息来源说它可以像Flag arguments in Java at Stack Overflow)。因此,我去了方式:

    /* 
     * Function to find the sum of the positioned digits
     * with the specified remainder at mod 2 in a number n.
     * 
     * remainder is the value, which the mod-operation
     * (number mod 2) should be equal to be considered
     * (e. g. remainder = 0, to consider all even numbers).
     */
    private static int sumD(int n, int remainder) {
        n = reverse(n);
        int sum = 0, c = 0;
        while (n != 0) {
            if (c % 2 == remainder) {
                sum += n % 10;
            }
            n /= 10;
            c++;
        }
        return sum;
    }
    

    现在您可以在两个机会之间进行选择:

    1. 你直接调用函数(删除旧函数sumEvenDsumOddD
    2. 您调用旧函数,它们调用新函数。

    这是机会 2 的代码:

    //Function to find the sum of the even positioned digits in a number n
    private static int sumEvenD(int n) {
        return sumD(n, 0);
    }
    //Function to find the sum of the odd positioned digits in a number n
    private static int sumOddD(int n) {
        return sumD(n, 1);
    }
    

    一点提示:您可以在computeCheckDigit 中编写更短的 if 和 return。像这样:

    // return the checkDigit
    if (lastDigit != 0) {
        return 10 - lastDigit; 
    } else {
        return 0;
    }
    

    而不是

    int checkDigit = 0;
    if (lastDigit != 0) {
            checkDigit = 10 - lastDigit;
    } else {
            return 0;
    }
    return checkDigit;
    

    是的,我的输出也是3

    希望能帮到你。

    【讨论】:

    • 感谢使用余数的函数的想法,例如当我选择机会 1 时,我将用 let evenD = sumD(n,0); let oddD = sumD(n,1); 替换下面的这两行是您的意思吗?
    • 是的,我就是这个意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多