WeixiangYan/CodeTransOcean huggingface dataset:
Task: ISBN13 check digit





Java: public static void main(){
        System.out.println(isISBN13("978-1734314502"));
        System.out.println(isISBN13("978-1734314509"));
        System.out.println(isISBN13("978-1788399081"));
        System.out.println(isISBN13("978-1788399083"));
    }
public static boolean isISBN13(String in){
        int pre = Integer.parseInt(in.substring(0,3));
        if (pre!=978)return false;
        String postStr = in.substring(4);
        if (postStr.length()!=10)return false;
        int post = Integer.parseInt(postStr);
        int sum = 38;
        for(int x = 0; x<10;x+=2)
        sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48));
        if(sum%10==0) return true;
        return false;
    }







Ada: with Ada.Text_IO;

procedure ISBN_Check is

   function Is_Valid (ISBN : String) return Boolean is
      Odd       : Boolean := True;
      Sum       : Integer := 0;
      Value     : Integer;
   begin
      for I in ISBN'Range loop
         if ISBN (I) in '0' .. '9' then
            Value := Character'Pos (ISBN (I)) - Character'Pos ('0');
            if Odd then
               Sum := Sum + Value;
            else
               Sum := Sum + 3 * Value;
            end if;
            Odd := not Odd;
         end if;
      end loop;
      return Sum mod 10 = 0;
   end Is_Valid;

   procedure Show (ISBN : String) is
      use Ada.Text_IO;
      Valid : constant Boolean := Is_Valid (ISBN);
   begin
      Put (ISBN); Put ("  ");
      Put ((if Valid then "Good" else "Bad"));
      New_Line;
   end Show;
begin
   Show ("978-1734314502");
   Show ("978-1734314509");
   Show ("978-1788399081");
   Show ("978-1788399083");
end ISBN_Check;
Task: ISBN13 check digit







Python: def is_isbn13(n):
    n = n.replace('-','').replace(' ', '')
    if len(n) != 13:
        return False
    product = (sum(int(ch) for ch in n[::2]) 
               + sum(int(ch) * 3 for ch in n[1::2]))
    return product % 10 == 0

if __name__ == '__main__':
    tests = .strip().split()
    for t in tests:
        print(f"ISBN13 {t} validates {is_isbn13(t)}")





Ada: with Ada.Text_IO;

procedure ISBN_Check is

   function Is_Valid (ISBN : String) return Boolean is
      Odd       : Boolean := True;
      Sum       : Integer := 0;
      Value     : Integer;
   begin
      for I in ISBN'Range loop
         if ISBN (I) in '0' .. '9' then
            Value := Character'Pos (ISBN (I)) - Character'Pos ('0');
            if Odd then
               Sum := Sum + Value;
            else
               Sum := Sum + 3 * Value;
            end if;
            Odd := not Odd;
         end if;
      end loop;
      return Sum mod 10 = 0;
   end Is_Valid;

   procedure Show (ISBN : String) is
      use Ada.Text_IO;
      Valid : constant Boolean := Is_Valid (ISBN);
   begin
      Put (ISBN); Put ("  ");
      Put ((if Valid then "Good" else "Bad"));
      New_Line;
   end Show;
begin
   Show ("978-1734314502");
   Show ("978-1734314509");
   Show ("978-1788399081");
   Show ("978-1788399083");
end ISBN_Check;
Task: ISBN13 check digit








VB: Module Module1

    Function CheckISBN13(code As String) As Boolean
        code = code.Replace("-", "").Replace(" ", "")
        If code.Length <> 13 Then
            Return False
        End If
        Dim sum = 0
        For Each i_d In code.Select(Function(digit, index) (index, digit))
            Dim index = i_d.index
            Dim digit = i_d.digit
            If Char.IsDigit(digit) Then
                sum += (Asc(digit) - Asc("0")) * If(index Mod 2 = 0, 1, 3)
            Else
                Return False
            End If
        Next
        Return sum Mod 10 = 0
    End Function

    Sub Main()
        Console.WriteLine(CheckISBN13("978-1734314502"))
        Console.WriteLine(CheckISBN13("978-1734314509"))
        Console.WriteLine(CheckISBN13("978-1788399081"))
        Console.WriteLine(CheckISBN13("978-1788399083"))
    End Sub

End Module




Ada: with Ada.Text_IO;

