Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
80
Task/Verhoeff-algorithm/JavaScript/verhoeff-algorithm.js
Normal file
80
Task/Verhoeff-algorithm/JavaScript/verhoeff-algorithm.js
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
const multiplicationTable = [
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
||||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
||||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
||||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
||||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
||||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
||||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
||||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
||||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
||||
];
|
||||
|
||||
const inverse = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
|
||||
|
||||
const permutationTable = [
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
||||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
||||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
||||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
||||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
||||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
||||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
||||
];
|
||||
|
||||
|
||||
function verhoeffChecksum(number, doValidation, doDisplay) {
|
||||
if (doDisplay) {
|
||||
const calculationType = doValidation ? "Validation" : "Check digit";
|
||||
console.log(calculationType + " calculations for " + number + "\n");
|
||||
console.log(" i ni p[i, ni] c");
|
||||
console.log("-------------------");
|
||||
}
|
||||
|
||||
if (!doValidation) {
|
||||
number += "0";
|
||||
}
|
||||
|
||||
let c = 0;
|
||||
const le = number.length - 1;
|
||||
|
||||
for (let i = le; i >= 0; i--) {
|
||||
const ni = parseInt(number.charAt(i));
|
||||
const pi = permutationTable[(le - i) % 8][ni];
|
||||
c = multiplicationTable[c][pi];
|
||||
|
||||
if (doDisplay) {
|
||||
console.log(`${String(le - i).padStart(2)}${String(ni).padStart(3)}${String(pi).padStart(8)}${String(c).padStart(6)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
if (doDisplay && !doValidation) {
|
||||
console.log("inverse[" + c + "] = " + inverse[c] + "\n");
|
||||
}
|
||||
|
||||
return doValidation ? c === 0 : inverse[c];
|
||||
}
|
||||
|
||||
|
||||
function main() {
|
||||
const tests = [
|
||||
["236", true],
|
||||
["12345", true],
|
||||
["123456789012", false],
|
||||
];
|
||||
|
||||
for (const test of tests) {
|
||||
let object = verhoeffChecksum(test[0], false, test[1]);
|
||||
console.log("The check digit for " + test[0] + " is " + object + "\n");
|
||||
|
||||
for (const number of [test[0] + String(object), test[0] + "9"]) {
|
||||
object = verhoeffChecksum(number, true, test[1]);
|
||||
const result = object ? "correct" : "incorrect";
|
||||
console.log("The validation for " + number + " is " + result + ".\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
98
Task/Verhoeff-algorithm/Rust/verhoeff-algorithm.rs
Normal file
98
Task/Verhoeff-algorithm/Rust/verhoeff-algorithm.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
const MULTIPLICATION_TABLE: [[i32; 10]; 10] = [
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
||||
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
||||
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
||||
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
||||
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
||||
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
||||
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
||||
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
||||
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
||||
];
|
||||
|
||||
const INVERSE: [i32; 10] = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9];
|
||||
|
||||
const PERMUTATION_TABLE: [[i32; 10]; 8] = [
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
||||
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
||||
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
||||
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
||||
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
||||
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
||||
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
||||
];
|
||||
|
||||
fn verhoeff_checksum(number: &str, do_validation: bool, do_display: bool) -> i32 {
|
||||
if do_display {
|
||||
let calculation_type = if do_validation {
|
||||
"Validation"
|
||||
} else {
|
||||
"Check digit"
|
||||
};
|
||||
println!("{} calculations for {}\n", calculation_type, number);
|
||||
println!(" i ni p[i, ni] c");
|
||||
println!("-------------------");
|
||||
}
|
||||
|
||||
let mut working_number = String::from(number);
|
||||
if !do_validation {
|
||||
working_number.push('0');
|
||||
}
|
||||
|
||||
let mut c = 0;
|
||||
let le = working_number.len() - 1;
|
||||
let chars: Vec<char> = working_number.chars().collect();
|
||||
|
||||
for i in (0..=le).rev() {
|
||||
let ni = chars[i].to_digit(10).unwrap() as i32;
|
||||
let pos = (le - i) % 8;
|
||||
let pi = PERMUTATION_TABLE[pos][ni as usize];
|
||||
c = MULTIPLICATION_TABLE[c as usize][pi as usize];
|
||||
|
||||
if do_display {
|
||||
println!(
|
||||
"{:2} {:2} {:2} {:2}\n",
|
||||
le - i,
|
||||
ni,
|
||||
pi,
|
||||
c
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if do_display && !do_validation {
|
||||
println!("inverse[{}] = {}\n", c, INVERSE[c as usize]);
|
||||
}
|
||||
|
||||
if do_validation {
|
||||
if c == 0 { 1 } else { 0 }
|
||||
} else {
|
||||
INVERSE[c as usize]
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let tests = [
|
||||
("123", true),
|
||||
("12345", true),
|
||||
("123456789012", false),
|
||||
];
|
||||
|
||||
for &(test_num, display) in tests.iter() {
|
||||
let digit = verhoeff_checksum(test_num, false, display);
|
||||
println!("The check digit for {} is {}\n", test_num, digit);
|
||||
|
||||
let numbers = [
|
||||
format!("{}{}", test_num, digit),
|
||||
format!("{}9", test_num),
|
||||
];
|
||||
|
||||
for number in numbers.iter() {
|
||||
let digit = verhoeff_checksum(number, true, display);
|
||||
let result = if digit == 1 { "correct" } else { "incorrect" };
|
||||
println!("The validation for {:?} is {:?}. ", number, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue