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
  • println "hello, world"
    • (display "Hello, world!")
    • println "hello, world"
MongoDB
NoSQL
Databases

Using backticks instead of exec prevents the mongodb output from being displayed.

Code
Diff
  • 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
    
    ENV['RACK_ENV'] = 'development'
    Mongoid.load!('mongoid.yml')
    
    class User
      include Mongoid::Document
      field :name
    end
    • 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
    • fork { `mongod` }
    • 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
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)]),

Oho, you forgot to write a newline!

Code
Diff
  • import sys
    sys.stdout.write("Hello Python!\n")
    • import sys
    • sys.stdout.write("Hello Python!")
    • sys.stdout.write("Hello Python!\n")
Code
Diff
  • #include "iostream"
    
    int main(){
      namespace std {
        cout << "Hello C++" << endl;
      }
    }
    • #include "iostream"
    • using namespace std;
    • int main(){
    • cout << "Hello C++" << endl;
    • namespace std {
    • cout << "Hello C++" << endl;
    • }
    • }