Snippets

cia_rana 左と右の有理数

Updated by cia_rana

File codeiq.2965.rb Modified

  • Ignore whitespace
  • Hide word diff
   [x, y - x]
 end
 def decide_right x, y
-  i = 0
   a, b = x, y
-  loop do
+  n = loop.with_index do |_, i|
     return [?-] if a * 2 == b
     c, d = from_left_child(a, b)
     if c > d
       a, b = from_right_child(a, b)
     elsif c * 2 < d
       a, b = to_right_child(c, d)
-      break
+      break i
     else
       a, b = c, d
     end
-    i += 1
   end
-  [a, a * i + b]
+  [a, a * n + b]
 end
 def decide_left x, y
-  i = 0
   a, b = x, y
-  loop do
+  n = loop.with_index do |_, i|
     return [?-] if a * 2 == b
     c, d = from_left_child(a, b)
     if c > d
       a, b = to_left_child *from_right_child(a, b)
-      break
+      break i
     else
       a, b = c, d
     end
-    i += 1
   end
-  (1..i).inject([a, b]){ |(c, d), _| c * 2 > d ? to_left_child(c, d) : to_right_child(c, d) }
+  (1..n).inject([a, b]){ |(c, d), _| c * 2 > d ? to_left_child(c, d) : to_right_child(c, d) }
 end
 
 x, y = gets.scan(/\d+/).map(&:to_i)
Updated by cia_rana

File codeiq.2965.rb Modified

  • Ignore whitespace
  • Hide word diff
     end
     i += 1
   end
-  1.upto(i){ a, b = a * 2 > b ? to_left_child(a, b) : to_right_child(a, b) }
-  [a, b]
+  (1..i).inject([a, b]){ |(c, d), _| c * 2 > d ? to_left_child(c, d) : to_right_child(c, d) }
 end
 
 x, y = gets.scan(/\d+/).map(&:to_i)
Updated by cia_rana

File codeiq.2965.rb Modified

  • Ignore whitespace
  • Hide word diff
     end
     i += 1
   end
-  (0...i).each{ a, b = a * 2 > b ? to_left_child(a, b) : to_right_child(a, b) }
+  1.upto(i){ a, b = a * 2 > b ? to_left_child(a, b) : to_right_child(a, b) }
   [a, b]
 end
 
Updated by cia_rana

File codeiq.2965.rb Modified

  • Ignore whitespace
  • Hide word diff
     c, d = from_left_child(a, b)
     if c > d
       a, b = from_right_child(a, b)
-      i += 1
-      next
     elsif c * 2 < d
       a, b = to_right_child(c, d)
       break
     else
       a, b = c, d
-      i += 1
     end
+    i += 1
   end
   [a, a * i + b]
 end
       break
     else
       a, b = c, d
-      i += 1
     end
+    i += 1
   end
   (0...i).each{ a, b = a * 2 > b ? to_left_child(a, b) : to_right_child(a, b) }
   [a, b]
Updated by cia_rana

File codeiq.2965.rb Modified

  • Ignore whitespace
  • Hide word diff
       i += 1
     end
   end
-  (0...i).each{ a, b = to_left_child(a, b) }
-  [a, b]
+  [a, a * i + b]
 end
 def decide_left x, y
   i = 0
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.