From 6c9aeb651a49ff60f1415591a805068e8248f2c6 Mon Sep 17 00:00:00 2001 From: vvarga Date: Thu, 19 Dec 2024 11:49:05 +0100 Subject: [PATCH] Fix Dart code with standard dart formatting tool --- levenshtein/dart/code.dart | 46 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/levenshtein/dart/code.dart b/levenshtein/dart/code.dart index 2cda0ec..b058ce7 100644 --- a/levenshtein/dart/code.dart +++ b/levenshtein/dart/code.dart @@ -4,28 +4,28 @@ int levenshteinDistance(String word1, String word2) { final m = word1.length; final n = word2.length; // Create a matrix to store distances - var matrix = List.generate(m+1, (i) => List.generate(n+1, (j) => 0)); + var matrix = List.generate(m + 1, (i) => List.generate(n + 1, (j) => 0)); - // Initialize first row and column + // Initialize first row and column for (int i = 1; i <= m; i++) { - matrix[i][0] = i; + matrix[i][0] = i; } for (int j = 1; j <= n; j++) { - matrix[0][j] = j; + matrix[0][j] = j; } - + // Compute Levenshtein distance for (int i = 1; i <= m; i++) { - for (int j = 1; j <= n; j++) { - int cost = word1[i-1] == word2[j-1]? 0:1; - matrix[i][j] = min( - min( - matrix[i-1][j] + 1, // Deletion - matrix[i][j-1] + 1 // Insertion - ), - matrix[i-1][j-1] + cost // Substitution - ); - } + for (int j = 1; j <= n; j++) { + int cost = word1[i - 1] == word2[j - 1] ? 0 : 1; + matrix[i][j] = min( + min( + matrix[i - 1][j] + 1, // Deletion + matrix[i][j - 1] + 1 // Insertion + ), + matrix[i - 1][j - 1] + cost // Substitution + ); + } } return matrix[m][n]; } @@ -40,15 +40,15 @@ void main(List args) { int times = 0; // Compare each pair of arguments exactly once for (int i = 0; i < args.length; i++) { - for (int j = i+1; j < args.length; j++) { - if (i != j) { - int distance = levenshteinDistance(args[i], args[j]); - if (minDistance == -1 || distance < minDistance ) { - minDistance = distance; - } - times++; + for (int j = i + 1; j < args.length; j++) { + if (i != j) { + int distance = levenshteinDistance(args[i], args[j]); + if (minDistance == -1 || distance < minDistance) { + minDistance = distance; } + times++; } + } } // The only output from the program should be the times (number of comparisons) @@ -56,4 +56,4 @@ void main(List args) { // formatted exactly like this. print("times: " + times.toString()); print("min_distance: " + minDistance.toString()); -} \ No newline at end of file +}