Ad
Fundamentals
Games
Code
Diff
  • def riddle(w) = w=~/\?/
    • def riddle(w) = w.index("?")
    • def riddle(w) = w=~/\?/
Code
Diff
  • def verify_sum(a, b)
     a&&b&&a.sum==b.sum||false
    end
    
    • def verify_sum(a, b)
    • return false if a.nil? || b.nil?
    • a.sum == b.sum
    • a&&b&&a.sum==b.sum||false
    • end
Code
Diff
  • def is_even(x):
        return not x & 1
    
    • def is_even(x):
    • return 1 ^ x % 2
    • return not x & 1
Code
Diff
  • def verify_sum(a,b)
      a&.sum == b&.sum
    end
    • def verify_sum(a,b)
    • a.sum == b.sum rescue false
    • a&.sum == b&.sum
    • end
Fundamentals
Games
Code
Diff
  • def riddle(w)=/\?/=~w
    • riddle = lambda w:w.index("?")
    • def riddle(w)=/\?/=~w
Fundamentals
Games
Code
Diff
  • def riddle(w, l=0):
        if w == "?":
            return l
        else:
            return riddle(w[1:], l+1)
    • def riddle(word): return word.rindex("?")
    • def riddle(w, l=0):
    • if w == "?":
    • return l
    • else:
    • return riddle(w[1:], l+1)
Fundamentals
Games
Code
Diff
  • def riddle(word):
        return word.rindex("?")
    • def riddle(word):
    • return len(word) - 1
    • return word.rindex("?")

A bit faster:

Rehearsal ------------------------------------
   3.207147   0.008056   3.215203 (  3.215506)
   0.760552   0.000013   0.760565 (  0.760609)
   1.287006   0.000000   1.287006 (  1.287230)
   0.466994   0.000000   0.466994 (  0.467006)
   0.105617   0.000005   0.105622 (  0.105649)
   0.065559   0.000017   0.065576 (  0.065609)
--------------------------- total: 5.900966sec

       user     system      total        real
   1.493939   0.000000   1.493939 (  1.494093) # Eileenandrea
   0.785715   0.000000   0.785715 (  0.785772) # Eileenandrea, a==b check
   1.238191   0.000018   1.238209 (  1.238325) # 深紅心
   0.484410   0.000000   0.484410 (  0.484429) # 深紅心, a==b check
   0.076156   0.000000   0.076156 (  0.076182) # pawptart
   0.065472   0.000000   0.065472 (  0.065472) # pawptart (refactored)
Code
Diff
  • def one_away(a, b)
      return true if a == b
      
      case a.size - b.size
      when -1 then compare(a, b)
      when 0 then compare(b, a, 1)
      when 1 then compare(b, a)
      else false
      end
    end
    
    def compare(a, b, o = 0)  
      (0..b.size).each do |i|
        return a[0, i] == b[0, i] && a[i + o, a.size] == b[i + 1, b.size] if a[i] != b[i]
      end
      
      true
    end
    • def one_away(a, b)
    • return true if a == b
    • case a.size - b.size
    • when -1..1 then compare(a, b)
    • when -1 then compare(a, b)
    • when 0 then compare(b, a, 1)
    • when 1 then compare(b, a)
    • else false
    • end
    • end
    • def compare(a, b)
    • c = 0
    • i = 0
    • x, y = [a, b].sort_by(&:size) # Don't have to do this, but it makes the code easier
    • x_off = x.size < y.size ? 0 : 1 # Used in case the string sizes are mismatched
    • # Find the first difference
    • # Use the index to split the strings into substrings
    • # Compare the substrings
    • while i < y.size
    • if x[i] != y[i]
    • return x[0, i] == y[0, i] && x[i + x_off, x.size] == y[i + 1, y.size]
    • end
    • i += 1
    • def compare(a, b, o = 0)
    • (0..b.size).each do |i|
    • return a[0, i] == b[0, i] && a[i + o, a.size] == b[i + 1, b.size] if a[i] != b[i]
    • end
    • true
    • end

Added some benchmarks, improved some existing solutions

Substring comparison is orders of magnitude faster than iterating over each letter of the string in Ruby.

== drops down into C to perform the comparison instead of relying on Ruby's relatively slow iteration.

Sample benchmark:

Rehearsal ------------------------------------
   2.953821   0.028788   2.982609 (  2.983072) # Eileenandrea
   1.069810   0.000000   1.069810 (  1.069971) # 深紅心
   0.414153   0.000074   0.414227 (  0.414269) # 深紅心 (modified)
   0.087044   0.003973   0.091017 (  0.091033) # pawptart
--------------------------- total: 4.557663sec

       user     system      total        real
   1.521839   0.000000   1.521839 (  1.522046)
   1.044602   0.000028   1.044630 (  1.044804)
   0.407469   0.000000   0.407469 (  0.407515)
   0.065739   0.000000   0.065739 (  0.065745)
