From 93bd23b848b424a35260cbc2c8a31c3282d33713 Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 5 Feb 2024 08:49:14 +0000 Subject: [PATCH] Added factorial exercise and cleaned up repo --- array-sorter/.gitignore | 23 ------------- array-sorter/gen-numbers.sh | 13 ------- array-sorter/go.mod | 5 --- array-sorter/go.sum | 2 -- array-sorter/main.go | 69 ------------------------------------- factorial/go.mod | 3 ++ factorial/main.go | 32 +++++++++++++++++ 7 files changed, 35 insertions(+), 112 deletions(-) delete mode 100644 array-sorter/.gitignore delete mode 100755 array-sorter/gen-numbers.sh delete mode 100644 array-sorter/go.mod delete mode 100644 array-sorter/go.sum delete mode 100644 array-sorter/main.go create mode 100644 factorial/go.mod create mode 100644 factorial/main.go diff --git a/array-sorter/.gitignore b/array-sorter/.gitignore deleted file mode 100644 index dcc4674..0000000 --- a/array-sorter/.gitignore +++ /dev/null @@ -1,23 +0,0 @@ -# ---> Go -# If you prefer the allow list template instead of the deny list, see community template: -# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore -# -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib -*.txt - -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Dependency directories (remove the comment below to include it) -# vendor/ - -# Go workspace file -go.work diff --git a/array-sorter/gen-numbers.sh b/array-sorter/gen-numbers.sh deleted file mode 100755 index 9b92ceb..0000000 --- a/array-sorter/gen-numbers.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -FILE="./array-sorter" - -echo {1..300} > ./numbers.txt -cat ./numbers.txt | tr ' ' "\\n" | shuf | tr "\\n" ' ' > ./random.txt -if test -f ./array-sorter; then - echo "$FILE exists, running" - -else - echo "$FILE doesn't exist, building" - go build -o $FILE . -fi -./array-sorter $(cat ./random.txt) diff --git a/array-sorter/go.mod b/array-sorter/go.mod deleted file mode 100644 index 0399acc..0000000 --- a/array-sorter/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module array-sorter - -go 1.21.6 - -require git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70 diff --git a/array-sorter/go.sum b/array-sorter/go.sum deleted file mode 100644 index 73bf989..0000000 --- a/array-sorter/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70 h1:q/uQwkakAZnaus16KHG6UkBFZzNaMbjY3SCZjh0Pytk= -git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70/go.mod h1:PdMyknNgNZYKg03bYhSc4aaSA/P3yScGPfxC4z+ju0c= diff --git a/array-sorter/main.go b/array-sorter/main.go deleted file mode 100644 index 4f857a7..0000000 --- a/array-sorter/main.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import ( - "fmt" - "git.bulgariu.xyz/raul/min-max-array" - "os" - "strconv" -) - -var numArray = []float64{} -var numToAdd int -var err error - -func main() { - if len(os.Args) < 2 { - fmt.Println("Tool to automatically sort an array from the smallest number to the largest") - fmt.Println("Usage: ./array-sorter 20 5 10") - os.Exit(1) - } - for i := 1; i < (len(os.Args)); i++ { - numToAdd, err = strconv.Atoi(os.Args[i]) - numArray = append(numArray, float64(numToAdd)) - } - - if err != nil { - panic(err) - } - - fmt.Println("RESULTS:") - fmt.Printf("%v\n", numArray) - - fmt.Printf("\nSORTED RESULTS:\n") - sortedArray := sorter(numArray) - fmt.Printf("%v\n", sortedArray) -} - -func sorter(arr []float64) (orderedArr []float64) { - if len(arr) == 0 { - return nil - } - - var numLocation int - //var numValue float64 - sliced_arr := arr - - // IT WAS THIS BASTARD WHO KEPT CAUSING OUT OF BOUNDS ERRORS - // I KEPT READING THE LENGTH OF THE ORIGINAL ARRAY INSTEAD OF THE - // ARRAY BEING SLOWLY EMPTIED - for i := 0; i < len(sliced_arr); { - // fmt.Printf("Current slice length: %v\n", sliced_arr) - // fmt.Println(orderedArr) - - mini := minmax.Min(sliced_arr) - orderedArr = append(orderedArr, mini) - - for i, v := range sliced_arr { - if v == mini { - numLocation = i - //numValue = v - break - } - } - - //fmt.Printf("\nDEBUGGING WIZARD:\nThe smallest value is %v and its position is %v\n\n", numValue, numLocation) - - sliced_arr = append(sliced_arr[:numLocation], sliced_arr[numLocation+1:]...) - } - return orderedArr -} diff --git a/factorial/go.mod b/factorial/go.mod new file mode 100644 index 0000000..6d85eb8 --- /dev/null +++ b/factorial/go.mod @@ -0,0 +1,3 @@ +module factorial + +go 1.21.6 diff --git a/factorial/main.go b/factorial/main.go new file mode 100644 index 0000000..27f1bcb --- /dev/null +++ b/factorial/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +var numFinal int = 1 +var multiplicado int = 1 + +func main() { + if len(os.Args) < 2 || len(os.Args) > 2 { + fmt.Println("Usage: ./factorial 10") + os.Exit(1) + } + + arg := os.Args[1] + + numFactorial, err := strconv.Atoi(arg) + if err != nil { + fmt.Println(err) + } + + // TODO: Fix this garbage + + for i := 1; i <= numFactorial; i++ { + numFinal = numFinal * i + } + + fmt.Println("El nĂºmero factorizado es:", numFinal) +}