Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
39
Task/Damm-algorithm/Ada/damm-algorithm.adb
Normal file
39
Task/Damm-algorithm/Ada/damm-algorithm.adb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Damm_Algorithm is
|
||||
|
||||
function Damm (Input : in String) return Boolean
|
||||
is
|
||||
subtype Digit is Character range '0' .. '9';
|
||||
|
||||
Table : constant array (Digit, Digit) of Digit :=
|
||||
(('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'));
|
||||
Intern : Digit := '0';
|
||||
begin
|
||||
for D of Input loop
|
||||
Intern := Table (Intern, D);
|
||||
end loop;
|
||||
return Intern = '0';
|
||||
end Damm;
|
||||
|
||||
procedure Put_Damm (Input : in String) is
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
Put_Line ("Damm of " & Input & " validates as " & Damm (Input)'Image);
|
||||
end Put_Damm;
|
||||
|
||||
begin
|
||||
Put_Damm ("5724");
|
||||
Put_Damm ("5727");
|
||||
Put_Damm ("112946");
|
||||
Put_Damm ("112949");
|
||||
end Damm_Algorithm;
|
||||
43
Task/Damm-algorithm/Agena/damm-algorithm.agena
Normal file
43
Task/Damm-algorithm/Agena/damm-algorithm.agena
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
scope # Damm Algorithm
|
||||
|
||||
# returns true if the check digit of s is correct according to the Damm algorithm,
|
||||
# false otherwise #
|
||||
local proc hasValidDammCheckDigit( s :: string ) :: boolean
|
||||
|
||||
local constant operationTable
|
||||
:= seq( (/ 0, 3, 1, 7, 5, 9, 8, 6, 4, 2 \) # as per wikipedia example
|
||||
, (/ 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 \)
|
||||
);
|
||||
local interimDigit := 0;
|
||||
for sPos to size s do
|
||||
local constant nextDigit := abs s[ sPos ] - abs "0";
|
||||
if 0 <= nextDigit and nextDigit <= 9 then
|
||||
interimDigit := operationTable[ interimDigit + 1, nextDigit + 1 ]
|
||||
else
|
||||
error( "Invalid Damm digit: [" & s[ sPos ] & "]" )
|
||||
fi
|
||||
od;
|
||||
return interimDigit = 0
|
||||
end;
|
||||
|
||||
local proc testDammAlgorithm( s :: string, expectedResult :: boolean )
|
||||
local constant isValid := hasValidDammCheckDigit( s );
|
||||
printf( "check digit of %s is %s%s\n", s
|
||||
, if isValid then "valid" else "invalid" fi
|
||||
, if isValid == expectedResult then "" else " *** NOT AS EXPECTED" fi
|
||||
)
|
||||
end;
|
||||
|
||||
# test cases
|
||||
testDammAlgorithm( "5724", true );
|
||||
testDammAlgorithm( "5727", false );
|
||||
testDammAlgorithm( "112946", true )
|
||||
end
|
||||
197
Task/Damm-algorithm/COBOL/damm-algorithm.cob
Normal file
197
Task/Damm-algorithm/COBOL/damm-algorithm.cob
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. DAMM-ALGORITHM.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
|
||||
*> Damm quasi-group table (10x10)
|
||||
01 DAMM-TABLE.
|
||||
05 ROW-0.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
05 ROW-1.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
05 ROW-2.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
05 ROW-3.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
05 ROW-4.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
05 ROW-5.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
05 ROW-6.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
05 ROW-7.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
05 ROW-8.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
05 ROW-9.
|
||||
10 FILLER PIC 9 VALUE 2.
|
||||
10 FILLER PIC 9 VALUE 5.
|
||||
10 FILLER PIC 9 VALUE 8.
|
||||
10 FILLER PIC 9 VALUE 1.
|
||||
10 FILLER PIC 9 VALUE 4.
|
||||
10 FILLER PIC 9 VALUE 3.
|
||||
10 FILLER PIC 9 VALUE 6.
|
||||
10 FILLER PIC 9 VALUE 7.
|
||||
10 FILLER PIC 9 VALUE 9.
|
||||
10 FILLER PIC 9 VALUE 0.
|
||||
|
||||
*> Redefine table as a 2D array for indexed access
|
||||
01 DAMM-TABLE-R REDEFINES DAMM-TABLE.
|
||||
05 DAMM-ROW OCCURS 10 TIMES.
|
||||
10 DAMM-CELL OCCURS 10 TIMES PIC 9.
|
||||
|
||||
*> Test numbers
|
||||
01 TEST-NUMBERS.
|
||||
05 TEST-NUM PIC 9(6) VALUE 005724.
|
||||
05 TEST-NUM PIC 9(6) VALUE 005727.
|
||||
05 TEST-NUM PIC 9(6) VALUE 112946.
|
||||
05 TEST-NUM PIC 9(6) VALUE 112949.
|
||||
|
||||
01 TEST-NUMBERS-R REDEFINES TEST-NUMBERS.
|
||||
05 TEST-ENTRY OCCURS 4 TIMES PIC 9(6).
|
||||
|
||||
*> Working variables
|
||||
01 WS-NUMBER-STR PIC X(6).
|
||||
01 WS-INTERIM PIC 9 VALUE 0.
|
||||
01 WS-DIGIT PIC 9.
|
||||
01 WS-ROW-IDX PIC 99.
|
||||
01 WS-COL-IDX PIC 99.
|
||||
01 WS-CHAR-POS PIC 9.
|
||||
01 WS-STR-LEN PIC 9.
|
||||
01 WS-LOOP-IDX PIC 9.
|
||||
01 WS-NUM-IDX PIC 9.
|
||||
01 WS-IS-VALID PIC X VALUE 'N'.
|
||||
01 WS-DISPLAY-NUM PIC Z(5)9.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
|
||||
MAIN-PARA.
|
||||
PERFORM VARYING WS-NUM-IDX FROM 1 BY 1
|
||||
UNTIL WS-NUM-IDX > 4
|
||||
|
||||
MOVE TEST-ENTRY(WS-NUM-IDX) TO WS-DISPLAY-NUM
|
||||
MOVE TEST-ENTRY(WS-NUM-IDX) TO WS-NUMBER-STR
|
||||
|
||||
*> Strip leading zeros to get actual digit string
|
||||
MOVE FUNCTION TRIM(WS-NUMBER-STR LEADING)
|
||||
TO WS-NUMBER-STR
|
||||
MOVE FUNCTION LENGTH(
|
||||
FUNCTION TRIM(WS-NUMBER-STR TRAILING))
|
||||
TO WS-STR-LEN
|
||||
|
||||
PERFORM DAMM-CHECK
|
||||
|
||||
IF WS-IS-VALID = 'Y'
|
||||
DISPLAY WS-DISPLAY-NUM " is valid"
|
||||
ELSE
|
||||
DISPLAY WS-DISPLAY-NUM " is invalid"
|
||||
END-IF
|
||||
|
||||
END-PERFORM
|
||||
|
||||
STOP RUN.
|
||||
|
||||
DAMM-CHECK.
|
||||
MOVE 0 TO WS-INTERIM
|
||||
|
||||
PERFORM VARYING WS-CHAR-POS FROM 1 BY 1
|
||||
UNTIL WS-CHAR-POS > WS-STR-LEN
|
||||
|
||||
MOVE WS-NUMBER-STR(WS-CHAR-POS:1) TO WS-DIGIT
|
||||
|
||||
*> Table is 1-indexed in COBOL, so add 1 to row/col
|
||||
COMPUTE WS-ROW-IDX = WS-INTERIM + 1
|
||||
COMPUTE WS-COL-IDX = WS-DIGIT + 1
|
||||
|
||||
MOVE DAMM-CELL(WS-ROW-IDX, WS-COL-IDX)
|
||||
TO WS-INTERIM
|
||||
|
||||
END-PERFORM
|
||||
|
||||
IF WS-INTERIM = 0
|
||||
MOVE 'Y' TO WS-IS-VALID
|
||||
ELSE
|
||||
MOVE 'N' TO WS-IS-VALID
|
||||
END-IF.
|
||||
21
Task/Damm-algorithm/Icon/damm-algorithm.icon
Normal file
21
Task/Damm-algorithm/Icon/damm-algorithm.icon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
procedure main(A)
|
||||
\A | stop("You must give one or more numeric values")
|
||||
every n := !A do write(n," is ",(damm(n),"valid")|"not valid")
|
||||
end
|
||||
|
||||
procedure damm(n)
|
||||
static grid
|
||||
initial grid := [[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]]
|
||||
interim := 0
|
||||
every d := !n do interim := grid[interim+1][d+1]
|
||||
return 0 = interim
|
||||
end
|
||||
42
Task/Damm-algorithm/Pluto/damm-algorithm.pluto
Normal file
42
Task/Damm-algorithm/Pluto/damm-algorithm.pluto
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
do -- Damm Algorithm
|
||||
|
||||
-- returns true if the check digit of s is correct according to the Damm algorithm,
|
||||
-- false otherwise #
|
||||
local function hasValidDammCheckDigit( s : string ) : boolean
|
||||
|
||||
local operationTable <const> = { { 0, 3, 1, 7, 5, 9, 8, 6, 4, 2 } -- as per wikipedia example
|
||||
, { 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 }
|
||||
}
|
||||
local interimDigit = 0
|
||||
for sPos = 1, # s do
|
||||
local nextDigit <const> = string.byte( s[ sPos ] ) - string.byte( "0" )
|
||||
if 0 <= nextDigit <= 9 then
|
||||
interimDigit = operationTable[ interimDigit + 1 ][ nextDigit + 1 ]
|
||||
else
|
||||
error( $"Invalid Damm digit: [{s[ sPos ]}]" )
|
||||
end
|
||||
end
|
||||
return interimDigit == 0
|
||||
end
|
||||
|
||||
local function testDammAlgorithm( s : string, expectedResult : boolean ) : void
|
||||
local isValid <const> = hasValidDammCheckDigit( s )
|
||||
print( $"check digit of {s} is "
|
||||
.. if isValid then "valid" else "invalid" end
|
||||
.. if isValid == expectedResult then "" else " *** NOT AS EXPECTED" end
|
||||
)
|
||||
end
|
||||
|
||||
-- test cases
|
||||
testDammAlgorithm( "5724", true )
|
||||
testDammAlgorithm( "5727", false )
|
||||
testDammAlgorithm( "112946", true )
|
||||
end
|
||||
25
Task/Damm-algorithm/PowerShell/damm-algorithm.ps1
Normal file
25
Task/Damm-algorithm/PowerShell/damm-algorithm.ps1
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
$table = (
|
||||
(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)
|
||||
)
|
||||
|
||||
function Test-Damm([string]$s) {
|
||||
$interim = 0
|
||||
foreach ($c in $s.ToCharArray()) {
|
||||
$interim = $table[$interim][[int]$c - [int][char]'0']
|
||||
}
|
||||
return $interim -eq 0
|
||||
}
|
||||
|
||||
foreach ($number in 5724, 5727, 112946, 112949) {
|
||||
$validity = if (Test-Damm $number) {'valid'} else {'invalid'}
|
||||
'{0,6} is {1}' -f $number, $validity
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue