【发布时间】: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,这是预期的输出,
但我的问题是关于代码冗余,我的意思是,我在 sumEvenD 和 sumOddD 方法中重复同一行代码。
如何修复该代码以获得干净的代码(我的意思是没有冗余的代码)。 基本上,我需要帮助来改进我的代码,因为我的解决方案太重了。 你能帮帮我吗?
关于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