Begin a new Kumite
Search
About
  • Filter by Language:
  • Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

    You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

    A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

require 'mongoid'
File.open('mongoid.yml', 'w') do |file|
  file.write <<-CONFIG
development:
  sessions:
    default:
      database: mongoid
      hosts:
        - localhost:27017
CONFIG
end
fork do
  exec 'mongod'
end

sleep 1 # need to find a better way to wait for mongo to startup

ENV['RACK_ENV'] = 'development'
Mongoid.load!('mongoid.yml')

class User
  include Mongoid::Document
  field :name
end
Mathematics
Algorithms
Numbers

Using Liebniz' formula... converges attrociously slowing...

(->> (iterate inc 0) 
     (map #(-> (Math/pow -1 %) (/ (inc (* 2 %))))) 
     (take 1200001) 
     (reduce +) 
     (* 4) 
     println)
Numbers
Interview Questions
Code
Diff
  • (ns number-to-words
      (:require [clojure.string :refer [join]]))
    
    (def digit-to-word
      {1 "One"  2 "Two" 3 "Three" 4 "Four"
       5 "Five" 6 "Six" 7 "Seven" 8 "Eight"
       9 "Nine"})
    
    (def tens-to-word
      {
       20 "Twenty" 30 "Thirty" 40 "Fourty"
       50 "Fifty"  60 "Sixty"  70 "Seventy"
       80 "Eighty" 90 "Ninety"})
    
    (def teen-to-word
      {10 "Ten"      11 "Eleven"    12 "Twelve"
       13 "Thirteen" 14 "Fourteen"  15 "Fifteen"  
       16 "Sixteen"  17 "Seventeen" 18 "Eighteen"
       19 "Nineteen"})
    
    (defn divmod [n d] [(quot n d) (mod n d)])
    
    (defn tens [n & {:keys [zero] :or {zero ""}}] 
      (let [[tens ones] (divmod n 10)
            digit       (digit-to-word ones)]
            (condp = tens 
              0 (or digit zero)
              1 (teen-to-word n)
                (str (tens-to-word (* 10 tens)) (when digit (str "-" digit))))))
                
    (defn order [d name f n]
      (let [[hs ts] (divmod n d)]
            (str ""
              (when (> hs 0) (str (f hs) " " name))
              (when (> ts 0) (str (when (> hs 0) " ") (f ts))))))
    
    (def hundreds (partial order 100 "Hundred" tens))
    
    (def orders ["" " Thousand" " Million" " Billion" " Trillion"])
              
    (defn addp [lst n o]
      (if (> n 0) (conj lst (str (hundreds n) (orders o))) lst))
              
    (defn convert- [x]
      (join ", "
        (loop [i 0, n x, out '()]
          (let [[a b] (divmod n 1000)]
            (cond
             (>= a 1000) (recur (inc i) a (addp out b i))
             (>  a 0)    (-> out (addp b i) (addp a (inc i)))
             :else       (addp out b i)
             )))))
    
    (defn convert [x]
      (case (compare x 0)
        -1 (str "Negative " (convert- (- (int x))))
         0 "Zero"
         1 (convert- (int x))
         nil))
    
  • 11
    (ns number-to-words
    
    22
      (:require [clojure.string :refer [join]]))
    
    33
    44
    (def digit-to-word
    
    5
      {1 "One"
    
    6
       2 "Two"
    
    7
       3 "Three"
    
    8
       4 "Four"
    
    9
       5 "Five"
    
    10
       6 "Six"
    
    11
       7 "Seven"
    
    12
       8 "Eight"
    
    5+
      {1 "One"  2 "Two" 3 "Three" 4 "Four"
    
    6+
       5 "Five" 6 "Six" 7 "Seven" 8 "Eight"
    
    1313
       9 "Nine"})
    
    1414
    1515
    (def tens-to-word
    
    1616
      {
    
    17
       20 "Twenty"
    
    18
       30 "Thirty"
    
    19
       40 "Fourty"
    
    20
       50 "Fifty"
    
    21
       60 "Sixty"
    
    22
       70 "Seventy"
    
    23
       80 "Eighty"
    
    24
       90 "Ninety"})
    
    11+
       20 "Twenty" 30 "Thirty" 40 "Fourty"
    
    12+
       50 "Fifty"  60 "Sixty"  70 "Seventy"
    
    13+
       80 "Eighty" 90 "Ninety"})
    
    33
       16 "Sixteen"
    
    34
       17 "Seventeen"
    
    35
       18 "Eighteen"
    
    16+
      {10 "Ten"      11 "Eleven"    12 "Twelve"
    
    17+
       13 "Thirteen" 14 "Fourteen"  15 "Fifteen"  
    
    18+
       16 "Sixteen"  17 "Seventeen" 18 "Eighteen"
    
    3636
       19 "Nineteen"})
    
    3737
    38
    (defn- small-number-to-words
    
    39
      [x]
    
    40
      (cond
    
    41
       (<= x 9) (digit-to-word x)
    
    42
       (< x 20) (teen-to-word x)
    
    21+
    (defn divmod [n d] [(quot n d) (mod n d)])
    
    22+
    33+
            (str ""
    
    34+
              (when (> hs 0) (str (f hs) " " name))
    
    35+
              (when (> ts 0) (str (when (> hs 0) " ") (f ts))))))
    
    36+
    37+
    (def hundreds (partial order 100 "Hundred" tens))
    
    38+
    39+
    (def orders ["" " Thousand" " Million" " Billion" " Trillion"])
    
    40+
              
    
    41+
    (defn addp [lst n o]
    
    42+
      (if (> n 0) (conj lst (str (hundreds n) (orders o))) lst))
    
