Added factorial exercise and cleaned up repo

This commit is contained in:
raul 2024-02-05 08:49:14 +00:00
parent 5df40a8847
commit 93bd23b848
7 changed files with 35 additions and 112 deletions

View File

@ -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

View File

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

View File

@ -1,5 +0,0 @@
module array-sorter
go 1.21.6
require git.bulgariu.xyz/raul/min-max-array v0.0.0-20240131105739-41c2fcc5af70

View File

@ -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=

View File

@ -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
}

3
factorial/go.mod Normal file
View File

@ -0,0 +1,3 @@
module factorial
go 1.21.6

32
factorial/main.go Normal file
View File

@ -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)
}