procedure ISBN_Check is

   function Is_Valid (ISBN : String) return Boolean is
      Odd       : Boolean := True;
      Sum       : Integer := 0;
      Value     : Integer;
   begin
      for I in ISBN'Range loop
         if ISBN (I) in '0' .. '9' then
            Value := Character'Pos (ISBN (I)) - Character'Pos ('0');
            if Odd then
               Sum := Sum + Value;
            else
               Sum := Sum + 3 * Value;
            end if;
            Odd := not Odd;
         end if;
      end loop;
      return Sum mod 10 = 0;
   end Is_Valid;

   procedure Show (ISBN : String) is
      use Ada.Text_IO;
      Valid : constant Boolean := Is_Valid (ISBN);
   begin
      Put (ISBN); Put ("  ");
      Put ((if Valid then "Good" else "Bad"));
      New_Line;
   end Show;
begin
   Show ("978-1734314502");
   Show ("978-1734314509");
   Show ("978-1788399081");
   Show ("978-1788399083");
end ISBN_Check;
Task: ISBN13 check digit

C: #include <stdio.h>

int check_isbn13(const char *isbn) {
    int ch = *isbn, count = 0, sum = 0;
    
    for ( ; ch != 0; ch = *++isbn, ++count) {
        
        if (ch == ' ' || ch == '-') {
            --count;
            continue;
        }
        if (ch < '0' || ch > '9') {
            return 0;
        }
        if (count & 1) {
            sum += 3 * (ch - '0');
        } else {
            sum += ch - '0';
        }
    }
    if (count != 13) return 0;
    return !(sum%10);
}

int main() {
    int i;
    const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"};
    for (i = 0; i < 4; ++i) {
        printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad");
    }
    return 0;
}
Task: ISBN13 check digit



C#: using System;
using System.Linq;

public class Program
{
    public static void Main() {
        Console.WriteLine(CheckISBN13("978-1734314502"));
        Console.WriteLine(CheckISBN13("978-1734314509"));
        Console.WriteLine(CheckISBN13("978-1788399081"));
        Console.WriteLine(CheckISBN13("978-1788399083"));

        static bool CheckISBN13(string code) {
            code = code.Replace("-", "").Replace(" ", "");
            if (code.Length != 13) return false;
            int sum = 0;
            foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) {
                if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3);
                else return false;
            }
            return sum % 10 == 0;
        }
    }
}
Task: ISBN13 check digit


C++: #include <iostream>

bool check_isbn13(std::string isbn) {
    int count = 0;
    int sum = 0;

    for (auto ch : isbn) {
        
        if (ch == ' ' || ch == '-') {
            continue;
        }
        if (ch < '0' || ch > '9') {
            return false;
        }
        if (count & 1) {
            sum += 3 * (ch - '0');
        } else {
            sum += ch - '0';
        }
        count++;
    }

    if (count != 13) {
        return false;
    }
    return sum % 10 == 0;
}

int main() {
    auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" };
    for (auto isbn : isbns) {
        std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n';
    }

    return 0;
}
Task: ISBN13 check digit





Java: public static void main(){
        System.out.println(isISBN13("978-1734314502"));
        System.out.println(isISBN13("978-1734314509"));
        System.out.println(isISBN13("978-1788399081"));
        System.out.println(isISBN13("978-1788399083"));
    }
public static boolean isISBN13(String in){
        int pre = Integer.parseInt(in.substring(0,3));
        if (pre!=978)return false;
        String postStr = in.substring(4);
        if (postStr.length()!=10)return false;
        int post = Integer.parseInt(postStr);
        int sum = 38;
        for(int x = 0; x<10;x+=2)
        sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48));
        if(sum%10==0) return true;
        return false;
    }
Task: ISBN13 check digit







Python: def is_isbn13(n):
    n = n.replace('-','').replace(' ', '')
    if len(n) != 13:
        return False
    product = (sum(int(ch) for ch in n[::2]) 
               + sum(int(ch) * 3 for ch in n[1::2]))
    return product % 10 == 0

if __name__ == '__main__':
    tests = .strip().split()
    for t in tests:
        print(f"ISBN13 {t} validates {is_isbn13(t)}")
Task: ISBN13 check digit








VB: arraybase 1
dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"}

