WeixiangYan/CodeTransOcean huggingface dataset:
Task: N'th


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

using namespace std;

string Suffix(int num)
{
    switch (num % 10)
    {
        case 1 : if(num % 100 != 11) return "st";
           break;
        case 2 : if(num % 100 != 12) return "nd";
           break;
        case 3 : if(num % 100 != 13) return "rd";
    }

    return "th";
}

int main()
{
    cout << "Set [0,25]:" << endl;
    for (int i = 0; i < 26; i++)
        cout << i << Suffix(i) << " ";
    
    cout << endl;

    cout << "Set [250,265]:" << endl;
    for (int i = 250; i < 266; i++)
        cout << i << Suffix(i) << " ";
   
    cout << endl;

    cout << "Set [1000,1025]:" << endl;
    for (int i = 1000; i < 1026; i++)
        cout << i << Suffix(i) << " ";
   
    cout << endl;

    return 0;
}








COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: N'th





Java: public class Nth {
	public static String ordinalAbbrev(int n){
		String ans = "th"; 
		if(n % 100 / 10 == 1) return ans; 
		switch(n % 10){
			case 1: ans = "st"; break;
			case 2: ans = "nd"; break;
			case 3: ans = "rd"; break;
		}
		return ans;
	}
	
	public static void main(String[] args){
		for(int i = 0; i <= 25;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
		System.out.println();
		for(int i = 250; i <= 265;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
		System.out.println();
		for(int i = 1000; i <= 1025;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
	}
}





COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: N'th







Python: _suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']

def nth(n):
    return "%i'%s" % (n, _suffix[n%10] if n % 100 <= 10 or n % 100 > 20 else 'th')

if __name__ == '__main__':
    for j in range(0,1001, 250):
        print(' '.join(nth(i) for i in list(range(j, j+25))))



COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: N'th








VB: Private Function ordinals() As Variant
    ordinals = [{"th","st","nd","rd"}]
End Function

Private Function Nth(n As Variant, Optional apostrophe As Boolean = False) As String
    Dim mod10 As Integer: mod10 = n Mod 10 + 1
    If mod10 > 4 Or n Mod 100 = mod10 + 9 Then mod10 = 1
    Nth = CStr(n) & String$(Val(-apostrophe), "
End Function

Public Sub main()
    Ranges = [{0,25;250,265;1000,1025}]
    For i = 1 To UBound(Ranges)
        For j = Ranges(i, 1) To Ranges(i, 2)
            If j Mod 10 = 0 Then Debug.Print
            Debug.Print Format(Nth(j, i = 2), "@@@@@@@");
        Next j
        Debug.Print
    Next i
End Sub


COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: N'th




Go: package main

import "fmt"

func ord(n int) string {
    s := "th"
    switch c := n % 10; c {
    case 1, 2, 3:
        if n%100/10 == 1 {
            break
        }
        switch c {
        case 1:
            s = "st"
        case 2:
            s = "nd"
        case 3:
            s = "rd"
        }
    }
    return fmt.Sprintf("%d%s", n, s)
}

func main() {
    for n := 0; n <= 25; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
    for n := 250; n <= 265; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
    for n := 1000; n <= 1025; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
}






COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: N'th






PHP: function nth($num) {
  $os = "th";
  if ($num % 100 <= 10 or $num % 100 > 20) {
    switch ($num % 10) {
      case 1:
        $os = "st";
        break;
      case 2:
        $os = "nd";
        break;
      case 3:
        $os = "rd";
        break;
    }
  } 
  return $num . $os;
}

foreach ([[0,25], [250,265], [1000,1025]] as $i) {
  while ($i[0] <= $i[1]) {
    echo nth($i[0]) . " ";
    $i[0]++;
  }
  echo "\n";
}
Task: N'th






PHP: function nth($num) {
  $os = "th";
  if ($num % 100 <= 10 or $num % 100 > 20) {
    switch ($num % 10) {
      case 1:
        $os = "st";
        break;
      case 2:
        $os = "nd";
        break;
      case 3:
        $os = "rd";
        break;
    }
  } 
  return $num . $os;
}

foreach ([[0,25], [250,265], [1000,1025]] as $i) {
  while ($i[0] <= $i[1]) {
    echo nth($i[0]) . " ";
    $i[0]++;
  }
  echo "\n";
}






Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th






PHP: function nth($num) {
  $os = "th";
  if ($num % 100 <= 10 or $num % 100 > 20) {
    switch ($num % 10) {
      case 1:
        $os = "st";
        break;
      case 2:
        $os = "nd";
        break;
      case 3:
        $os = "rd";
        break;
    }
  } 
  return $num . $os;
}

foreach ([[0,25], [250,265], [1000,1025]] as $i) {
  while ($i[0] <= $i[1]) {
    echo nth($i[0]) . " ";
    $i[0]++;
  }
  echo "\n";
}




COBOL: IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-NUMBER.
    05 N               PIC 9(8).
    05 LAST-TWO-DIGITS PIC 99.
    05 LAST-DIGIT      PIC 9.
    05 N-TO-OUTPUT     PIC Z(7)9.
    05 SUFFIX          PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
    PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
    PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
    PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
    STOP RUN.
NTH-PARAGRAPH.
    MOVE 'TH' TO SUFFIX.
    MOVE N (7:2) TO LAST-TWO-DIGITS.
    IF LAST-TWO-DIGITS IS LESS THAN 4,
    OR LAST-TWO-DIGITS IS GREATER THAN 20,
    THEN PERFORM DECISION-PARAGRAPH.
    MOVE N TO N-TO-OUTPUT.
    DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
    DISPLAY SUFFIX WITH NO ADVANCING.
    DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
    MOVE N (8:1) TO LAST-DIGIT.
    IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
    IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.
Task: Levenshtein distance



C#: using System;

namespace LevenshteinDistance
{
    class Program
    {
        static int LevenshteinDistance(string s, string t)
        {
            int n = s.Length;
            int m = t.Length;
            int[,] d = new int[n + 1, m + 1];
		
	    if (n == 0)
	    {
		return m;
	    }
	
	    if (m == 0)
	    {
		return n;
	    }

            for (int i = 0; i <= n; i++)
                d[i, 0] = i;
            for (int j = 0; j <= m; j++)
                d[0, j] = j;
			
            for (int j = 1; j <= m; j++)
                for (int i = 1; i <= n; i++)
                    if (s[i - 1] == t[j - 1])
                        d[i, j] = d[i - 1, j - 1];  
                    else
                        d[i, j] = Math.Min(Math.Min(
                            d[i - 1, j] + 1,    
                            d[i, j - 1] + 1),   
                            d[i - 1, j - 1] + 1 
                            );
            return d[n, m];
        }

        static void Main(string[] args)
        {
            if (args.Length == 2)
                Console.WriteLine("{0} -> {1} = {2}",
                    args[0], args[1], LevenshteinDistance(args[0], args[1]));
            else
                Console.WriteLine("Usage:-\n\nLevenshteinDistance <string1> <string2>");
        }
    }
}









Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;
Task: Levenshtein distance

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


int levenshtein(const char *s, int ls, const char *t, int lt)
{
        int a, b, c;

        
        if (!ls) return lt;
        if (!lt) return ls;

        
        if (s[ls - 1] == t[lt - 1])
                return levenshtein(s, ls - 1, t, lt - 1);

        
        a = levenshtein(s, ls - 1, t, lt - 1);
        b = levenshtein(s, ls,     t, lt - 1);
        c = levenshtein(s, ls - 1, t, lt    );

        if (a > b) a = b;
        if (a > c) a = c;

        return a + 1;
}

int main()
{
        const char *s1 = "rosettacode";
        const char *s2 = "raisethysword";
        printf("distance between `%s' and `%s': %d\n", s1, s2,
                levenshtein(s1, strlen(s1), s2, strlen(s2)));

        return 0;
}











Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;
Task: Levenshtein distance


C++: #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
 
template <typename StringType>
size_t levenshtein_distance(const StringType& s1, const StringType& s2) {
    const size_t m = s1.size();
    const size_t n = s2.size();
    if (m == 0)
        return n;
    if (n == 0)
        return m;
    std::vector<size_t> costs(n + 1);
    std::iota(costs.begin(), costs.end(), 0);
    size_t i = 0;
    for (auto c1 : s1) {
        costs[0] = i + 1;
        size_t corner = i;
        size_t j = 0;
        for (auto c2 : s2) {
            size_t upper = costs[j + 1];
            costs[j + 1] = (c1 == c2) ? corner
                : 1 + std::min(std::min(upper, corner), costs[j]);
            corner = upper;
            ++j;
        }
        ++i;
    }
    return costs[n];
}
 
int main() {
    std::wstring s0 = L"rosettacode";
    std::wstring s1 = L"raisethysword";
    std::wcout << L"distance between " << s0 << L" and " << s1 << L": "
        << levenshtein_distance(s0, s1) << std::endl;
    return 0;
}










Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;
Task: Levenshtein distance




Go: package main

import "fmt"

func main() {
    fmt.Println(ld("kitten", "sitting"))
}

func ld(s, t string) int {
    d := make([][]int, len(s)+1)
    for i := range d {
        d[i] = make([]int, len(t)+1)
    }
    for i := range d {
        d[i][0] = i
    }
    for j := range d[0] {
        d[0][j] = j
    }
    for j := 1; j <= len(t); j++ {
        for i := 1; i <= len(s); i++ {
            if s[i-1] == t[j-1] {
                d[i][j] = d[i-1][j-1]
            } else {
                min := d[i-1][j]
                if d[i][j-1] < min {
                    min = d[i][j-1]
                }
                if d[i-1][j-1] < min {
                    min = d[i-1][j-1]
                }
                d[i][j] = min + 1
            }
        }

    }
    return d[len(s)][len(t)]
}








Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;
Task: Levenshtein distance





Java: public class Levenshtein {

    public static int distance(String a, String b) {
        a = a.toLowerCase();
        b = b.toLowerCase();
        
        int [] costs = new int [b.length() + 1];
        for (int j = 0; j < costs.length; j++)
            costs[j] = j;
        for (int i = 1; i <= a.length(); i++) {
            
            costs[0] = i;
            int nw = i - 1;
            for (int j = 1; j <= b.length(); j++) {
                int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
                nw = costs[j];
                costs[j] = cj;
            }
        }
        return costs[b.length()];
    }

    public static void main(String [] args) {
        String [] data = { "kitten", "sitting", "saturday", "sunday", "rosettacode", "raisethysword" };
        for (int i = 0; i < data.length; i += 2)
            System.out.println("distance(" + data[i] + ", " + data[i+1] + ") = " + distance(data[i], data[i+1]));
    }
}







Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;
Task: Levenshtein distance







Python: def setup():
    println(distance("kitten", "sitting"))

def distance(a, b):
    costs = []
    for j in range(len(b) + 1):
        costs.append(j)
    for i in range(1, len(a) + 1):
        costs[0] = i
        nw = i - 1
        for j in range(1, len(b) + 1):
            cj = min(1 + min(costs[j], costs[j - 1]),
                     nw if a[i - 1] == b[j - 1] else nw + 1)
            nw = costs[j]
            costs[j] = cj

    return costs[len(b)]





Ada: with Ada.Text_IO;

procedure Main is
   function Levenshtein_Distance (S, T : String) return Natural is
      D : array (0 .. S'Length, 0 .. T'Length) of Natural;
   begin
      for I in D'Range (1) loop
         D (I, 0) := I;
      end loop;
      for I in D'Range (2) loop
         D (0, I) := I;
      end loop;
      for J in T'Range loop
         for I in S'Range loop
            if S (I) = T (J) then
               D (I, J) := D (I - 1, J - 1);
            else
               D (I, J) :=
                  Natural'Min
                    (Natural'Min (D (I - 1, J) + 1, D (I, J - 1) + 1),
                     D (I - 1, J - 1) + 1);
            end if;
         end loop;
      end loop;
      return D (S'Length, T'Length);
   end Levenshtein_Distance;
begin
   Ada.Text_IO.Put_Line
     ("kitten -> sitting:" &
      Integer'Image (Levenshtein_Distance ("kitten", "sitting")));
   Ada.Text_IO.Put_Line
     ("rosettacode -> raisethysword:" &
      Integer'Image (Levenshtein_Distance ("rosettacode", "raisethysword")));
end Main;