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.
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]
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
test.assert_equals(get_prime_factorization(-25.2), [-1.0, 0.1, 2.0, 2.0, 3.0, 3.0, 7.0])
test.assert_equals(get_prime_factorization(-10), [-1.0, 2.0, 5.0])
test.assert_equals(get_prime_factorization(-1.0), [-1.0])
test.assert_equals(get_prime_factorization(-.2), [-1.0, 0.1, 2])
test.assert_equals(get_prime_factorization(-.05), [-1.0, 0.01, 5])
test.assert_equals(get_prime_factorization(0.0), [0.0])
test.assert_equals(get_prime_factorization(0.36), [0.01, 2, 2, 3, 3])
test.assert_equals(get_prime_factorization(0.7), [0.1, 7])
test.assert_equals(get_prime_factorization(1.0), [1.0])
test.assert_equals(get_prime_factorization(1.3), [0.1, 13])
test.assert_equals(get_prime_factorization(1.95), [0.01, 3, 5, 13])
test.assert_equals(get_prime_factorization(3.0), [3.0])
test.assert_equals(get_prime_factorization(30.0), [2.0, 3.0, 5.0])
test.assert_equals(get_prime_factorization(683.0), [683.0])
test.assert_equals(get_prime_factorization(7130.0), [2.0, 5.0, 23.0, 31.0])
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) {
}
}