Added array-sorter exercise

This commit is contained in:
raul 2024-02-02 08:18:21 +00:00
parent 4b34de5781
commit 2bcbe7edc9
2 changed files with 48 additions and 0 deletions

3
array-sorter/go.mod Normal file
View File

@ -0,0 +1,3 @@
module array-sorter
go 1.21.6

45
array-sorter/main.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"os"
"strconv"
)
var numArray = []float64{}
var numToAdd int
var err error
func main() {
if len(os.Args) < 2 {
fmt.Println("[-] Not enough arguments")
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.Println(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
}
for i := 0; i < len(arr); i++ {
for _, v := range arr {
orderedArr = append(orderedArr, v)
}
}
return
}