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.

Sequences
Arrays

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
Code
Diff
  • #include <iostream>
    
    int main()
    {
      std::cout << "Hello C++" << std::endl;
      return 0;
    }
  • 11
    #include <iostream>
    
    22
    3
    int main(){
    
    4
      
    
    3+
    int main()
    
    4+
    {
    
    55
      std::cout << "Hello C++" << std::endl;
    
    6
      
    
    6+
      return 0;
    
    77
    }
    

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();

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))

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);
    });
}

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";
Code
Diff
  • #!/usr/bin/env python
    
    import optparse
    
    def log(msg):
    	print(msg)
    
    def info(msg):
        log("[*]: %s " % msg)
    
    def debug(msg):
        log("[D]: %s" % msg)
    
    def main():
        parser = optparse.OptionParser(description="doit")
        parser.add_option("-f", dest="flags", action="store", default=None, help="flags")
        (opts,args) = parser.parse_args()
        debug(opts)
        return 0
    
    if __name__ == "__main__":
        main()
  • 11
    #!/usr/bin/env python
    
    22
    3+
    import optparse
    
    4+
    5+
    def log(msg):
    
    6+
    	print(msg)
    
    7+
    8+
    def info(msg):
    
    9+
        log("[*]: %s " % msg)
    
    10+
    11+
    def debug(msg):
    
    12+
        log("[D]: %s" % msg)
    
    33
    44
    def main():
    
    5
        print "hello world"
    
    15+
        parser = optparse.OptionParser(description="doit")
    
    16+
        parser.add_option("-f", dest="flags", action="store", default=None, help="flags")
    
    17+
        (opts,args) = parser.parse_args()
    
    18+
        debug(opts)
    
    19+
        return 0
    
    66
    77
    if __name__ == "__main__":
    
    88
        main()
    

Recent Moves:

import pprint;pprint.pprint(zip(('Byte', 'KByte', 'MByte', 'GByte', 'TByte'), (1 << 10*i for i in xrange(5))))