New Shared Library
This commit is contained in:
16
shared/Board.go
Normal file
16
shared/Board.go
Normal file
@ -0,0 +1,16 @@
|
||||
package shared
|
||||
|
||||
type Board struct {
|
||||
Values [5][5]BoardValue
|
||||
Hash map[int]Location
|
||||
}
|
||||
|
||||
type BoardValue struct {
|
||||
Value int
|
||||
Visited bool
|
||||
}
|
||||
|
||||
type Location struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
43
shared/Coordinate.go
Normal file
43
shared/Coordinate.go
Normal file
@ -0,0 +1,43 @@
|
||||
package shared
|
||||
|
||||
type Coordinate struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func (c *Coordinate) Neighbours(gridWidth int, gridHeight int, diagonal bool) (out []Coordinate) {
|
||||
spaceLeft := c.X > 0
|
||||
spaceRight := c.X < gridWidth-1
|
||||
spaceUp := c.Y > 0
|
||||
spaceDown := c.Y < gridHeight-1
|
||||
|
||||
if spaceLeft {
|
||||
out = append(out, Coordinate{c.X - 1, c.Y})
|
||||
}
|
||||
if spaceRight {
|
||||
out = append(out, Coordinate{c.X + 1, c.Y})
|
||||
}
|
||||
if spaceUp {
|
||||
out = append(out, Coordinate{c.X, c.Y - 1})
|
||||
}
|
||||
if spaceDown {
|
||||
out = append(out, Coordinate{c.X, c.Y + 1})
|
||||
}
|
||||
|
||||
if diagonal {
|
||||
if spaceUp && spaceLeft {
|
||||
out = append(out, Coordinate{c.X - 1, c.Y - 1})
|
||||
}
|
||||
if spaceUp && spaceRight {
|
||||
out = append(out, Coordinate{c.X + 1, c.Y - 1})
|
||||
}
|
||||
if spaceDown && spaceLeft {
|
||||
out = append(out, Coordinate{c.X - 1, c.Y + 1})
|
||||
}
|
||||
if spaceDown && spaceRight {
|
||||
out = append(out, Coordinate{c.X + 1, c.Y + 1})
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
21
shared/LinkedList.go
Normal file
21
shared/LinkedList.go
Normal file
@ -0,0 +1,21 @@
|
||||
package shared
|
||||
|
||||
import "fmt"
|
||||
|
||||
type LinkedList struct {
|
||||
Head *Node
|
||||
}
|
||||
|
||||
func (l *LinkedList) InsertItem(first *Node, second *Node, value string) {
|
||||
first.Next = &Node{Value: value, Next: second}
|
||||
}
|
||||
|
||||
func (l *LinkedList) PrintList() {
|
||||
list := []string{}
|
||||
current := l.Head
|
||||
for current != nil {
|
||||
list = append(list, current.Value)
|
||||
current = current.Next
|
||||
}
|
||||
fmt.Println(list)
|
||||
}
|
6
shared/Node.go
Normal file
6
shared/Node.go
Normal file
@ -0,0 +1,6 @@
|
||||
package shared
|
||||
|
||||
type Node struct {
|
||||
Value string
|
||||
Next *Node
|
||||
}
|
39
shared/Sort.go
Normal file
39
shared/Sort.go
Normal file
@ -0,0 +1,39 @@
|
||||
package shared
|
||||
|
||||
func MergeSort(nums []int, start int, end int) []int {
|
||||
if start == end {
|
||||
return []int{nums[start]}
|
||||
}
|
||||
|
||||
var mid int = ((end - start) / 2) + start
|
||||
|
||||
//Assign values back into Left and right
|
||||
left := MergeSort(nums, start, mid)
|
||||
right := MergeSort(nums, mid+1, end)
|
||||
|
||||
var combined []int
|
||||
|
||||
//Pointers for new array
|
||||
leftPointer, rightPointer := 0, 0
|
||||
|
||||
for leftPointer <= len(left)-1 || rightPointer <= len(right)-1 {
|
||||
|
||||
if leftPointer == len(left) {
|
||||
addValue(&combined, right[rightPointer], &rightPointer)
|
||||
} else if rightPointer == len(right) {
|
||||
addValue(&combined, left[leftPointer], &leftPointer)
|
||||
} else {
|
||||
if left[leftPointer] <= right[rightPointer] {
|
||||
addValue(&combined, left[leftPointer], &leftPointer)
|
||||
} else {
|
||||
addValue(&combined, right[rightPointer], &rightPointer)
|
||||
}
|
||||
}
|
||||
}
|
||||
return combined
|
||||
}
|
||||
|
||||
func addValue(nums *[]int, value int, pointer *int) {
|
||||
*nums = append(*nums, value)
|
||||
*pointer++
|
||||
}
|
33
shared/Stack.go
Normal file
33
shared/Stack.go
Normal file
@ -0,0 +1,33 @@
|
||||
package shared
|
||||
|
||||
type Stack struct {
|
||||
head *Node
|
||||
}
|
||||
|
||||
func (s *Stack) Push(item string) {
|
||||
if s.head == nil {
|
||||
s.head = &Node{
|
||||
Value: item,
|
||||
Next: nil,
|
||||
}
|
||||
} else {
|
||||
s.head = &Node{
|
||||
Value: item,
|
||||
Next: s.head,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stack) Pop() string {
|
||||
node := s.Peek()
|
||||
s.head = node.Next
|
||||
return node.Value
|
||||
}
|
||||
|
||||
func (s *Stack) Peek() (node *Node) {
|
||||
node = s.head
|
||||
if node == nil {
|
||||
node = &Node{}
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user