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.
#!/usr/bin/env python
def main():
print "hello world"
if __name__ == "__main__":
main()
Hello, world! style application, come on folks let's make PHP a cool subject
echo "Hello, PHP folks!";
import pprint;pprint.pprint(zip(('Byte', 'KByte', 'MByte', 'GByte', 'TByte'), (1 << 10*i for i in xrange(5))))
Create a function that returns a working relative path from the base path to the given target path.
The base path and the target path are absolute.
Ex:
Target: /httpdocs/admin/img/icons/
Base: /httpdocs/admin/
Result: img/icons
Ex 2:
Target: /httpdocs/admin/
Base: /httpdocs/admin/
Result: .
Ex 3:
Target: /httpdocs/
Base: /httpdocs/admin/
Result: ..
Ex 4:
Target: /httpdocs/img/
Base: /httpdocs/admin/
Result: ../img
function getRelativePath($base = '/', $target = '/')
{
$out = array();
$base_ex = explode('/', $base);
$target_ex = explode('/', $target);
//Find common ancestor
$max = min(count($base_ex), count($target_ex));
for($i = 0; $i < $max; $i++)
{
$b = $base_ex[$i];
$t = $target_ex[$i];
if($b != $t) break;
}
$common = $i;
$diff = (count($base_ex) - 1) - $common;
/*
$diff = 0:
Target: /httpdocs/admin/
Base: /httpdocs/admin/
$diff > 0:
Target: /httpdocs/
Base: /httpdocs/admin/
*/
for($i = 0; $i < $diff; $i++)
{
$out[] = '..';
}
$rest = array_slice($target_ex, $common);
$out = array_merge($out, $rest);
if(count($out) == 0) $out[] = '.';
return join('/', $out);
}
echo getRelativePath('/httpdocs/admin/', '/httpdocs/admin/img/icons/');
echo "\n";
echo getRelativePath('/httpdocs/admin/', '/httpdocs/admin/');
echo "\n";
echo getRelativePath('/httpdocs/admin/', '/httpdocs/img/');
echo "\n";
Quick example of a basic resource pool. The test cases are pretty bad and could use some work, but they demonstrate the basic idea.
// sample Resource
function Resource(name){
this.run = function(cb){
console.log("resource ran: " + name)
if (cb) cb()
}
var events = {}
this.on = function(event, cb){
events[event] = events[event] || []
events[event].push(cb);
}
this.emit = function(event){
var self = this;
events[event].forEach(function(cb){
cb(self);
})
}
}
function Pool(){
this.ready = [];
this.waiting = [];
this.resources = [];
}
Pool.prototype.checkout = function(cb)
{
if (this.ready.length > 0)
{
this.ready.shift().run(cb);
}
else
{
this.waiting.push(cb);
}
}
Pool.prototype.checkin = function(resource)
{
if (this.ready.indexOf(resource) == -1){
this.ready.push(resource);
}
if (this.waiting.length > 0)
{
this.checkout(this.waiting.shift());
}
}
Pool.prototype.register = function(resource)
{
this.resources.push(resource);
var self = this;
resource.on('ready', function()
{
self.checkin(resource);
});
}
var pool = new Pool();
pool.register(new Resource('a'))
pool.register(new Resource('b'))
pool.register(new Resource('c'))
// we can checkout even if nothing is ready
pool.checkout(function(){console.log(1)})
// simulate some ready events, some of these will fire even before there are any handlers to play with
pool.resources[0].emit('ready')
pool.resources[1].emit('ready')
pool.resources[2].emit('ready')
pool.checkout(function(){
console.log(2)
Test.pass()
})
pool.checkout()
pool.checkout()
pool.checkout()
pool.resources[0].emit('ready')
Given a starting seed (which must be odd), produces a lazy sequence of "random" numbers generated by the infamous RANDU generator, starting with the seed itself.
(ns randu)
(defn randu-seq [x] (iterate #(mod (* 65539 %) 0x80000000) x))
(ns randu-test
(:require [clojure.test :refer :all]
[randu :refer [randu-seq]]))
(deftest test-oeis-a096555
(is
(=
(take 21 (randu-seq 1))
[1 65539 393225 1769499 7077969 26542323
95552217 334432395 1146624417 1722371299
14608041 1766175739 1875647473 1800754131
366148473 1022489195 692115265 1392739779
2127401289 229749723 1559239569]
)
"(randu-seq 1) doesn't match OEIS A096555!"
)
)
I've written a Curry class Add
that can be used as follows.
var c = new Curry();
c.Add(1)(1); //should make c.Number == 2.
c.Add(3)(4)(5); //should make c.Number == 12.
Are there other ways of doing this same kind of thing in c#? I would love to see what you can come up with. My version of the Curry
class has been added in the Preloaded section.
I've also included the TestCurrying
class for us to test that the behavior is as expected. I am not sure how to setup proper tests with c#, so hopefully this gives us something we can run with.
var test = new Currying.TestCurrying();
test.testSequential();
test.testRandom();
Returns the Collatz sequence starting with the given number.
module Collatz where
collatz :: Int -> [Int]
collatz n = n : collatz next
where next | even n = n `div` 2
| odd n = n * 3 + 1
import Test.Hspec
import Collatz
main = hspec $ do
describe "Collatz sequence" $ do
it "One" $ do
(take 5 $ collatz 1) `shouldBe` [1,4,2,1,4]
it "Zero" $ do
(take 3 $ collatz 0) `shouldBe` [0,0,0]
it "Even" $ do
(take 3 $ collatz 68) `shouldBe` [68,34,17]
it "Odd" $ do
(take 3 $ collatz 23) `shouldBe` [23,70,35]
it "Cycle" $ do
(take 6 $ collatz (-5)) `shouldBe` [-5,-14,-7,-20,-10,-5]
it "Long" $ do
(take 19 $ collatz 28) `shouldBe` [28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]