WeixiangYan/CodeTransOcean huggingface dataset:
Task: Greatest subsequential sum


C++: #include <utility>   
#include <iterator>  
#include <iostream>  
#include <ostream>   
#include <algorithm> 
#include <iterator>  



















template<typename ForwardIterator>
 std::pair<ForwardIterator, ForwardIterator>
 max_subseq(ForwardIterator begin, ForwardIterator end)
{
  typedef typename std::iterator_traits<ForwardIterator>::value_type
    value_type;

  ForwardIterator seq_begin = begin, seq_end = seq_begin;
  value_type seq_sum = value_type();
  ForwardIterator current_begin = begin;
  value_type current_sum = value_type();

  value_type zero = value_type();

  for (ForwardIterator iter = begin; iter != end; ++iter)
  {
    value_type value = *iter;
    if (zero < value)
    {
      if (current_sum < zero)
      {
        current_sum = zero;
        current_begin = iter;
      }
    }
    else
    {
      if (seq_sum < current_sum)
      {
        seq_begin = current_begin;
        seq_end = iter;
        seq_sum = current_sum;
      }
    }
    current_sum += value;
  }

  if (seq_sum < current_sum)
  {
    seq_begin = current_begin;
    seq_end = end;
    seq_sum = current_sum;
  }

  return std::make_pair(seq_begin, seq_end);
}


int array[] = { -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 };


template<typename T, int N> int* end(T (&arr)[N]) { return arr+N; }

int main()
{
  
  std::pair<int*, int*> seq = max_subseq(array, end(array));

  
  std::copy(seq.first, seq.second, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  return 0;
}




Java: import java.util.Scanner;
import java.util.ArrayList;

public class Sub{
    private static int[] indices;

    public static void main(String[] args){
        ArrayList<Long> array= new ArrayList<Long>(); 
        Scanner in = new Scanner(System.in);
        while(in.hasNextLong()) array.add(in.nextLong());
        long highSum= Long.MIN_VALUE;
        ArrayList<Long> highSet= new ArrayList<Long>();
        
        for(int subSize= 0;subSize<= array.size();subSize++){
            indices= new int[subSize];
            for(int i= 0;i< subSize;i++) indices[i]= i;
            do{
                long sum= 0;
                ArrayList<Long> temp= new ArrayList<Long>();
                
                for(long index:indices) {sum+= array.get(index); temp.add(array.get(index));}
                if(sum > highSum){
                    highSet= temp;    
                    highSum= sum;
                }
            }while(nextIndices(array));
        }
        System.out.println("Sum: " + highSum + "\nSet: " + 
        		highSet);
    }
    

    private static boolean nextIndices(ArrayList<Long> a) {
        for(int i= indices.length-1;i >= 0;--i){
            indices[i]++;
            for(int j=i+1;j < indices.length;++j){
                indices[j]= indices[j - 1] + 1;
            }
            if(indices[indices.length - 1] < a.size()){
                return true;
            }
        }
        return false;
    }
}
Task: Greatest subsequential sum




Go: package main

import "fmt"

func gss(s []int) ([]int, int) {
    var best, start, end, sum, sumStart int
    for i, x := range s {
        sum += x
        switch {
        case sum > best:
            best = sum
            start = sumStart
            end = i + 1
        case sum < 0:
            sum = 0
            sumStart = i + 1
        }
    }
    return s[start:end], best
}

var testCases = [][]int{
    {-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1},
    {-1, 1, 2, -5, -6},
    {},
    {-1, -2, -1},
}

func main() {
    for _, c := range testCases {
        fmt.Println("Input:  ", c)
        subSeq, sum := gss(c)
        fmt.Println("Sub seq:", subSeq)
        fmt.Println("Sum:    ", sum, "\n")
    }
}


Java: import java.util.Scanner;
import java.util.ArrayList;

public class Sub{
    private static int[] indices;

    public static void main(String[] args){
        ArrayList<Long> array= new ArrayList<Long>(); 
        Scanner in = new Scanner(System.in);
        while(in.hasNextLong()) array.add(in.nextLong());
        long highSum= Long.MIN_VALUE;
        ArrayList<Long> highSet= new ArrayList<Long>();
        
        for(int subSize= 0;subSize<= array.size();subSize++){
            indices= new int[subSize];
            for(int i= 0;i< subSize;i++) indices[i]= i;
            do{
                long sum= 0;
                ArrayList<Long> temp= new ArrayList<Long>();
                
                for(long index:indices) {sum+= array.get(index); temp.add(array.get(index));}
                if(sum > highSum){
                    highSet= temp;    
                    highSum= sum;
                }
            }while(nextIndices(array));
        }
        System.out.println("Sum: " + highSum + "\nSet: " + 
        		highSet);
    }
    

    private static boolean nextIndices(ArrayList<Long> a) {
        for(int i= indices.length-1;i >= 0;--i){
            indices[i]++;
            for(int j=i+1;j < indices.length;++j){
                indices[j]= indices[j - 1] + 1;
            }
            if(indices[indices.length - 1] < a.size()){
                return true;
            }
        }
        return false;
    }
}
Task: Greatest subsequential sum




Go: package main

import "fmt"

func gss(s []int) ([]int, int) {
    var best, start, end, sum, sumStart int
    for i, x := range s {
        sum += x
        switch {
        case sum > best:
            best = sum
            start = sumStart
            end = i + 1
        case sum < 0:
            sum = 0
            sumStart = i + 1
        }
    }
    return s[start:end], best
}

var testCases = [][]int{
    {-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1},
    {-1, 1, 2, -5, -6},
    {},
    {-1, -2, -1},
}

func main() {
    for _, c := range testCases {
        fmt.Println("Input:  ", c)
        subSeq, sum := gss(c)
        fmt.Println("Sub seq:", subSeq)
        fmt.Println("Sum:    ", sum, "\n")
    }
}




Python: def maxsubseq(seq):
  return max((seq[begin:end] for begin in xrange(len(seq)+1)
                             for end in xrange(begin, len(seq)+1)),
             key=sum)
Task: Greatest subsequential sum


C++: #include <utility>   
#include <iterator>  
#include <iostream>  
#include <ostream>   
#include <algorithm> 
#include <iterator>  



















template<typename ForwardIterator>
 std::pair<ForwardIterator, ForwardIterator>
 max_subseq(ForwardIterator begin, ForwardIterator end)
{
  typedef typename std::iterator_traits<ForwardIterator>::value_type
    value_type;

  ForwardIterator seq_begin = begin, seq_end = seq_begin;
  value_type seq_sum = value_type();
  ForwardIterator current_begin = begin;
  value_type current_sum = value_type();

  value_type zero = value_type();

  for (ForwardIterator iter = begin; iter != end; ++iter)
  {
    value_type value = *iter;
    if (zero < value)
    {
      if (current_sum < zero)
      {
        current_sum = zero;
        current_begin = iter;
      }
    }
    else
    {
      if (seq_sum < current_sum)
      {
        seq_begin = current_begin;
        seq_end = iter;
        seq_sum = current_sum;
      }
    }
    current_sum += value;
  }

  if (seq_sum < current_sum)
  {
    seq_begin = current_begin;
    seq_end = end;
    seq_sum = current_sum;
  }

  return std::make_pair(seq_begin, seq_end);
}


int array[] = { -1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1 };


template<typename T, int N> int* end(T (&arr)[N]) { return arr+N; }

int main()
{
  
  std::pair<int*, int*> seq = max_subseq(array, end(array));

  
  std::copy(seq.first, seq.second, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;

  return 0;
}






Python: def maxsubseq(seq):
  return max((seq[begin:end] for begin in xrange(len(seq)+1)
                             for end in xrange(begin, len(seq)+1)),
             key=sum)
Task: Greatest subsequential sum





Java: import java.util.Scanner;
import java.util.ArrayList;

public class Sub{
    private static int[] indices;

    public static void main(String[] args){
        ArrayList<Long> array= new ArrayList<Long>(); 
        Scanner in = new Scanner(System.in);
        while(in.hasNextLong()) array.add(in.nextLong());
        long highSum= Long.MIN_VALUE;
        ArrayList<Long> highSet= new ArrayList<Long>();
        
        for(int subSize= 0;subSize<= array.size();subSize++){
            indices= new int[subSize];
            for(int i= 0;i< subSize;i++) indices[i]= i;
            do{
                long sum= 0;
                ArrayList<Long> temp= new ArrayList<Long>();
                
                for(long index:indices) {sum+= array.get(index); temp.add(array.get(index));}
                if(sum > highSum){
                    highSet= temp;    
                    highSum= sum;
                }
            }while(nextIndices(array));
        }
        System.out.println("Sum: " + highSum + "\nSet: " + 
        		highSet);
    }
    

    private static boolean nextIndices(ArrayList<Long> a) {
        for(int i= indices.length-1;i >= 0;--i){
            indices[i]++;
            for(int j=i+1;j < indices.length;++j){
                indices[j]= indices[j - 1] + 1;
            }
            if(indices[indices.length - 1] < a.size()){
                return true;
            }
        }
        return false;
    }
}



Python: def maxsubseq(seq):
  return max((seq[begin:end] for begin in xrange(len(seq)+1)
                             for end in xrange(begin, len(seq)+1)),
             key=sum)
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}



C#: int GetNumber() {
    return 5;
}
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}




