Guest User

Untitled

a guest
Jan 6th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type Node struct {
  8. name string
  9. children []Node
  10. }
  11.  
  12. var data Node = Node{
  13. name: "var",
  14. children: []Node {
  15. Node {
  16. name: "lib",
  17. children: []Node {},
  18. },
  19. Node {
  20. name: "log",
  21. children: []Node {
  22. Node {
  23. name: "apt",
  24. children: []Node {},
  25. },
  26. Node {
  27. name: "exim4",
  28. children: []Node {},
  29. },
  30. },
  31. },
  32. Node {
  33. name: "www",
  34. children: []Node {
  35. Node {
  36. name: "html",
  37. children: []Node {
  38. Node {
  39. name: "assets",
  40. children: []Node {
  41. Node {
  42. name: "images",
  43. children: []Node {},
  44. },
  45. Node {
  46. name: "audio",
  47. children: []Node {},
  48. },
  49. },
  50. },
  51. Node {
  52. name: "sites",
  53. children: []Node {
  54. Node {
  55. name: "main",
  56. children: []Node {},
  57. },
  58. Node {
  59. name: "secondary",
  60. children: []Node {},
  61. },
  62. },
  63. },
  64. },
  65. },
  66. },
  67. },
  68. },
  69. }
  70.  
  71. func recPrint(n *Node, depth int){
  72.  
  73. for i := 0; i < depth; i++ {
  74. // to print pretty
  75. fmt.Printf("└─ ")
  76. }
  77.  
  78. fmt.Printf("%s\n", n.name)
  79.  
  80. for _, child := range n.children {
  81. recPrint(&child, (depth + 1))
  82. }
  83.  
  84. }
  85.  
  86. func recFind(n *Node, toFind []string) [][]string {
  87. // empty result array
  88. retStrings := [][]string {}
  89.  
  90.  
  91. if (len(toFind) == 1 && toFind[0] == n.name) || toFind == nil {
  92. // is it the last matching node?
  93. // or is it a child of the last matching node?
  94. retStrings = append(retStrings, []string {n.name})
  95. toFind = nil
  96. } else if len(toFind) > 1 {
  97. // search for next match in children
  98. toFind = toFind[1:]
  99. } else {
  100. return retStrings
  101. }
  102.  
  103. for i := 0; i < len(n.children); i++ {
  104.  
  105. // get all match children, and children of matches
  106. // toFind is contextual to the above
  107. childString := recFind(&n.children[i], toFind)
  108.  
  109. for j := 0; j < len(childString); j++ {
  110.  
  111. // if children were returned, create entries for them to be
  112. // appended to
  113. retStrings = append(retStrings, []string {n.name})
  114.  
  115. // get last index of array
  116. lastRS := len(retStrings) - 1
  117. // append new child to last index of array
  118. retStrings[lastRS] = append(retStrings[lastRS], childString[j]...)
  119.  
  120. }
  121.  
  122. }
  123.  
  124. return retStrings
  125.  
  126. }
  127.  
  128. func main () {
  129.  
  130. fmt.Println("RAW: ")
  131. fmt.Println(data)
  132. fmt.Println()
  133.  
  134. fmt.Println("PRETTY:")
  135. recPrint(&data, 0)
  136. fmt.Println()
  137.  
  138. toFind := []string{ "var", "www", "html" }
  139.  
  140. matches := recFind(&data, toFind)
  141.  
  142. fmt.Println()
  143. fmt.Println("MATCHES:")
  144. for i := 0; i < len(matches); i++ {
  145. fmt.Println(matches[i])
  146. }
  147.  
  148. }
Add Comment
Please, Sign In to add comment