Created
March 4, 2020 21:03
-
-
Save knabben/06b018544f54d2f9717189da5a2c63ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package graph | |
| import ( | |
| "fmt" | |
| "sort" | |
| ) | |
| type Graph struct { | |
| Nodes map[string]Fields | |
| Links map[string]map[string]Fields | |
| } | |
| func NewGraph() *Graph { | |
| return &Graph{ | |
| Nodes: make(map[string]Fields), | |
| Links: make(map[string]map[string]Fields), | |
| } | |
| } | |
| // Check if node already exists | |
| func (g *Graph) NodeExists(name string) bool { | |
| if _, exists := g.Nodes[name]; exists { | |
| return true | |
| } | |
| return false | |
| } | |
| // AddNode insert a node in the graph | |
| func (g *Graph) AddNode(objectType Fields) { | |
| name := objectType.Name | |
| if(!g.NodeExists(name)) { | |
| g.Nodes[name] = objectType | |
| } | |
| } | |
| // AddEdge | |
| func (g *Graph) AddEdge(vertexName1, vertexName2 string) bool { | |
| if !g.NodeExists(vertexName1) || !g.NodeExists(vertexName2) { | |
| return false | |
| } | |
| vertex2 := g.Nodes[vertexName2] | |
| var exists bool | |
| if _, exists = g.Links[vertexName1]; !exists { | |
| g.Links[vertexName1] = make(map[string]Fields) | |
| } | |
| g.Links[vertexName1][vertexName2] = vertex2 | |
| return true | |
| } | |
| func (g *Graph) DepthFirstSearch(callback func(root string, vertex Fields)) { | |
| for root, m := range g.Links { | |
| for vertex := range m { | |
| callback(root, vertex) | |
| } | |
| } | |
| } | |
| func BuildGraph(s Schema) { | |
| g := NewGraph() | |
| fields := []Fields{} | |
| for i, t := range s.Schema.QueryType.Fields { | |
| fields = append(fields, t) | |
| fmt.Printf("Node: %d %s \n", i, t.Name) | |
| g.AddNode(t) | |
| } | |
| // Sort slices by alpha ordering. | |
| sort.Slice(fields, func(i, j int) bool { | |
| return fields[i].Name < fields[j].Name }) | |
| // Split the first level of ancestor of the graph. | |
| step := len(fields)/3 | |
| // Add the other first level ancestors. | |
| for n := 0; n < len(fields); n += step { | |
| ancestors := fields[n:step + n] | |
| for _, field := range ancestors[1:] { | |
| firstName := ancestors[0].Name | |
| g.AddEdge(firstName, field.Name) | |
| } | |
| } | |
| // Traverse edges and do request. | |
| g.DepthFirstSearch() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tree | |
| import "fmt" | |
| type Node struct { | |
| Data int64 | |
| Next *Node | |
| } | |
| func NewNode(data int64) Node { | |
| return Node{Data: data} | |
| } | |
| func (n *Node) Iterate(head Node) { | |
| node := head; | |
| fmt.Println(node.Data) | |
| for node.Next != nil { | |
| node = *node.Next | |
| fmt.Println(node.Data); | |
| } | |
| } | |
| func (n *Node) AppendTail(data int64) { | |
| endNode := NewNode(data) | |
| node := n | |
| for node.Next != nil { | |
| node = node.Next | |
| } | |
| node.Next = &endNode | |
| } | |
| func (n *Node) DeleteNode(head Node, data int64) *Node { | |
| if head.Data == data { | |
| return head.Next | |
| } | |
| node := head | |
| for node.Next != nil { | |
| if node.Next.Data == data { | |
| node.Next = node.Next.Next | |
| return &node | |
| } | |
| node = *node.Next | |
| } | |
| return &node | |
| } | |
| package tree | |
| import ( | |
| "fmt" | |
| "sort" | |
| ) | |
| func Run(a int64) int64 { | |
| if a == 0 { | |
| return 0 | |
| } | |
| fmt.Println(a) | |
| return Run(a - 1) | |
| } | |
| func Run1(s []int, i int) bool { | |
| sort.Ints(s) | |
| fmt.Println(s) | |
| middle := len(s) / 2 | |
| if middle == 0 { | |
| return false | |
| } | |
| if s[middle] == i { | |
| return true | |
| } | |
| if i > s[middle] { | |
| return Run1(s[middle:len(s)], i) | |
| } else if i < s[middle] { | |
| return Run1(s[:middle], i) | |
| } | |
| return false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment