Commit 7a5fd0c

mo <mokha@cisco.com>
2017-06-22 17:11:38
complete part 3.
1 parent 7fd5fd4
Changed files (1)
spec/cycle_spec.rb
@@ -13,6 +13,10 @@ A -> B -> C -> D -> E -> F -> D
 |C|F|
 |D|E|
 |E|D|
+
+Part 3.
+could you find the entry point of the circle?
+which means the point A for (a->b->c->a)
 DOC
 
 describe "cycle" do
@@ -48,6 +52,18 @@ describe "cycle" do
     0
   end
 
+  def entry_point(head)
+    return nil if head.nil?
+
+    tortoise, hare = head, head.next_item
+
+    until hare.nil?
+      return tortoise.next_item if tortoise == hare
+      tortoise, hare = tortoise.next_item, hare.next_item&.next_item
+    end
+    nil
+  end
+
   class Node
     attr_reader :next_item, :value
 
@@ -116,4 +132,45 @@ A -> B -> C -> D -> E -> F -> D
     expect(circle?(a)).to be_falsey
     expect(cycle_length(a)).to eql(0)
   end
+
+  it 'detects the entry point' do
+    # (a->b->c->a)
+    # a|b
+    # b|a
+    # c|c
+    a = Node.new("A")
+    b = Node.new("B")
+    c = Node.new("C")
+    a.add(b)
+    b.add(c)
+    c.add(a)
+
+    expect(entry_point(a)).to eql(a)
+  end
+
+
+  it 'detects entry point' do
+    # A -> B -> C -> D -> C is C the entry point
+    a = Node.new("A")
+    b = Node.new("B")
+    c = Node.new("C")
+    d = Node.new("D")
+    a.add(b)
+    b.add(c)
+    c.add(d)
+    d.add(c)
+
+    expect(entry_point(a)).to eql(c)
+  end
+
+  it 'finds no entry point' do
+    #(a->b->c)
+    a = Node.new("A")
+    b = Node.new("B")
+    c = Node.new("C")
+    a.add(b)
+    b.add(c)
+
+    expect(entry_point(a)).to be_nil
+  end
 end