golanglabs

Log | Files | Refs | README | LICENSE

commit 3373f83348fc6bf902085a2dcaa5c533c8f41abd
parent 7705977950056c7badaf28db2709c2843c25cf30
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Sat,  6 Apr 2024 16:53:49 +0200

Restructuring of lab files.

Diffstat:
Ddsa/merge-sort.go | 83-------------------------------------------------------------------------------
Asrc/dsa/fisher_yates.go | 5+++++
Asrc/dsa/merge_sort.go | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rhelloworld/hello.go -> src/helloworld/hello.go | 0
Asrc/main.go | 31+++++++++++++++++++++++++++++++
Asrc/routing_webserver/routes.json | 8++++++++
Asrc/routing_webserver/src/about.html | 0
Asrc/routing_webserver/src/home.html | 0
Asrc/routing_webserver/webserver.go | 32++++++++++++++++++++++++++++++++
Rsimple-webserver/webserver.go -> src/simple_webserver/webserver.go | 0
10 files changed, 133 insertions(+), 83 deletions(-)

diff --git a/dsa/merge-sort.go b/dsa/merge-sort.go @@ -1,83 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "math" -) - -func main() { - v := []int{9, 8, 6, 3, 1, 1, 11, 4} - printArr(v) - printArr(mergeSort(v)) -} - -// MergeSort O(n * log(n)) -func mergeSort(v []int) []int { - - if len(v) == 1 { - return v - } - - q1 := 0 - q2 := int(math.Ceil(float64(len(v)) / 2)) - q3 := len(v) - - v1 := v[q1:q2] - v2 := v[q2:q3] - - v1 = mergeSort(v1) - v2 = mergeSort(v2) - - return merge(v1, v2) -} - -// Merge the two halves of the mergeSort. -func merge(v1 []int, v2 []int) []int { - var v3 []int - - v1pos, v2pos := 0, 0 // keeps track of the current index at which the next element should be picked - - // If both halves contain elements - for len(v1) > v1pos && len(v2) > v2pos { - if v1[v1pos] < v2[v2pos] { - v3 = append(v3, v1[v1pos]) - v1pos++ - } else { - v3 = append(v3, v2[v2pos]) - v2pos++ - } - } - - // If right is empty - for len(v1) > v1pos { - v3 = append(v3, v1[v1pos]) - v1pos++ - } - - // If left is empty - for len(v2) > v2pos { - v3 = append(v3, v2[v2pos]) - v2pos++ - } - - return v3 -} - -// print an []int array -func printArr(v []int) { - - var buffer bytes.Buffer - buffer.WriteString("{") - - for i, n := range v { - buffer.WriteString(fmt.Sprintf("%d", n)) - if i < len(v)-1 { - buffer.WriteString(", ") - } - } - - buffer.WriteString("}") - - fmt.Println(buffer.String()) -} diff --git a/src/dsa/fisher_yates.go b/src/dsa/fisher_yates.go @@ -0,0 +1,5 @@ +package dsa + +func FisherYates() { + +} diff --git a/src/dsa/merge_sort.go b/src/dsa/merge_sort.go @@ -0,0 +1,57 @@ +package dsa + +import ( + "math" +) + +// MergeSort O(n * log(n)) +func MergeSort(v []int) []int { + + if len(v) == 1 { + return v + } + + q1 := 0 + q2 := int(math.Ceil(float64(len(v)) / 2)) + q3 := len(v) + + v1 := v[q1:q2] + v2 := v[q2:q3] + + v1 = MergeSort(v1) + v2 = MergeSort(v2) + + return merge(v1, v2) +} + +// Merge the two halves of the mergeSort. +func merge(v1 []int, v2 []int) []int { + var v3 []int + + v1pos, v2pos := 0, 0 // keeps track of the current index at which the next element should be picked + + // If both halves contain elements + for len(v1) > v1pos && len(v2) > v2pos { + if v1[v1pos] < v2[v2pos] { + v3 = append(v3, v1[v1pos]) + v1pos++ + } else { + v3 = append(v3, v2[v2pos]) + v2pos++ + } + } + + // If right is empty + for len(v1) > v1pos { + v3 = append(v3, v1[v1pos]) + v1pos++ + } + + // If left is empty + for len(v2) > v2pos { + v3 = append(v3, v2[v2pos]) + v2pos++ + } + + return v3 +} diff --git a/helloworld/hello.go b/src/helloworld/hello.go diff --git a/src/main.go b/src/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "bytes" + "fmt" + + "github.com/LindholmLabs/golanglabs/src/dsa" +) + +func main() { + slice := []int{1, 2, 3, 4} + dsa.MergeSort(slice) +} + +// print an []int array +func PrintArr(v []int) { + + var buffer bytes.Buffer + buffer.WriteString("{") + + for i, n := range v { + buffer.WriteString(fmt.Sprintf("%d", n)) + if i < len(v)-1 { + buffer.WriteString(", ") + } + } + + buffer.WriteString("}") + + fmt.Println(buffer.String()) +} diff --git a/src/routing_webserver/routes.json b/src/routing_webserver/routes.json @@ -0,0 +1,7 @@ +{ + "/": "home.html", + "/home": "home.html", + "/about": "about.html", + "/home.css": "css/home.css", + "/about.css": "css/about.css" +} +\ No newline at end of file diff --git a/src/routing_webserver/src/about.html b/src/routing_webserver/src/about.html diff --git a/src/routing_webserver/src/home.html b/src/routing_webserver/src/home.html diff --git a/src/routing_webserver/webserver.go b/src/routing_webserver/webserver.go @@ -0,0 +1,32 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" +) + +func main() { + +} + +type Routes struct { + route string `json:"path"` +} + +func loadRoutes() { + jsonFile, err := os.Open("routes.json") + + if err != nil { + fmt.Println(err) + } + + fmt.Println("Loaded routes.") + + defer jsonFile.Close() + + byteValue, _ := ioutil.ReadAll(jsonFile) + var routes Routes + json.Unmarshal(byteValue, &routes) +} diff --git a/simple-webserver/webserver.go b/src/simple_webserver/webserver.go