Commit 6d8e630

mo <mokha@cisco.com>
2017-06-27 19:14:27
find entry point of cycle.
1 parent 30894cb
spec/cycle_spec.rb
@@ -58,10 +58,40 @@ describe "cycle" do
     tortoise, hare = head, head.next_item
 
     until hare.nil?
-      return tortoise.next_item if tortoise == hare
+      break if tortoise == hare
       tortoise, hare = tortoise.next_item, hare.next_item&.next_item
     end
-    nil
+
+    tortoise = head
+    until hare == tortoise
+      tortoise = tortoise.next_item
+      hare = hare.next_item
+    end
+    return tortoise
+  end
+
+  def entry_point(head)
+    return nil if head.nil?
+
+    tortoise, hare = head, head.next_item
+
+    until hare.nil?
+      break if tortoise == hare
+      tortoise, hare = tortoise.next_item, hare.next_item&.next_item
+    end
+
+    return nil if tortoise.nil? || hare.nil?
+
+    visited = {}
+    tortoise = head
+    until hare == tortoise
+      tortoise = tortoise.next_item
+      hare = hare.next_item
+      return hare if visited[hare]
+
+      visited[hare] = true
+    end
+    return tortoise
   end
 
   class Node
spec/rotate_image_spec.rb
@@ -31,6 +31,14 @@ DOC
 
 describe "rotate_image" do
   def rotate_image(image)
+    0.upto(image.size) do |i|
+      row = image[i]
+      puts row.inspect
+      0.upto(row.size - 1) do |j|
+
+        puts row[j].inspect
+      end
+    end
     image
   end