int main()
{
  printf("%d\n", test());
}

int test()
{
  __asm__("movl $1337, %eax");
}
Redis
NoSQL
Databases

A basic example of how to run a Redis in ruby on CW.

fork do
  exec "redis-server"
end

require 'redis'
r = Redis.new

r.set('a', 'b')

When you have a thread ouputting to stdout, and you want to catch its output in a test case...

(defmacro with-out-str-not-thread-safe
  "A version of clojure.core/with-out-str that is not thread-safe"
  [& body]
  `(let [s# (StringWriter.)]
     (with-redefs [*out* s#]
       ~@body
       (str s#))))
Recursion
Algorithms
Computability Theory
Theoretical Computer Science
Arrays
Methods
Functions
Object-oriented Programming
Control Flow
Basic Language Features
Fundamentals
Classes
Code
Diff
  • class Array # Opens up the Array class for method creation
      def flip
        self.reverse.map{|e| e.is_a?(Array) ? e.flip : e}
      end
    end
  • 11
    class Array # Opens up the Array class for method creation
    
    22
      def flip
    
    3
        result = []
    
    4
        me = self.reverse # Don't fret, I'll sort the Arrays out!
    
    5
        
    
    6
        me.each do |x|
    
    7
          
    
    8
          if x.class == Array then result << x.flip # Here's the recursion.
    
    9
          else result << x
    
    10
          end
    
    11
          
    
    12
        end
    
    13
        
    
    14
        return result
    
    3+
        self.reverse.map{|e| e.is_a?(Array) ? e.flip : e}
    
    1515
      end
    
    1616
    end
    
class Solution {
    public static void main(String[] args) {
        System.out.println("Hello darkness, my old friend...");
    }
}
Recursion
Algorithms
Computability Theory
Theoretical Computer Science

A Tail recursive factorial function

Code
Diff
  • Factorial = fun(N) when N > 0 ->
    
      Tail = fun (Y, Acc, 0) ->  Acc;
                 (Y, Acc, N) -> Y(Y, Acc * N, N - 1) 
             end,
             
      Tail(Tail, 1, N)
    end,
    
    io:format("~s~n", [Factorial(5)]),
    
  • 1
    Factorial = fun (Y, 0) ->  1;
    
    2
          (Y, N) -> N * Y(Y, N - 1)
    
    3
      end,
    
    1+
    Factorial = fun(N) when N > 0 ->
    
    44
    5
    io:format("~p~n", [Factorial(Factorial, 5)]),
    
    3+
      Tail = fun (Y, Acc, 0) ->  Acc;
    
    4+
                 (Y, Acc, N) -> Y(Y, Acc * N, N - 1) 
    
    5+
             end,
    
    6+
             
    
    7+
      Tail(Tail, 1, N)
    
    8+
    end,
    
    66
    7
    init:stop().
    
    10+
    io:format("~s~n", [Factorial(5)]),