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

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

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();
Sequences
Arrays
Data Types

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

Compute n ^ p % m

pub fn powermod(n: u64, p: u64, m: u64) -> u64 {
	if p == 0 { return 1 % m }
	if p == 1 { return n % m }

	let mut r = powermod(n, p / 2, m);

	r = r * r % m;
	if p & 1 == 1 {
		r = r * n % m;
	}
	
	r
}

#[test]
fn test_powermod() {
	assert_eq!(powermod(2, 999999, 147), 50);
}
def get_prime_factorization(number):
    """Gets the prime factorization of a number, and returns it.

    @type number: float
    @param number: A float representing the number to be factored.
    """
    current_dividing_factor = float(number)
    if current_dividing_factor == 1.0 or current_dividing_factor == 0.0 or current_dividing_factor == -1.0:
        return [current_dividing_factor]
    
    factors = []
    # Catches all negative numbers and makes them positive.
    if current_dividing_factor < 0.0:
        factors.append(-1.0)
        current_dividing_factor *= -1.0
        
    # Catches all numbers not whole numbers and makes them whole numbers.
    if current_dividing_factor % 1.0 != 0.0:
        decimal_places = len(str(current_dividing_factor % 1.0)) - 2.0
        factors.append(10.0 ** (-decimal_places))
        current_dividing_factor *= 10.0 ** decimal_places
        
    # Gets all factors greater than 2.0 and adds them to the factor list.
    test_factor = 2.0
    while test_factor <= current_dividing_factor:
        if current_dividing_factor % test_factor == 0.0:
            factors.append(test_factor)
            current_dividing_factor /= test_factor
            test_factor = 2.0
        else:
            test_factor += 1.0
    return factors

This small piece of code calculates the prime factorisation of any given number.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Factorisation {

	public static List<Integer> primeFactorsOf(int input) {

		List<Integer> pFactors = new ArrayList<>();
		long number = input;

		for (int i = 2; i <= number; i++) {
			if (number % i == 0) {
				pFactors.add(i);
				number /= i;
				i--;
			}
		}
		return pFactors;
	}

  public static void main(String[] args) {
    
  }
}

It always starts with "hello world" ^^

c("Hello World!")