Damm algorithm: Difference between revisions
Content added Content deleted
m (→{{header|Perl 6}}: add zkl hteader) |
(→{{header|zkl}}: added code) |
||
Line 67: | Line 67: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<lang zkl>fcn damm(digits){ // digits is something that supports an iterator of integers |
|||
var [const] tbl=Data(0,Int, // 10x10 byte bucket |
|||
0, 3, 1, 7, 5, 9, 8, 6, 4, 2, |
|||
7, 0, 9, 2, 1, 5, 4, 8, 6, 3, |
|||
4, 2, 0, 6, 8, 7, 1, 3, 5, 9, |
|||
1, 7, 5, 0, 9, 8, 3, 4, 2, 6, |
|||
6, 1, 2, 3, 0, 4, 5, 9, 7, 8, |
|||
3, 6, 7, 4, 2, 0, 9, 5, 8, 1, |
|||
5, 8, 6, 9, 7, 2, 0, 1, 3, 4, |
|||
8, 9, 4, 5, 3, 6, 2, 0, 1, 7, |
|||
9, 4, 3, 8, 6, 1, 7, 2, 0, 5, |
|||
2, 5, 8, 1, 4, 3, 6, 7, 9, 0); |
|||
0 == digits.reduce(fcn(interim,digit){ tbl[interim*10 + digit] },0) |
|||
}</lang> |
|||
<lang zkl>damm(List(5,7,2,4)).println(); // True |
|||
damm(Data(0,Int,5,7,2,7).howza(0)).println(); // stream bytes, False |
|||
damm((112946).split()).println(); // True</lang> |
|||
{{out}} |
|||
<pre> |
|||
True |
|||
False |
|||
True |
|||
</pre> |
Revision as of 23:43, 29 May 2017
Damm algorithm is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
The Damm algorithm is a checksum algorithm which detects all single digit errors and adjacent transposition errors. The task is to verify the checksum, stored as last digit of an input.
C
<lang c>#include <stdbool.h>
- include <stddef.h>
- include <stdio.h>
bool damm(unsigned char *input, size_t length) {
static const unsigned char table[10][10] = { {0, 3, 1, 7, 5, 9, 8, 6, 4, 2}, {7, 0, 9, 2, 1, 5, 4, 8, 6, 3}, {4, 2, 0, 6, 8, 7, 1, 3, 5, 9}, {1, 7, 5, 0, 9, 8, 3, 4, 2, 6}, {6, 1, 2, 3, 0, 4, 5, 9, 7, 8}, {3, 6, 7, 4, 2, 0, 9, 5, 8, 1}, {5, 8, 6, 9, 7, 2, 0, 1, 3, 4}, {8, 9, 4, 5, 3, 6, 2, 0, 1, 7}, {9, 4, 3, 8, 6, 1, 7, 2, 0, 5}, {2, 5, 8, 1, 4, 3, 6, 7, 9, 0}, }; unsigned char interim = 0; for (size_t i = 0; i < length; i++) { interim = table[interim][input[i]]; } return interim == 0;
}
int main() {
unsigned char input[4] = {5, 7, 2, 4}; puts(damm(input, 4) ? "Checksum correct" : "Checksum incorrect"); return 0;
}</lang>
Perl 6
<lang perl6>sub damm ( *@digits ) {
my @tbl = [0, 3, 1, 7, 5, 9, 8, 6, 4, 2], [7, 0, 9, 2, 1, 5, 4, 8, 6, 3], [4, 2, 0, 6, 8, 7, 1, 3, 5, 9], [1, 7, 5, 0, 9, 8, 3, 4, 2, 6], [6, 1, 2, 3, 0, 4, 5, 9, 7, 8], [3, 6, 7, 4, 2, 0, 9, 5, 8, 1], [5, 8, 6, 9, 7, 2, 0, 1, 3, 4], [8, 9, 4, 5, 3, 6, 2, 0, 1, 7], [9, 4, 3, 8, 6, 1, 7, 2, 0, 5], [2, 5, 8, 1, 4, 3, 6, 7, 9, 0]; my $row = 0; for @digits -> $col { $row = @tbl[$row][$col] } not $row
}
- Testing
for 5724, 5727, 112946 {
say "$_:\tChecksum digit { damm( $_.comb ) ?? !! 'in' }correct."
}</lang>
- Output:
5724: Checksum digit correct. 5727: Checksum digit incorrect. 112946: Checksum digit correct.
zkl
<lang zkl>fcn damm(digits){ // digits is something that supports an iterator of integers
var [const] tbl=Data(0,Int, // 10x10 byte bucket 0, 3, 1, 7, 5, 9, 8, 6, 4, 2, 7, 0, 9, 2, 1, 5, 4, 8, 6, 3, 4, 2, 0, 6, 8, 7, 1, 3, 5, 9, 1, 7, 5, 0, 9, 8, 3, 4, 2, 6, 6, 1, 2, 3, 0, 4, 5, 9, 7, 8, 3, 6, 7, 4, 2, 0, 9, 5, 8, 1, 5, 8, 6, 9, 7, 2, 0, 1, 3, 4, 8, 9, 4, 5, 3, 6, 2, 0, 1, 7, 9, 4, 3, 8, 6, 1, 7, 2, 0, 5, 2, 5, 8, 1, 4, 3, 6, 7, 9, 0); 0 == digits.reduce(fcn(interim,digit){ tbl[interim*10 + digit] },0)
}</lang> <lang zkl>damm(List(5,7,2,4)).println(); // True damm(Data(0,Int,5,7,2,7).howza(0)).println(); // stream bytes, False damm((112946).split()).println(); // True</lang>
- Output:
True False True