Advertisement
Guest User

Untitled

a guest
May 31st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. cat nodes
  2. #!/usr/bin/env ruby
  3.  
  4. module Chain
  5. class Node
  6. attr_accessor :left, :right, :node_count, :name
  7.  
  8. def initialize(name)
  9. @name = name
  10. @node_count = 0
  11. end
  12.  
  13. def has_left?
  14. !left.nil?
  15. end
  16.  
  17. def has_right?
  18. !right.nil?
  19. end
  20.  
  21. def send_left(data)
  22. if has_left?
  23. left.from_right(data)
  24. elsif has_right?
  25. # found the left boundary, bounce right with the final count.
  26. send_right(node_count: data[:counter])
  27. @node_count = data[:counter]
  28. end
  29. end
  30.  
  31. def send_right(data)
  32. if has_right?
  33. right.from_left(data)
  34. elsif has_left?
  35. # found right boundary, bounce left with the counter.
  36. left.from_right(counter: 1) if data[:counter]
  37. end
  38. end
  39.  
  40. def from_left(data)
  41. @node_count = data[:node_count] if data[:node_count]
  42. send_right(data)
  43. end
  44.  
  45. def from_right(data)
  46. data[:counter] += 1 if data[:counter]
  47. send_left(data)
  48. end
  49. end
  50.  
  51. class List
  52. attr_accessor :nodes
  53. end
  54. end
  55.  
  56. # Setup
  57. node_x = Chain::Node.new("node_x")
  58. node_y = Chain::Node.new("node_y")
  59. node_z = Chain::Node.new("node_z")
  60. node_a = Chain::Node.new("node_a")
  61. node_x.right = node_y
  62. node_y.left = node_x
  63. node_y.right = node_z
  64. node_z.left = node_y
  65. node_z.right = node_a
  66. node_a.left = node_z
  67.  
  68. list = Chain::List.new
  69. list.nodes = [node_x, node_y, node_z, node_a].shuffle
  70. puts "node counts before"
  71. list.nodes.each { |node| puts "Node: #{node.name} Count: #{node.node_count}" }
  72. list.nodes.each { |node| node.send_right(counter: 0) }
  73. puts "node counts after"
  74. list.nodes.each { |node| puts "Node: #{node.name} Count: #{node.node_count}" }
  75.  
  76. # Setup
  77. node_a = Chain::Node.new("node_a")
  78. node_b = Chain::Node.new("node_b")
  79. node_c = Chain::Node.new("node_c")
  80. node_d = Chain::Node.new("node_d")
  81. node_e = Chain::Node.new("node_e")
  82. node_f = Chain::Node.new("node_f")
  83. node_g = Chain::Node.new("node_g")
  84.  
  85. node_a.right = node_b
  86. node_b.left = node_a
  87. node_b.right = node_c
  88. node_c.left = node_b
  89. node_c.right = node_d
  90. node_d.left = node_c
  91. node_d.right = node_e
  92. node_e.left = node_d
  93. node_e.right = node_f
  94. node_f.left = node_e
  95. node_f.right = node_g
  96. node_g.left = node_f
  97.  
  98. list = Chain::List.new
  99. list.nodes = [node_a, node_b, node_c, node_d, node_e, node_f, node_g].shuffle
  100. puts "node counts before"
  101. list.nodes.each { |node| puts "Node: #{node.name} Count: #{node.node_count}" }
  102. list.nodes.each { |node| node.send_right(counter: 0) }
  103. puts "node counts after"
  104. list.nodes.each { |node| puts "Node: #{node.name} Count: #{node.node_count}" }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement