Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad
Code
Diff
  • fn main() {
        println!("All we are in rust in the wind...");
    }
    • println "hello, world"
    • fn main() {
    • println!("All we are in rust in the wind...");
    • }
Code
Diff
  • class Fridge
      def initialize
        @items = {}
      end
      
      def add(item, amount = 1)
        @items[item] ||= 0
        @items[item] += amount
      end
      
      def serve(item, amount = 1)
        _amount = [amount, inventory(item)].min
        add(item, _amount * -1)
        
        _amount.times do
          puts "One #{item} coming right up!"
        end
        
        (amount - _amount).times do
          puts "We're all out of #{item}"
        end
      end
      
      def inventory(item = nil)
         if item
           @items[item]
         else
           @items.values.reduce(0) {|s, v| s + v} 
         end
      end
    end
    
    ## examples:
    
    fridge = Fridge.new
    
    fridge.add :beer, 6
    fridge.add :pudding, 8
    fridge.add :salsa
    fridge.add :soda
    fridge.add :eggs
    fridge.add :steak
    
    puts "There are #{fridge.inventory} items in the fridge"
    puts "#{fridge.inventory(:beer)} of those are beers"
    
    fridge.serve(:beer, 7)
    fridge.serve(:steak)
    
    puts "There are #{fridge.inventory} items in the fridge"
    puts "#{fridge.inventory(:beer)} of those are beers"
    • 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...")
    • class Fridge
    • def initialize
    • @items = {}
    • end
    • end,
    • def add(item, amount = 1)
    • @items[item] ||= 0
    • @items[item] += amount
    • end
    • def serve(item, amount = 1)
    • _amount = [amount, inventory(item)].min
    • add(item, _amount * -1)
    • _amount.times do
    • puts "One #{item} coming right up!"
    • end
    • (amount - _amount).times do
    • puts "We're all out of #{item}"
    • end
    • end
    • def inventory(item = nil)
    • if item
    • @items[item]
    • else
    • @items.values.reduce(0) {|s, v| s + v}
    • end
    • end
    • end
    • ## examples:
    • fridge = Fridge.new
    • fridge.add :beer, 6
    • fridge.add :pudding, 8
    • fridge.add :salsa
    • fridge.add :soda
    • fridge.add :eggs
    • fridge.add :steak
    • Pid = spawn(fun() -> Proc(Proc, dict:new()) end),
    • puts "There are #{fridge.inventory} items in the fridge"
    • puts "#{fridge.inventory(:beer)} of those are beers"
    • 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},
    • fridge.serve(:beer, 7)
    • fridge.serve(:steak)
    • init:stop().
    • puts "There are #{fridge.inventory} items in the fridge"
    • puts "#{fridge.inventory(:beer)} of those are beers"
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'})
    • require 'mongoid'
    • File.open('mongoid.yml', 'w') do |file|
    • file.write <<-CONFIG
    • development:
    • sessions:
    • default:
    • database: mongoid
    • hosts:
    • - localhost:27017
    • CONFIG
    • end
    • fork { `mongod` }
    • sleep 1 # need to find a better way to wait for mongo to startup
    • 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
    • ENV['RACK_ENV'] = 'development'
    • Mongoid.load!('mongoid.yml')
    • class User
    • include Mongoid::Document
    • field :name
    • end
    • from pymongo import MongoClient
    • with MongoClient() as client:
    • table = client["mongoid"]["User"]
    • table.insert({'_id': 9999, 'first name': 'William', 'last name': 'the Conqueror'})
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().
    • Proc = fun () ->
    • io:format("Hello Process.~n")
    • Proc = fun (Index) ->
    • timer:sleep(10),
    • io:format("~p~n", [Index])
    • end,
    • Pid = spawn(Proc),
    • io:format(" Hello's Pid: ~p~n", [Pid]),
    • [spawn(fun() -> Proc(X) end) || X <- lists:seq(1, 40)],
    • init:stop().
Numbers
Data Types
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))
    
    • (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"
    • {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"})
    • 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"
    • {10 "Ten" 11 "Eleven" 12 "Twelve"
    • 13 "Thirteen" 14 "Fourteen" 15 "Fifteen"
    • 16 "Sixteen" 17 "Seventeen" 18 "Eighteen"
    • 19 "Nineteen"})
    • (defn- small-number-to-words
    • [x]
    • (cond
    • (<= x 9) (digit-to-word x)
    • (< x 20) (teen-to-word x)
    • (< x 100)
    • (let [ones-part (mod x 10)
    • tens-part (- x ones-part)]
    • (str (tens-to-word tens-part) "-"
    • (digit-to-word ones-part)))
    • (<= x 999)
    • (let [small-part (mod x 100)
    • hundreds-digit (-> x (- small-part) (/ 100))]
    • (str (digit-to-word hundreds-digit) " Hundred " (small-number-to-words small-part)))))
    • (defn- digit-shift
    • [x shift]
    • (-> x (- (mod x shift)) (/ shift) int))
    • (defn convert
    • [x]
    • (loop [words [] x x]
    • (cond
    • (-> x (>= 1e6))
    • (recur (conj words "One Million")
    • (- x 1e6))
    • (-> x (>= 1e3))
    • (recur (conj words
    • (str
    • (-> x (digit-shift 1000) small-number-to-words)
    • " Thousand"))
    • (mod x 1000))
    • (-> x (> 0))
    • (recur (conj words (small-number-to-words x)) 0)
    • :else (join ", " words))))
    • (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))
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 # Opens up the Array class for method creation
      def flip
        self.reverse.map{|e| e.is_a?(Array) ? e.flip : e}
      end
    end
    • class Array # Opens up the Array class for method creation
    • def flip
    • result = []
    • me = self.reverse # Don't fret, I'll sort the Arrays out!
    • me.each do |x|
    • if x.class == Array then result << x.flip # Here's the recursion.
    • else result << x
    • end
    • end
    • return result
    • self.reverse.map{|e| e.is_a?(Array) ? e.flip : e}
    • end
    • end
Recursion
Algorithms
Computability Theory
Logic
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)]),
    
    • Factorial = fun (Y, 0) -> 1;
    • (Y, N) -> N * Y(Y, N - 1)
    • end,
    • Factorial = fun(N) when N > 0 ->
    • io:format("~p~n", [Factorial(Factorial, 5)]),
    • Tail = fun (Y, Acc, 0) -> Acc;
    • (Y, Acc, N) -> Y(Y, Acc * N, N - 1)
    • end,
    • Tail(Tail, 1, N)
    • end,
    • init:stop().
    • io:format("~s~n", [Factorial(5)]),