for n = 1 to isbn[?]
	sum = 0
	isbnStr = isbn[n]
	isbnStr = replace(isbnStr, "-", "")
	isbnStr = replace(isbnStr, " ", "")
	for m = 1 to length(isbnStr)
		if m mod 2 = 0 then
			num = 3 * int(mid(isbnStr, m, 1))
		else
			num = int(mid(isbnStr, m, 1))
		end if
		sum += num
	next m
	if sum mod 10 = 0 then
		print isbn[n]; ": good"
	else
		print isbn[n]; ": bad"
	end if
next n
Task: ISBN13 check digit




Go: package main

import (
    "fmt"
    "strings"
    "unicode/utf8"
)

func checkIsbn13(isbn string) bool {
    
    isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "")
    
    le := utf8.RuneCountInString(isbn)
    if le != 13 {
        return false
    }
    
    sum := int32(0)
    for i, c := range isbn {
        if c < '0' || c > '9' {
            return false
        }
        if i%2 == 0 {
            sum += c - '0'
        } else {
            sum += 3 * (c - '0')
        }
    }
    return sum%10 == 0
}

func main() {
    isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}
    for _, isbn := range isbns {
        res := "bad"
        if checkIsbn13(isbn) {
            res = "good"
        }
        fmt.Printf("%s: %s\n", isbn, res)
    }
}
Task: ISBN13 check digit



C#: using System;
using System.Linq;

public class Program
{
    public static void Main() {
        Console.WriteLine(CheckISBN13("978-1734314502"));
        Console.WriteLine(CheckISBN13("978-1734314509"));
        Console.WriteLine(CheckISBN13("978-1788399081"));
        Console.WriteLine(CheckISBN13("978-1788399083"));

        static bool CheckISBN13(string code) {
            code = code.Replace("-", "").Replace(" ", "");
            if (code.Length != 13) return false;
            int sum = 0;
            foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) {
                if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3);
                else return false;
            }
            return sum % 10 == 0;
        }
    }
}








Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit


C++: #include <iostream>

bool check_isbn13(std::string isbn) {
    int count = 0;
    int sum = 0;

    for (auto ch : isbn) {
        
        if (ch == ' ' || ch == '-') {
            continue;
        }
        if (ch < '0' || ch > '9') {
            return false;
        }
        if (count & 1) {
            sum += 3 * (ch - '0');
        } else {
            sum += ch - '0';
        }
        count++;
    }

    if (count != 13) {
        return false;
    }
    return sum % 10 == 0;
}

int main() {
    auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" };
    for (auto isbn : isbns) {
        std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n';
    }

    return 0;
}









Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit

C: #include <stdio.h>

int check_isbn13(const char *isbn) {
    int ch = *isbn, count = 0, sum = 0;
    
    for ( ; ch != 0; ch = *++isbn, ++count) {
        
        if (ch == ' ' || ch == '-') {
            --count;
            continue;
        }
        if (ch < '0' || ch > '9') {
            return 0;
        }
        if (count & 1) {
            sum += 3 * (ch - '0');
        } else {
            sum += ch - '0';
        }
    }
    if (count != 13) return 0;
    return !(sum%10);
}

int main() {
    int i;
    const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"};
    for (i = 0; i < 4; ++i) {
        printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad");
    }
    return 0;
}










Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit





Java: public static void main(){
        System.out.println(isISBN13("978-1734314502"));
        System.out.println(isISBN13("978-1734314509"));
        System.out.println(isISBN13("978-1788399081"));
        System.out.println(isISBN13("978-1788399083"));
    }
public static boolean isISBN13(String in){
        int pre = Integer.parseInt(in.substring(0,3));
        if (pre!=978)return false;
        String postStr = in.substring(4);
        if (postStr.length()!=10)return false;
        int post = Integer.parseInt(postStr);
        int sum = 38;
        for(int x = 0; x<10;x+=2)
        sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48));
        if(sum%10==0) return true;
        return false;
    }






Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit







Python: def is_isbn13(n):
    n = n.replace('-','').replace(' ', '')
    if len(n) != 13:
        return False
    product = (sum(int(ch) for ch in n[::2]) 
               + sum(int(ch) * 3 for ch in n[1::2]))
    return product % 10 == 0

if __name__ == '__main__':
    tests = .strip().split()
    for t in tests:
        print(f"ISBN13 {t} validates {is_isbn13(t)}")




Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit








VB: arraybase 1
dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"}

