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.

MongoDB
NoSQL
Databases
Code
Diff
  • import subprocess
    import re
    mongod = subprocess.Popen("mongod", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    # @jhoffner: I listen to my child process until it prints "waiting for connections" instead of sleeping...
    while True:
      l = mongod.stdout.readline()
      m = re.match(r".*waiting for connections on port 27017", l)
      if m:
        print l
        break
    
    from pymongo import MongoClient
    with MongoClient() as client:
      table = client["mongoid"]["User"]
      table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
  • 1
    require 'mongoid'
    
    2
    File.open('mongoid.yml', 'w') do |file|
    
    3
      file.write <<-CONFIG
    
    4
    development:
    
    5
      sessions:
    
    6
        default:
    
    7
          database: mongoid
    
    8
          hosts:
    
    9
            - localhost:27017
    
    10
    CONFIG
    
    11
    end
    
    12
    fork { `mongod` }
    
    13
    sleep 1 # need to find a better way to wait for mongo to startup
    
    1+
    import subprocess
    
    2+
    import re
    
    3+
    mongod = subprocess.Popen("mongod", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    4+
    # @jhoffner: I listen to my child process until it prints "waiting for connections" instead of sleeping...
    
    5+
    while True:
    
    6+
      l = mongod.stdout.readline()
    
    7+
      m = re.match(r".*waiting for connections on port 27017", l)
    
    8+
      if m:
    
    9+
        print l
    
    10+
        break
    
    1414
    15
    ENV['RACK_ENV'] = 'development'
    
    16
    Mongoid.load!('mongoid.yml')
    
    17
    18
    class User
    
    19
      include Mongoid::Document
    
    20
      field :name
    
    21
    end
    
    12+
    from pymongo import MongoClient
    
    13+
    with MongoClient() as client:
    
    14+
      table = client["mongoid"]["User"]
    
    15+
      table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
    

Recent Moves:

Add, remove and display the items in the fridge. There is some room for improvement here!

Code
Diff
  • Proc = fun(Y, Items) ->
    	receive
        {Sender, Item} when is_pid(Sender) -> 
        	case dict:find(Item, Items) of
           {ok, Value} ->
              NewCount = Value - 1,
            	io:format("One ~p coming right up!~n", [Item]),
           		Sender ! Item,
              Y(Y, dict:store(Item, NewCount, Items));
          	_ -> 
            	io:format("We're all out of ~p~n", [Item]),
              Y(Y, Items)
          end;
          
        {count, Item} ->
            case dict:find(Item, Items) of
              {ok, Value} -> io:format("~p: ~p~n", [Item, Value]);
               _ -> io:format("We're all out of ~p~n", [Item])
            end,
            Y(Y, Items);
            
        {Item, Count} when is_atom(Item) andalso is_integer(Count) -> 
        	io:format("~p added.~n", [Item]),
          NewCount = case dict:find(Item, Items) of
            {ok, Value} -> Value + Count;
          	_ -> Count
          end,
          Y(Y, dict:store(Item, NewCount, Items));
          
        count ->
          io:format("~p~n", [dict:to_list(Items)]),
          Y(Y, Items);
          
        Item when is_atom(Item) -> 
        	io:format("~p added.~n", [Item]),
          NewCount = case dict:find(Item, Items) of
            {ok, Value} -> Value + 1;
          	_ -> 0
          end,
          Y(Y, dict:store(Item, NewCount, Items));
          
        _ -> io:format("Killing Fridge...")
      end
    end,
    
    Pid = spawn(fun() -> Proc(Proc, dict:new()) end),
    
    Pid ! {beer, 6},
    Pid ! {pudding, 8},
    Pid ! salsa,
    Pid ! soda,
    Pid ! eggs,
    Pid ! steak,
    
    Pid ! count,
    Pid ! {count, beer},
    Pid ! {self(), beer},
    Pid ! {count, beer},
    
    init:stop().
  • 11
    Proc = fun(Y, Items) ->
    
    22
    	receive
    
    3
      	Item when is_atom(Item) -> 
    
    3+
        {Sender, Item} when is_pid(Sender) -> 
    
    4+
        	case dict:find(Item, Items) of
    
    5+
           {ok, Value} ->
    
    6+
              NewCount = Value - 1,
    
    7+
            	io:format("One ~p coming right up!~n", [Item]),
    
    8+
           		Sender ! Item,
    
    9+
              Y(Y, dict:store(Item, NewCount, Items));
    
    10+
          	_ -> 
    
    11+
            	io:format("We're all out of ~p~n", [Item]),
    
    12+
              Y(Y, Items)
    
    13+
          end;
    
    14+
          
    
    15+
        {count, Item} ->
    
    16+
            case dict:find(Item, Items) of
    
    17+
              {ok, Value} -> io:format("~p: ~p~n", [Item, Value]);
    
    18+
               _ -> io:format("We're all out of ~p~n", [Item])
    
    19+
            end,
    
    20+
            Y(Y, Items);
    
    21+
            
    
    22+
        {Item, Count} when is_atom(Item) andalso is_integer(Count) -> 
    
    44
        	io:format("~p added.~n", [Item]),
    
    5
          Y(Y, [Item | Items]);
    
    24+
          NewCount = case dict:find(Item, Items) of
    
    25+
            {ok, Value} -> Value + Count;
    
    26+
          	_ -> Count
    
    27+
          end,
    
    28+
          Y(Y, dict:store(Item, NewCount, Items));
    
    29+
          
    
    30+
        count ->
    
    31+
          io:format("~p~n", [dict:to_list(Items)]),
    
    32+
          Y(Y, Items);
    
    33+
          
    
    34+
        Item when is_atom(Item) -> 
    
    35+
        	io:format("~p added.~n", [Item]),
    
    36+
          NewCount = case dict:find(Item, Items) of
    
    37+
            {ok, Value} -> Value + 1;
    
    38+
          	_ -> 0
    
    39+
          end,
    
    40+
          Y(Y, dict:store(Item, NewCount, Items));
    
    41+
          
    
    66
        _ -> io:format("Killing Fridge...")
    
    77
      end
    
    88
    end,
    
    99
    10
    Pid = spawn(fun() -> Proc(Proc, []) end),
    
    46+
    Pid = spawn(fun() -> Proc(Proc, dict:new()) end),
    
    1111
    12
    Pid ! beer,
    
    48+
    Pid ! {beer, 6},
    
    49+
    Pid ! {pudding, 8},
    
    1313
    Pid ! salsa,
    
    1414
    Pid ! soda,
    
    1515
    Pid ! eggs,
    
    1616
    Pid ! steak,
    
    17
    Pid ! pudding,
    
    54+
    55+
    Pid ! count,
    
    56+
    Pid ! {count, beer},
    
    57+
    Pid ! {self(), beer},
    
    58+
    Pid ! {count, beer},
    
    1818
    1919
    init:stop().
    

Recent Moves:

Code
Diff
  • Proc = fun (Index) -> 
    	timer:sleep(10),
    	io:format("~p~n", [Index])
    end,
    
    [spawn(fun() -> Proc(X) end) || X <- lists:seq(1, 40)],
    
    init:stop().
  • 1
    Proc = fun () -> 
    
    2
    	io:format("Hello Process.~n")
    
    1+
    Proc = fun (Index) -> 
    
    2+
    	timer:sleep(10),
    
    3+
    	io:format("~p~n", [Index])
    
    33
    end,
    
    44
    5
    Pid = spawn(Proc),
    
    6
    7
    io:format(" Hello's Pid: ~p~n", [Pid]),
    
    6+
    [spawn(fun() -> Proc(X) end) || X <- lists:seq(1, 40)],
    
    88
    99
    init:stop().
    

Recent Moves:

Code
Diff
  • println "hello, world"
  • 1
    (display "Hello, world!")
    
    1+
    println "hello, world"
    
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))