Ad
Fundamentals
Code
Diff
  • Object.const_set(
      :Odd,
      Class.new do
        define_method :odd do |n|
          (n & 1) == 1
        end
      end
    )
    • class Odd
    • def odd(n)
    • n.odd?
    • Object.const_set(
    • :Odd,
    • Class.new do
    • define_method :odd do |n|
    • (n & 1) == 1
    • end
    • end
    • end
    • )
Fundamentals
Games
Code
Diff
  • def riddle(w)=w=~/\?/
    • def riddle(word)
    • word.downcase.count "a-z"
    • end
    • def riddle(w)=w=~/\?/
Code
Diff
  • def multiply_and_add_one(a, b):
        if a < 0 and b < 0:
            return multiply_and_add_one(abs(a), abs(b))  
        elif a > b:
            return multiply_and_add_one(b, a)
        elif b != 0:
            return a + multiply_and_add_one(a, b - 1)
        elif b == 0:
            return 1
        else:
            return 0
    
    • def multiply_and_add_one(a, b):
    • if a > b:
    • if a < 0 and b < 0:
    • return multiply_and_add_one(abs(a), abs(b))
    • elif a > b:
    • return multiply_and_add_one(b, a)
    • elif b != 0:
    • return a + multiply_and_add_one(a, b - 1)
    • elif b == 0:
    • return 1
    • else:
    • return 0

20 characters saved

min() key is now a tuple generator

tuples are compared (sorted) by index 0 then on conflict subsequent indices

index 0 is abs() like before
index 1 is negative reciprocal so positive numbers are sorted before negative numbers

Code
Diff
  • ClosestToZero=lambda n:type("",(),{"e":lambda:min(n,key=lambda x:(abs(x),-x),default=0)})
    • ClosestToZero=lambda n:type("",(),{"e":lambda v=min(n,key=abs,default=0):dict(zip(n,n)).get(abs(v),v),"n":n})
    • ClosestToZero=lambda n:type("",(),{"e":lambda:min(n,key=lambda x:(abs(x),-x),default=0)})
Code
Diff
  • # Or just make string equality case insensitive.
    
    class CaseInsensitiveString(str):
        def __eq__(self, other):
            return other.lower() == self.lower()
    
    fun=lambda x: CaseInsensitiveString(x)
    • fun=str.upper
    • # Or just make string equality case insensitive.
    • class CaseInsensitiveString(str):
    • def __eq__(self, other):
    • return other.lower() == self.lower()
    • fun=lambda x: CaseInsensitiveString(x)
Code
Diff
  • def multiply_and_add_one(a, b):
        r = 0
        
        while b != 0:
            r += a if b & 1 else 0
            
            a <<= 1
            b >>= 1
            
        return r + 1
    • multiply_and_add_one = lambda a, b: a * b + 1
    • def multiply_and_add_one(a, b):
    • r = 0
    • while b != 0:
    • r += a if b & 1 else 0
    • a <<= 1
    • b >>= 1
    • return r + 1
Code
Diff
  • def max_number(n):
        d = 0
        
        # Get number of digits
        while n // (10 ** d):
            d += 1
        
        # Get digit of n with length d at index i
        def digit(n, d, i):
            return int((n // (10 ** (d - i - 1)))) % 10
            
        # Swap digits of n in place
        def swap(n, d, i, j):
            di = digit(n, d, i)
            dj = digit(n, d, j)
            
            n -= di * (10 ** (d - i - 1))
            n -= dj * (10 ** (d - j - 1))
            n += di * (10 ** (d - j - 1))
            n += dj * (10 ** (d - i - 1))
            
            return n
    
        x = None
    
        # Bubble sort an integer in place
        while x != n:
            x = n
    
            for i in range(d):
                di = digit(n, d, i)
                for j in range(d - 1, i, -1):
                    dj = digit(n, d, j)
                    
                    # Swap condition if you'd prefer smallest int
                    if di < dj:
                        n = swap(n, d, i, j)
                        break
                        
                if x != n:
                    break
                    
        return n
    • import java.util.Arrays;
    • def max_number(n):
    • d = 0
    • # Get number of digits
    • while n // (10 ** d):
    • d += 1
    • # Get digit of n with length d at index i
    • def digit(n, d, i):
    • return int((n // (10 ** (d - i - 1)))) % 10
    • # Swap digits of n in place
    • def swap(n, d, i, j):
    • di = digit(n, d, i)
    • dj = digit(n, d, j)
    • n -= di * (10 ** (d - i - 1))
    • n -= dj * (10 ** (d - j - 1))
    • n += di * (10 ** (d - j - 1))
    • n += dj * (10 ** (d - i - 1))
    • return n
    • public class MaxNumber {
    • public static long print(long number) {
    • return number
    • }
    • }
    • x = None
    • # Bubble sort an integer in place
    • while x != n:
    • x = n
    • for i in range(d):
    • di = digit(n, d, i)
    • for j in range(d - 1, i, -1):
    • dj = digit(n, d, j)
    • # Swap condition if you'd prefer smallest int
    • if di < dj:
    • n = swap(n, d, i, j)
    • break
    • if x != n:
    • break
    • return n
Code
Diff
  • add = sum
    • def add(*a):
    • sum = 0
    • for i in a:
    • sum += i
    • print(sum)
    • add = sum
Code
Diff
  • def multiply_and_add_one(a, b):
        if a > b:
            return multiply_and_add_one(b, a)
        
        elif b != 0:
            return a + multiply_and_add_one(a, b - 1)
        
        elif b == 0:
            return 1
        
        else:
            return 0
    
    • def multiply_and_add_one(a, b):
    • return (a * b) + 1
    • if a > b:
    • return multiply_and_add_one(b, a)
    • elif b != 0:
    • return a + multiply_and_add_one(a, b - 1)
    • elif b == 0:
    • return 1
    • else:
    • return 0
Code
Diff
  • package kata
    
    func SumLetters(a string, b string) bool {
      return len(a) == len(b) && len(a) > 0
    }
    • def verify_sum(a, b)
    • a&&b&&a.sum==b.sum||false
    • end
    • package kata
    • func SumLetters(a string, b string) bool {
    • return len(a) == len(b) && len(a) > 0
    • }
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
Loading more items...