for n = 1 to isbn[?]
	sum = 0
	isbnStr = isbn[n]
	isbnStr = replace(isbnStr, "-", "")
	isbnStr = replace(isbnStr, " ", "")
	for m = 1 to length(isbnStr)
		if m mod 2 = 0 then
			num = 3 * int(mid(isbnStr, m, 1))
		else
			num = int(mid(isbnStr, m, 1))
		end if
		sum += num
	next m
	if sum mod 10 = 0 then
		print isbn[n]; ": good"
	else
		print isbn[n]; ": bad"
	end if
next n



Fortran: program isbn13
    implicit none

    character(len=14), dimension(4), parameter  :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
    integer                                     :: i

    do i = 1, ubound(isbns, 1)
        if (check_isbn13(isbns(i))) then
            print*, isbns(i), " : ", "good"
        else
            print*, isbns(i), " : ", "bad"
        end if
    end do
contains
    pure function check_isbn13(isbn)
        character(len=*), intent(in)    :: isbn
        logical                         :: check_isbn13
        integer                         :: summ, counter, i, digit

        check_isbn13 = .false.
        counter = 0
        summ = 0

        do i = 1, len(isbn)
            if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle
            counter = counter + 1
            read(isbn(i:i), '(I1)') digit
            if (modulo(counter, 2) == 0) then
                summ = summ + 3*digit
            else
                summ = summ + digit
            end if
        end do
        if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true.
    end function check_isbn13
end program isbn13
Task: ISBN13 check digit








VB: Module Module1

    Function CheckISBN13(code As String) As Boolean
        code = code.Replace("-", "").Replace(" ", "")
        If code.Length <> 13 Then
            Return False
        End If
        Dim sum = 0
        For Each i_d In code.Select(Function(digit, index) (index, digit))
            Dim index = i_d.index
            Dim digit = i_d.digit
            If Char.IsDigit(digit) Then
                sum += (Asc(digit) - Asc("0")) * If(index Mod 2 = 0, 1, 3)
            Else
                Return False
            End If
        Next
        Return sum Mod 10 = 0
    End Function

    Sub Main()
        Console.WriteLine(CheckISBN13("978-1734314502"))
        Console.WriteLine(CheckISBN13("978-1734314509"))
        Console.WriteLine(CheckISBN13("978-1788399081"))
        Console.WriteLine(CheckISBN13("978-1788399083"))
    End Sub

End Module
Task: Days between dates



C#: using System;
using System.Globalization;

public class Program
{
    public static void Main() => WriteLine(DateDiff("1970-01-01", "2019-10-18"));

    public static int DateDiff(string d1, string d2) {
        var a = DateTime.ParseExact(d1, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        var b = DateTime.ParseExact(d2, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        return (int)(b - a).TotalDays;
    }
}









Ada: with Ada.Calendar;
with Ada.Text_IO;
with Ada.Integer_Text_IO;

with GNAT.Calendar.Time_IO;

procedure Days_Between_Dates is

   function Days_Between (Lower_Date  : in Ada.Calendar.Time;
                          Higher_Date : in Ada.Calendar.Time) return Integer
   is
      use Ada.Calendar;
      Diff : constant Duration := Higher_Date - Lower_Date;
   begin
      return Integer (Diff / Day_Duration'Last);
   end Days_Between;

   procedure Put_Days_Between (Lower_Date  : in String;
                               Higher_Date : in String;
                               Comment     : in String)
   is
      use Ada.Text_IO;
      use Ada.Integer_Text_IO;
      use GNAT.Calendar.Time_IO;

      Diff : constant Integer := Days_Between (Lower_Date  => Value (Lower_Date),
                                               Higher_Date => Value (Higher_Date));
   begin
      Put ("Days between " & Lower_Date & " and " & Higher_Date & " is ");
      Put (Diff, Width => 5);
      Put (" days  
      Put (Comment);
      New_Line;
   end Put_Days_Between;

begin
   Put_Days_Between ("1995-11-21", "1995-11-21", "Identical dates");
   Put_Days_Between ("2019-01-01", "2019-01-02", "Positive span");
   Put_Days_Between ("2019-01-02", "2019-01-01", "Negative span");
   Put_Days_Between ("2019-01-01", "2019-03-01", "Non-leap year");
   Put_Days_Between ("2020-01-01", "2020-03-01", "Leap year");
   Put_Days_Between ("1902-01-01", "1968-12-25", "Past");
   Put_Days_Between ("2090-01-01", "2098-12-25", "Future");
   Put_Days_Between ("1902-01-01", "2098-12-25", "Long span");
end Days_Between_Dates;