From ab634908debacef72001ec04a572eba227f0f5db Mon Sep 17 00:00:00 2001 From: devalexandre Date: Tue, 31 Dec 2024 13:05:50 -0300 Subject: [PATCH 1/3] fix: update golang loop --- loops/go/code.go | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/loops/go/code.go b/loops/go/code.go index 3b016dc6..3a557132 100644 --- a/loops/go/code.go +++ b/loops/go/code.go @@ -1,22 +1,26 @@ package main + import ( - "fmt" - "math/rand" - "strconv" - "os" + "fmt" + "math/rand" + "os" + "strconv" ) func main() { - input, e := strconv.Atoi(os.Args[1]) // Get an input number from the command line - if e != nil { panic(e) } - u := int32(input) - r := rand.Int31n(10000) // Get a random number 0 <= r < 10k - var a[10000]int32 // Array of 10k elements initialized to 0 - for i := int32(0); i < 10000; i++ { // 10k outer loop iterations - for j := int32(0); j < 100000; j++ { // 100k inner loop iterations, per outer loop iteration - a[i] = a[i] + j%u // Simple sum + input, e := strconv.Atoi(os.Args[1]) + if e != nil { + panic(e) + } + u := int32(input) + var soma int32 + for j := int32(0); j < 100000; j++ { + soma += j % u + } + r := rand.Int31n(10000) + var a [10000]int32 + for i := int32(0); i < 10000; i++ { + a[i] = soma + r } - a[i] += r // Add a random value to each element in array - } - fmt.Println(a[r]) // Print out a single element from the array + fmt.Println(a[r]) } From 698dcaa739f0c8522f6dbdad5a7ef456eacb8a65 Mon Sep 17 00:00:00 2001 From: devalexandre Date: Tue, 31 Dec 2024 13:07:50 -0300 Subject: [PATCH 2/3] fix: update golang fibonacci --- fibonacci/go/code.go | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/fibonacci/go/code.go b/fibonacci/go/code.go index 17a38bf0..9025de30 100644 --- a/fibonacci/go/code.go +++ b/fibonacci/go/code.go @@ -1,23 +1,32 @@ package main + import ( - "fmt" - "strconv" - "os" + "fmt" + "os" + "strconv" ) func fibonacci(n int) int { - if n == 0 { return 0 } - if n == 1 { return 1 } - return fibonacci(n-1) + fibonacci(n-2) + if n < 2 { + return n + } + a, b := 0, 1 + for i := 2; i <= n; i++ { + a, b = b, a+b + } + return b } func main() { - input, e := strconv.Atoi(os.Args[1]) - if e != nil { panic(e) } - u := int(input) - r := 0 - for i := 0; i < u; i++ { - r += fibonacci(i) - } - fmt.Println(r) + if len(os.Args) < 2 { + fmt.Println("Uso: go run main.go ") + return + } + u, err := strconv.Atoi(os.Args[1]) + if err != nil { + panic(err) + } + result := fibonacci(u+1) - 1 + + fmt.Println(result) } From 85fd51baf55da30560a4fbfef8c07004ff97bd0a Mon Sep 17 00:00:00 2001 From: devalexandre Date: Tue, 31 Dec 2024 13:11:25 -0300 Subject: [PATCH 3/3] fix: update golang levenshtein --- levenshtein/go/code.go | 130 ++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 72 deletions(-) diff --git a/levenshtein/go/code.go b/levenshtein/go/code.go index df06d2cc..209b5db2 100644 --- a/levenshtein/go/code.go +++ b/levenshtein/go/code.go @@ -5,91 +5,77 @@ import ( "os" ) -// levenshtein calculates the Levenshtein distance between two strings using Wagner-Fischer algorithm -// Space Complexity: O(min(m,n)) - only uses two rows instead of full matrix -// Time Complexity: O(m*n) where m and n are the lengths of the input strings -func levenshtein(s1, s2 string) int { - // Early termination checks - if s1 == s2 { - return 0 - } - if len(s1) == 0 { - return len(s2) - } - if len(s2) == 0 { - return len(s1) - } - // Make s1 the shorter string for space optimization - if len(s1) > len(s2) { - s1, s2 = s2, s1 - } +func levenshtein(s1, s2 string) int { + // Early termination + if s1 == s2 { + return 0 + } + if len(s1) == 0 { + return len(s2) + } + if len(s2) == 0 { + return len(s1) + } - m, n := len(s1), len(s2) + if len(s1) > len(s2) { + s1, s2 = s2, s1 + } - // Use two rows instead of full matrix for space optimization - prevRow := make([]int, m+1) - currRow := make([]int, m+1) + m, n := len(s1), len(s2) - // Initialize first row - for i := 0; i <= m; i++ { - prevRow[i] = i - } + prevRow := make([]int, m+1) + currRow := make([]int, m+1) - // Main computation loop - for j := 1; j <= n; j++ { - currRow[0] = j + for i := 0; i <= m; i++ { + prevRow[i] = i + } - for i := 1; i <= m; i++ { - cost := 1 - if s1[i-1] == s2[j-1] { - cost = 0 - } - - // Find minimum of three operations - deletion := prevRow[i] + 1 - insertion := currRow[i-1] + 1 - substitution := prevRow[i-1] + cost - - currRow[i] = deletion - if insertion < currRow[i] { - currRow[i] = insertion - } - if substitution < currRow[i] { - currRow[i] = substitution - } - } + for j := 1; j <= n; j++ { + currRow[0] = j + for i := 1; i <= m; i++ { + cost := 1 + if s1[i-1] == s2[j-1] { + cost = 0 + } + del := prevRow[i] + 1 // deletar char de s1 + ins := currRow[i-1] + 1 // inserir char em s1 + sub := prevRow[i-1] + cost // substituir char - // Swap rows - prevRow, currRow = currRow, prevRow - } + currRow[i] = del + if ins < currRow[i] { + currRow[i] = ins + } + if sub < currRow[i] { + currRow[i] = sub + } + } + prevRow, currRow = currRow, prevRow + } - return prevRow[m] + return prevRow[m] } func main() { - args := os.Args[1:] - - minD := -1 - times := 0 + args := os.Args[1:] + if len(args) < 2 { + fmt.Println("Use: go run main.go [ ...]") + return + } - if len(args) < 2 { - fmt.Println("Please provide at least two strings as arguments.") - return - } + minD := -1 + times := 0 - for i := 0; i < len(args); i++ { - for j := 0; j < len(args); j++ { - if i != j { - d := levenshtein(args[i], args[j]) - if minD == -1 || d < minD { - minD = d + for i := 0; i < len(args)-1; i++ { + for j := i + 1; j < len(args); j++ { + d := levenshtein(args[i], args[j]) + if minD == -1 || d < minD { + minD = d + } + times++ } - times++ - } - } - } + } - fmt.Printf("times: %d\n", times) - fmt.Printf("min_distance: %d\n", minD) + fmt.Printf("times: %d\n", times) + fmt.Printf("min_distance: %d\n", minD) }