Last active
February 19, 2021 02:59
-
-
Save christianmeichtry/9348451 to your computer and use it in GitHub Desktop.
Calcul du chiffre-clé, modulo 10, récursif BVR Suisse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Calcul du chiffre-clé, modulo 10, récursif BVR Suisse | |
# Berechnung der Prüfziffer nach Modulo 10, rekursiv BESR Schweiz | |
# Calculating the Check Digits Using Module 10, Recursively | |
def modulo10_checksum(number) # Best to send a string, an integer with leading 0's will be interpreted as octal... | |
# table from https://www.credit-suisse.com/media/production/pb/docs/unternehmen/kmugrossunternehmen/besr_technische_dokumentation_fr.pdf | |
table = [ | |
[0,9,4,6,8,2,7,1,3,5], | |
[9,4,6,8,2,7,1,3,5,0], | |
[4,6,8,2,7,1,3,5,0,9], | |
[6,8,2,7,1,3,5,0,9,4], | |
[8,2,7,1,3,5,0,9,4,6], | |
[2,7,1,3,5,0,9,4,6,8], | |
[7,1,3,5,0,9,4,6,8,2], | |
[1,3,5,0,9,4,6,8,2,7], | |
[3,5,0,9,4,6,8,2,7,1], | |
[5,0,9,4,6,8,2,7,1,3] | |
] | |
numbers = number.to_s.chars.map(&:to_i) # Map integer to array of digits | |
report = 0 # Start at row 0 | |
numbers.each do |n| | |
report = table[report][n] # Traverse table for each digit | |
end | |
return (10-report) | |
end |
Est-ce que vous savez pourquoi on a choisi cette table particulière ?
Merci pour ces informations très utiles :)
Il est alors encore possible de simplifier un peu avec un tableau à une seule dimension:
def modulo10_checksum(number) # Best to send a string, an integer with leading 0's will be interpreted as octal
table = [0,9,4,6,8,2,7,1,3,5]
numbers = number.to_s.chars.map(&:to_i) # Map integer to array of digits
report = 0 # Start at row 0
numbers.each do |n|
report = table[(report+n)%10] # Traverse table for each digit
end
return (10-report)%10
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
La ligne 28 doit être modulo aussi: