WeixiangYan/CodeTransOcean huggingface dataset:
Task: Tokenize a string with escaping



C#: using System;
using System.Text;
using System.Collections.Generic;

public class TokenizeAStringWithEscaping
{
    public static void Main() {
        string testcase = "one^|uno||three^^^^|four^^^|^cuatro|";
        foreach (var token in testcase.Tokenize(separator: '|', escape: '^')) {
            Console.WriteLine(": " + token); 
        }
    }
}

public static class Extensions
{
    public static IEnumerable<string> Tokenize(this string input, char separator, char escape) {
        if (input == null) yield break;
        var buffer = new StringBuilder();
        bool escaping = false;
        foreach (char c in input) {
            if (escaping) {
                buffer.Append(c);
                escaping = false;
            } else if (c == escape) {
                escaping = true;
            } else if (c == separator) {
                yield return buffer.Flush();
            } else {
                buffer.Append(c);
            }
        }
        if (buffer.Length > 0 || input[input.Length-1] == separator) yield return buffer.Flush();
    }
    
    public static string Flush(this StringBuilder stringBuilder) {
        string result = stringBuilder.ToString();
        stringBuilder.Clear();
        return result;
    }
}








Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Tokenize a string with escaping


C++: #include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using namespace std;

vector<string> tokenize(const string& input, char seperator, char escape) {
    vector<string> output;
    string token;

    bool inEsc = false;
    for (char ch : input) {
        if (inEsc) {
            inEsc = false;
        } else if (ch == escape) {
            inEsc = true;
            continue;
        } else if (ch == seperator) {
            output.push_back(token);
            token = "";
            continue;
        }
        token += ch;
    }
    if (inEsc)
        throw new invalid_argument("Invalid terminal escape");

    output.push_back(token);
    return output;
}

int main() {
    string sample = "one^|uno||three^^^^|four^^^|^cuatro|";

    cout << sample << endl;
    cout << '[';
    for (auto t : tokenize(sample, '|', '^')) {
        cout << '"' << t << "\", ";
    }
    cout << ']' << endl;

    return 0;
}









Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Tokenize a string with escaping

C: #include <stdlib.h>
#include <stdio.h>

#define STR_DEMO "one^|uno||three^^^^|four^^^|^cuatro|"
#define SEP '|'
#define ESC '^'

typedef char* Str; 


unsigned int ElQ( const char *s, char sep, char esc );
Str *Tokenize( char *s, char sep, char esc, unsigned int *q );



int main() {
    char s[] = STR_DEMO;
    unsigned int i, q;

    Str *list = Tokenize( s, SEP, ESC, &q );

    if( list != NULL ) {
        printf( "\n Original string: %s\n\n", STR_DEMO );
        printf( " %d tokens:\n\n", q );

        for( i=0; i<q; ++i )
            printf( " %4d. %s\n", i+1, list[i] );

        free( list );
    }

    return 0;
}



unsigned int ElQ( const char *s, char sep, char esc ) {
    unsigned int q, e;
    const char *p;

    for( e=0, q=1, p=s; *p; ++p ) {
        if( *p == esc )
            e = !e;
        else if( *p == sep )
            q += !e;
        else e = 0;
    }

    return q;
}



Str *Tokenize( char *s, char sep, char esc, unsigned int *q ) {
    Str *list = NULL;

    *q = ElQ( s, sep, esc );
    list = malloc( *q * sizeof(Str) );

    if( list != NULL ) {
        unsigned int e, i;
        char *p;

        i = 0;
        list[i++] = s;

        for( e=0, p=s; *p; ++p ) {
            if( *p == esc ) {
                e = !e;
            }
            else if( *p == sep && !e ) {
                list[i++] = p+1;
                *p = '\0';
            }
            else {
                e = 0;
            }
        }
    }

    return list;
}










Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Tokenize a string with escaping





Java: import java.util.*;

public class TokenizeStringWithEscaping {

    public static void main(String[] args) {
        String sample = "one^|uno||three^^^^|four^^^|^cuatro|";
        char separator = '|';
        char escape = '^';

        System.out.println(sample);
        try {
            System.out.println(tokenizeString(sample, separator, escape));
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static List<String> tokenizeString(String s, char sep, char escape)
            throws Exception {
        List<String> tokens = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        boolean inEscape = false;
        for (char c : s.toCharArray()) {
            if (inEscape) {
                inEscape = false;
            } else if (c == escape) {
                inEscape = true;
                continue;
            } else if (c == sep) {
                tokens.add(sb.toString());
                sb.setLength(0);
                continue;
            }
            sb.append(c);
        }
        if (inEscape)
            throw new Exception("Invalid terminal escape");

        tokens.add(sb.toString());

        return tokens;
    }
}






Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Tokenize a string with escaping







Python: def token_with_escape(a, escape = '^', separator = '|'):
    
    result = []
    token = ''
    state = 0
    for c in a:
        if state == 0:
            if c == escape:
                state = 1
            elif c == separator:
                result.append(token)
                token = ''
            else:
                token += c
        elif state == 1:
            token += c
            state = 0
    result.append(token)
    return result




Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Tokenize a string with escaping








VB: Private Function tokenize(s As String, sep As String, esc As String) As Collection
    Dim ret As New Collection
    Dim this As String
    Dim skip As Boolean
     
    If Len(s) <> 0 Then
        For i = 1 To Len(s)
            si = Mid(s, i, 1)
            If skip Then
                this = this & si
                skip = False
            Else
                If si = esc Then
                    skip = True
                Else
                    If si = sep Then
                        ret.Add this
                        this = ""
                    Else
                        this = this & si
                    End If
                End If
            End If
        Next i
        ret.Add this
    End If
    Set tokenize = ret
End Function

Public Sub main()
    Dim out As Collection
    Set out = tokenize("one^|uno||three^^^^|four^^^|^cuatro|", "|", "^")
    Dim outstring() As String
    ReDim outstring(out.Count - 1)
    For i = 0 To out.Count - 1
        outstring(i) = out(i + 1)
    Next i
    Debug.Print Join(outstring, ", ")
End Sub



Fortran:       SUBROUTINE SPLIT(TEXT,SEP,ESC)	
       CHARACTER*(*) TEXT	
       CHARACTER*(1) SEP	
       CHARACTER*(1) ESC	
       CHARACTER*(LEN(TEXT)) TOKEN	
       INTEGER N	
       INTEGER I	
       INTEGER L	
       LOGICAL ESCAPING	
        N = 0		
        L = 0		
        ESCAPING = .FALSE.	
        DO I = 1,LEN(TEXT)	
          IF (ESCAPING) THEN	
            L = L + 1			
            TOKEN(L:L) = TEXT(I:I)	
            ESCAPING = .FALSE.		
           ELSE			
            IF (TEXT(I:I).EQ.ESC) THEN	
             ESCAPING = .TRUE.			
            ELSE IF (TEXT(I:I).EQ.SEP) THEN	
             CALL SPLOT				
            ELSE			
             L = L + 1				
             TOKEN(L:L) = TEXT(I:I)		
            END IF			
          END IF		
        END DO			
Completes on end-of-text with L > 0, or, if the last character had been SEP, a null token is deemed to be following.
        CALL SPLOT	
       CONTAINS	
        SUBROUTINE SPLOT	
         N = N + 1			
         WRITE (6,1) N,TOKEN(1:L)	
    1    FORMAT ("Token ",I0," >",A,"<")
         L = 0				
        END SUBROUTINE SPLOT	
      END SUBROUTINE SPLIT	

      PROGRAM POKE

      CALL SPLIT("one^|uno||three^^^^|four^^^|^cuatro|","|","^")

      END
Task: Damm algorithm



C#: using System;

namespace DammAlgorithm {
    class Program {
        static int[,] 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},
        };

        static bool Damm(string s) {
            int interim = 0;
            foreach (char c in s) {
                interim = table[interim, c - '0'];
            }
            return interim == 0;
        }

        static void Main(string[] args) {
            int[] numbers = { 5724, 5727, 112946, 112949 };
            foreach (int number in numbers) {
                bool isValid = Damm(number.ToString());
                if (isValid) {
                    Console.WriteLine("{0,6} is valid", number);
                }
                else {
                    Console.WriteLine("{0,6} is invalid", number);
                }
            }
        }
    }
}









Ada: 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;
Task: Damm algorithm

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;
}











Ada: 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;
Task: Damm algorithm


C++: #include <sstream>

const int TABLE[][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},
};

using std::string;
bool damm(string s) {
	int interim = 0;
	for (char c : s) {
		interim = TABLE[interim][c - '0'];
	}
	return interim == 0;
}

int main() {
	auto numbers = { 5724, 5727, 112946, 112949 };
	for (int num : numbers) {
		using std::stringstream;
		stringstream ss;
		ss << num;
		bool isValid = damm(ss.str());
		if (isValid) {
			printf("%6d is valid\n", num);
		} else {
			printf("%6d is invalid\n", num);
		}
	}

	return 0;
}










Ada: 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;
Task: Damm algorithm




Go: package main

import "fmt"

var table = [10][10]byte{
    {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},
}

func damm(input string) bool {
    var interim byte
    for _, c := range []byte(input) {
        interim = table[interim][c-'0']
    }
    return interim == 0
}

func main() {
    for _, s := range []string{"5724", "5727", "112946", "112949"} {
        fmt.Printf("%6s  %t\n", s, damm(s))
    }
}








Ada: 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;