Skip to content

Commit

Permalink
fixup python and php
Browse files Browse the repository at this point in the history
  • Loading branch information
bddicken committed Dec 18, 2024
1 parent 3c54677 commit 14b8d05
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
6 changes: 3 additions & 3 deletions levenshtein/js/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function levenshteinDistance(str1, str2) {
const n = str2.length;

// Use TypedArrays for better performance
const prevRow = new Uint32Array(m + 1);
const currRow = new Uint32Array(m + 1);
let prevRow = new Uint32Array(m + 1);
let currRow = new Uint32Array(m + 1);

// Initialize the first row
for (let i = 0; i <= m; i++) {
Expand Down Expand Up @@ -90,4 +90,4 @@ function main(args) {
// Handle command-line arguments for Node.js
if (typeof process !== 'undefined' && process.argv) {
process.exit(main(process.argv.slice(2)));
}
}
41 changes: 24 additions & 17 deletions levenshtein/php/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,40 @@

function levenshtein_distance(string $str1, string $str2): int
{
if (strlen($str2) < strlen($str1)) {
return levenshtein_distance($str2, $str1);
}
$m = strlen($str1);
$n = strlen($str2);

// Create a matrix to store distances
$matrix = [];
$prev = [];
$curr = [];

// Initialize first row and column
for ($i = 0; $i <= $m; $i++) {
$matrix[$i][0] = $i;
}
for ($j = 0; $j <= $n; $j++) {
$matrix[0][$j] = $j;
$prev[$i] = $i;
}

// Compute Levenshtein distance
for ($i = 1; $i <= $m; $i++) {
for ($j = 1; $j <= $n; $j++) {
$cost = (int) $str1[$i - 1] != $str2[$j - 1];
$matrix[$i][$j] = min(
$matrix[$i - 1][$j] + 1, // Deletion
$matrix[$i][$j - 1] + 1, // Insertion
$matrix[$i - 1][$j - 1] + $cost // Substitution
for ($i = 1; $i <= $n; $i++) {
$curr[0] = $i;
for ($j = 1; $j <= $m; $j++) {
$cost = 0;
if($str1[$j - 1] != $str2[$i - 1]) {
$cost = 1;
}
$curr[$j] = min(
$prev[$j] + 1, // Deletion
$curr[$j - 1] + 1, // Insertion
$prev[$j - 1] + $cost // Substitution
);
}
for ($j = 0; $j <= $m; $j++) {
$prev[$j] = $curr[$j];
}
}

return $matrix[$m][$n];

//var_dump($prev);
return $prev[$m];
}

// Main function to mimic C program's behavior
Expand Down Expand Up @@ -59,4 +66,4 @@ function main(array $args): void
// Skip script name.
unset($argv[0]);
// Call main.
main($argv);
main($argv);
32 changes: 17 additions & 15 deletions levenshtein/py/code.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
def levenshtein_distance(str1: str, str2: str) -> int:
if (len(str2) < len(str1)):
return levenshtein_distance(str2, str1)
m, n = len(str1), len(str2)

# Create a matrix to store distances
matrix = [[0] * (n + 1) for _ in range(m + 1)]
prev = [0] * (m+1)
curr = [0] * (m+1)

# Initialize first row and column
for i in range(m + 1):
matrix[i][0] = i
for j in range(n + 1):
matrix[0][j] = j
prev[i] = i

# Compute Levenshtein distance
for i in range(1, m + 1):
for j in range(1, n + 1):
for i in range(1, n + 1):
curr[0] = i
for j in range(1, m + 1):
# Cost is 0 if characters match, 1 if they differ
cost = 0 if str1[i-1] == str2[j-1] else 1
matrix[i][j] = min(
matrix[i-1][j] + 1, # Deletion
matrix[i][j-1] + 1, # Insertion
matrix[i-1][j-1] + cost # Substitution
cost = 0 if str1[j-1] == str2[i-1] else 1
curr[j] = min(
prev[j] + 1, # Deletion
curr[j-1] + 1, # Insertion
prev[j-1] + cost # Substitution
)
for j in range(m+1):
prev[j] = curr[j]

return matrix[m][n]
return prev[m]

def main():
import sys
Expand All @@ -34,7 +36,7 @@ def main():

# Compare each pair of arguments exactly once
for i in range(len(args)):
for j in range(i+1, len(args)):
for j in range(len(args)):
if i != j:
distance = levenshtein_distance(args[i], args[j])
if min_distance == -1 or distance < min_distance:
Expand Down
9 changes: 7 additions & 2 deletions levenshtein/swift/code.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Darwin

/// Calculates the Levenshtein distance between two strings using Wagner-Fischer algorithm
/// Space Complexity: O(min(m,n)) - only uses two arrays instead of full matrix
/// Time Complexity: O(m*n) where m and n are the lengths of the input strings
Expand Down Expand Up @@ -44,7 +46,9 @@ func levenshteinDistance(_ s1: String, _ s2: String) -> Int {
}

// Main program
func main() {
@main
struct Main {
static func main() {
// Get command line arguments (skipping the program name)
let args = Array(CommandLine.arguments.dropFirst())

Expand All @@ -70,6 +74,7 @@ func main() {
print("times: \(times)")
print("min_distance: \(minDistance)")
}
}

// Run main program
main()
//main()
4 changes: 2 additions & 2 deletions levenshtein/zig/code.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub fn main() !void {
while (j < args.len) : (j += 1) {
if (i != j) {
const distance = try levenshteinDistance(allocator, args[i], args[j]);
if (min_distance == -1 or distance < @intCast(usize, min_distance)) {
min_distance = @intCast(isize, distance);
if (min_distance == -1 or distance < @as(usize, @intCast(min_distance))) {
min_distance = @as(isize, @intCast(distance));
}
times += 1;
}
Expand Down
9 changes: 4 additions & 5 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ run "Objective-C" "" "./objc/code"
run "Kotlin JVM" "java -jar" "kotlin/code.jar"
run "Kotlin Native" "" "./kotlin/code.kexe"
run "Node" "node" "./js/code.js"
run "Node (jitless)" "node --jitless" "./js/code.js"
run "Bun" "bun" "./js/code.js"
run "Bun (Compiled)" "" "./js/bun"
run "Bun (jitless)" "bun" "./js/code.js" "BUN_JSC_useJIT=0"
run "Deno (jitless)" "deno --v8-flags=--jitless" "./js/code.js"
run "Deno" "deno" "./js/code.js"
run "PyPy" "pypy" "./py/code.py"
run "CPP" "" "./cpp/code"
run "Go" "" "./go/code"
run "Node (jitless)" "node --jitless" "./js/code.js"
run "Bun (jitless)" "bun" "./js/code.js" "BUN_JSC_useJIT=0"
run "Deno (jitless)" "deno --v8-flags=--jitless" "./js/code.js"
run "Java" "java" "jvm.code"
run "Java" "jvava" "jvm.code"
run "Scala" "" "./scala/code"
run "Scala-Native" "" "./scala/code-native"
run "Ruby" "ruby" "./ruby/code.rb"
run "PHP JIT" "php -dopcache.enable_cli=1 -dopcache.jit=on -dopcache.jit_buffer_size=64M" "./php/code.php"
run "PHP" "php" "./php/code.php"
run "R" "Rscript" "./r/code.R"
Expand Down

0 comments on commit 14b8d05

Please sign in to comment.