我对检查钥匙有一个评估,如下所示:
为了检测数字标识符中的错误,通常会在该标识符中添加一个检查键。
实现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,也就是说实现ComputeTechkDigit(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
这是正确的。
所以我也想改进它。
你有什么想法吗?
1条答案
按热度按时间iaqfqrcu1#
您描述了问题:
但我的问题是关于代码冗余,我的意思是,我在sumevend和sumoddd方法中重复相同的代码行
很好!正如您之前所写的,这两种方法之间只有一个区别(我使用java代码,您可以将其转换为javascript—我希望这是可以的):当您比较
if (c % 2 == 0) {
及if (c % 2 == 1) {
唯一的区别是0
vs。1
. 首先,我想到了一个关于旗帜的争论,比如calcForOdd
但我了解到,就干净代码而言,这种方式并不好(“干净代码解释-初学者干净代码实用介绍”,作者:yiğit kemal erinçon freecodecamp.org)(其他来源说,它就像java中堆栈溢出时的标志参数一样正常)。因此,我走了这条路:现在,您可以在两个机会中进行选择:
直接调用函数(删除旧函数
sumEvenD
及sumOddD
)你调用旧函数,它们调用新函数。
这是opportunity 2的代码:
一个小提示:您可以编写if并在
computeCheckDigit
. 这样地:而不是
是的,我的输出是
3
也我希望我能帮助你。