Code
Diff
  • def one_away(a, b)
      return true if a == b
      
      case a.size - b.size
      when -1..1 then compare(a, b)
      else false
      end
    end
    
    def compare(a, b)
      c     = 0
      i     = 0
      x, y  = [a, b].sort_by(&:size)  # Don't have to do this, but it makes the code easier
      x_off = x.size < y.size ? 0 : 1 # Used in case the string sizes are mismatched
      
      # Find the first difference
      # Use the index to split the strings into substrings
      # Compare the substrings
      while i < y.size
        if x[i] != y[i]
          return x[0, i] == y[0, i] && x[i + x_off, x.size] == y[i + 1, y.size]
        end
        i += 1                        
      end
    end
    • def one_away(a,b)
    • case a.length - b.length
    • when 0
    • i = 0
    • while i < a.length && a[i] == b[i]
    • i += 1
    • end
    • i += 1
    • # probably slower than just using substring equality,
    • while i < a.length && a[i] == b[i]
    • i += 1
    • end
    • # but the allure of pretty complexities is too much
    • return i >= a.length
    • when 1
    • return helper(a,b)
    • when -1
    • return helper(b, a)
    • else
    • return false
    • def one_away(a, b)
    • return true if a == b
    • case a.size - b.size
    • when -1..1 then compare(a, b)
    • else false
    • end
    • end
    • def helper(longer, shorter)
    • i = 0
    • while i < shorter.length && longer[i] == shorter[i]
    • i += 1
    • end
    • while i < shorter.length && longer[i + 1] == shorter[i]
    • i += 1
    • def compare(a, b)
    • c = 0
    • i = 0
    • x, y = [a, b].sort_by(&:size) # Don't have to do this, but it makes the code easier
    • x_off = x.size < y.size ? 0 : 1 # Used in case the string sizes are mismatched
    • # Find the first difference
    • # Use the index to split the strings into substrings
    • # Compare the substrings
    • while i < y.size
    • if x[i] != y[i]
    • return x[0, i] == y[0, i] && x[i + x_off, x.size] == y[i + 1, y.size]
    • end
    • i += 1
    • end
    • return i >= shorter.length
    • end
Recursion
Algorithms
Computability Theory
Logic
Theoretical Computer Science
Arrays
Data Types
Methods
Functions
Object-oriented Programming
Control Flow
Basic Language Features
Fundamentals
Programming Paradigms
Classes
Code
Diff
  • class Array
      def flip
        reverse.map{|e|e.flip rescue e}
      end
    end
    • class Array # Opens up the Array class for method creation
    • class Array
    • def flip
    • self.reverse.map{|e| e.is_a?(Array) ? e.flip : e}
    • reverse.map{|e|e.flip rescue e}
    • end
    • end
Code
Diff
  • from functools import reduce
    
    def prod(numbers):
        return reduce(lambda x, y: x * y, numbers or [0], 1)
        
    • from functools import reduce
    • def prod(numbers):
    • return numbers[0] * (prod(numbers[1:]) or 1) if numbers else 0
    • return reduce(lambda x, y: x * y, numbers or [0], 1)
Fundamentals
Puzzles
Games
Code
Diff
  • # It doesn't get any simpler, but we can definitely make it harder!
    
    def minimal_square(a, b):
        # This solution abuses the fact that we can multiply by True/False:
        # For instance, 1 * True = 1 and 1 * False = 0
        #
        # A step further:
        # (a * (a > b ) + b * ( b >= a )) is equivalent to max(a, b)
        #
        # And:
        # (a * ( a < b ) + b * ( b <= a )) is equivalent to min(a, b)
        #
        # Putting it all together, the following is equivalent to:
        # max(min(a,b)*2,max(a,b))**2
    
        side = (((a*(a<b)+b*(b<=a))*2)*(((a*(a<b)+b*(b<=a))*2)>(a*(a>b)+b*(b>=a))))+\
            ((a*(a>b)+b*(b>=a))*(((a*(a<b)+b*(b<=a))*2)<=(a*(a>b)+b*(b>=a))))
        return side**2
    
    • # It doesn't get any simpler, but we can definitely make it harder!
    • def minimal_square(a, b):
    • return max(min(a, b)*2, max(a, b))**2
    • # This is my first kumite (∩_∩)
    • # I think this is the best solution there's not much to improve on
    • # The problem isn't too hard
    • # This solution abuses the fact that we can multiply by True/False:
    • # For instance, 1 * True = 1 and 1 * False = 0
    • #
    • # A step further:
    • # (a * (a > b ) + b * ( b >= a )) is equivalent to max(a, b)
    • #
    • # And:
    • # (a * ( a < b ) + b * ( b <= a )) is equivalent to min(a, b)
    • #
    • # Putting it all together, the following is equivalent to:
    • # max(min(a,b)*2,max(a,b))**2
    • side = (((a*(a<b)+b*(b<=a))*2)*(((a*(a<b)+b*(b<=a))*2)>(a*(a>b)+b*(b>=a))))+\
    • ((a*(a>b)+b*(b>=a))*(((a*(a<b)+b*(b<=a))*2)<=(a*(a>b)+b*(b>=a))))
    • return side**2
Mathematics
Algorithms
Logic
Numbers
Data Types
Code
Diff
  • def average(g)
      g==[] ? nil : g.sum.to_f/g.size
    end 
    • def average(g)
    • g.size < 1 ? nil : g.reduce(:+).to_f / g.size
    • g==[] ? nil : g.sum.to_f/g.size
    • end

Add in non-arithmetic operators

Code
Diff
  • def calculator(a, operator, b):
        return "Not a valid operation" if operator not in ['+', '-', '/', '*'] else eval(str(a) + operator + str(b))
    
    • def calculator(a, operator, b):
    • return eval(str(a) + operator + str(b))
    • return "Not a valid operation" if operator not in ['+', '-', '/', '*'] else eval(str(a) + operator + str(b))
Code
Diff
  • def calculator(a, operator, b):
        return eval(str(a) + operator + str(b))
    
    • def calculator(a, operator, b):
    • operations = {"+": lambda x, y: x + y, "-": lambda x, y: x - y,
    • "*": lambda x, y: x * y, "/": lambda x, y: x / y}
    • return operations[operator](a, b)
    • return eval(str(a) + operator + str(b))
Loading more items...