Go: func main() {
inf:
    goto inf
}
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}





Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}







Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}


C#: int GetNumber() {
    return 5;
}
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}



Go: func main() {
inf:
    goto inf
}
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}




Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}






Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}







VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures



C#: int GetNumber() {
    return 5;
}



Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);
Task: Flow-control structures



C#: int GetNumber() {
    return 5;
}





Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures



C#: int GetNumber() {
    return 5;
}






VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures





Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);



Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures





Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);




VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures




Go: func main() {
inf:
    goto inf
}




Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures




Go: func main() {
inf:
    goto inf
}





VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures







Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"


VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures




Go: func main() {
inf:
    goto inf
}


Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}








VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
Task: Flow-control structures

C: int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      
    }
  }
out:
  return 0;
}






PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>
Task: Flow-control structures



C#: int GetNumber() {
    return 5;
}




PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>
Task: Flow-control structures


C++: #include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}





PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>
Task: Flow-control structures




Go: func main() {
inf:
    goto inf
}



PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>
Task: Flow-control structures





Java: switch (xx) {
  case 1:
  case 2:
    
    ...
    break;
  case 4:
    
    ...
    break;
  case 5:
    
    ...
    break;
  default:
    
    break;
}

for (int i = 0; i < 10; ++i) {
  ...
  if (some_condition) { break; }
  ...
}

_Time_: do {
  for (int i = 0; i < 10; ++i) {
    ...
    if (some_condition) { break _Time_; }
    ...
    }
  ...
} while (thisCondition);


PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>
Task: Flow-control structures






PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>


Python: 
for i in range(n):
    if (n%2) == 0:
        continue
    if (n%i) == 0:
        result = i
        break
else:
    result = None
    print "No odd factors found"
Task: Flow-control structures






PHP: <?php
goto a;
echo 'Foo';
 
a:
echo 'Bar';
?>



VB:   Sub bar2()
      Dim x = 0
      GoTo label
      x = 5
label:
      Console.WriteLine(x)
  End Sub
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;
}


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



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






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)]