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

Looks like C++ and C# return their tests in alphabetical order.

int a = 1;

For some reason I keep using this function as a test in all the runner changes

fn doubler(n:i32) -> i32 {
  n * 2
}
let stringToHTML = (el = 'div', attr = [], template = '') => 
  `<${el} ${attr.join(' ')}>${template}</${el}>`,
  
  element = 'a',
  content = "https://www.codewars.com"
  attributes = [`href='${content}'`, "class='link'"];
  
stringToHTML(element, attributes, content);

// => "<a href='https://www.codewars.com' class='link'>https://www.codewars.com</a>"
Bugs
// !!!

Can this code be refactored into something more elegant?

defmodule Piapprox do

  def iter_pi(epsilon) do
    leibniz_stream |>
    Enum.reduce_while(0, fn {i, n}, acc ->
      if abs(:math.pi - acc) >= epsilon do
        { :cont, acc + i }
      else
        { :halt, [n, Float.round(acc, 10)] }
      end
    end)
  end

  defp leibniz_stream do
    Stream.unfold(1, fn
      n when rem(n,4) == 1 -> { 4/n, n+2 }
      n                    -> {-4/n, n+2 }
    end) |> Stream.with_index
  end
end

I used mathematical analysis to find the two constants, from the iterative form: f(n) = f(n-1) + f(n-2) and initial conditions.

# Fibonacci #

from math import sqrt

c1 = (sqrt(5) - 1) / (2 * sqrt(5))
c2 = (sqrt(5) + 1) / (2 * sqrt(5))

def f(x):
    fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**x
    return int(round(fibo))

Construct a singleton in js.

Tests:
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;

obj1 === obj2 // true
obj2.test // 1

let Singleton = (function () {
	let instance;

	return function Construct_singletone () {
		if (instance) {
			return instance;
		}
		if (this && this.constructor === Construct_singletone) {
			instance = this;
		} else {
			return new Construct_singletone();
		}
	}
}());
Mathematics
Algorithms
Logic
Numbers
Fundamentals

You must create a Dice generator.

Dice have 6 sides. Player throws up a cube and look at the result.

Code must have output like this:
function(random){...}

Cube(); // 1
Cube(); // 5
Cube(); // 3
...

You should use Math.floor to get a integer numbers.

function Cube(){
  let random = Math.floor(Math.random()*6+1);
  if(random===1)return 1
  else if(random===2)return 2
  else if(random===3)return 3
  else if(random===4)return 4
  else if(random===5)return 5
  else if(random===6)return 6
}

Need to do 2d affine transformations, or rotate stuff in 3d for your toy canvas library? look no further, the solution is here! Or at least it will be if you help improve this thing by forking it.

var mat3 = (function(){

    function Mat3(a){
      if(!a) return mat3.identity();
      this.a = [];
      for(var i = 0; i < 9; i++) this.a[i] = a[i] || 0;
    };

    Mat3.prototype.add = function(mat){
      return mat3([
        this.a[0] + mat.a[0], this.a[1] + mat.a[1], this.arr[2] + mat.a[2],
        this.a[3] + mat.a[3], this.a[4] + mat.a[4], this.arr[5] + mat.a[5],
        this.a[6] + mat.a[6], this.a[7] + mat.a[7], this.arr[8] + mat.a[8]
      ]); 
    };
    
    Mat3.prototype.applyToCanvasContext = function(ctx){
      ctx.transform(this.a[0], this.a[3], this.a[1], this.a[4], this.a[2], this.a[5]);
    };
    
    Mat3.prototype.equals = function(mat){
      return this.a[0] === mat.a[0] && this.a[1] === mat.a[1] && this.a[2] === mat.a[2]
          && this.a[3] === mat.a[3] && this.a[4] === mat.a[4] && this.a[5] === mat.a[5]
          && this.a[6] === mat.a[6] && this.a[7] === mat.a[7] && this.a[8] === mat.a[8];
    };
    
    Mat3.prototype.toString = function(){
      return '[' + this.a.join(',') + ']';
    };
    
    // Matrix-Vector multiplication
    Mat3.prototype.transform3d = function(v){
      return [
        v[0] * this.a[0] + v[1] * this.a[1] + v[2] * this.a[2],
        v[0] * this.a[3] + v[1] * this.a[4] + v[2] * this.a[5],
        v[0] * this.a[6] + v[1] * this.a[7] + v[2] * this.a[8]
      ];
    };
    
    // 2d affine transformation
    Mat3.prototype.transform2d = function(v){
      var v2 = this.transform3d([v[0],v[1],1]);
      return [v2[0],v2[1]];
    };
    
    Mat3.prototype.inverse = function(){
      var f = this.a[0] * (this.a[4]*this.a[8] - this.a[5]*this.a[7])
            + this.a[1] * (this.a[5]*this.a[6] - this.a[3]*this.a[8])
            + this.a[2] * (this.a[3]*this.a[7] - this.a[4]*this.a[6]);
    
      return mat3([
        this.a[4]*this.a[8] - this.a[5]*this.a[7],
        this.a[2]*this.a[7] - this.a[1]*this.a[8],
        this.a[1]*this.a[5] - this.a[2]*this.a[4],
        
        this.a[5]*this.a[6] - this.a[3]*this.a[8],
        this.a[0]*this.a[8] - this.a[2]*this.a[6],
        this.a[2]*this.a[3] - this.a[0]*this.a[5],
        
        this.a[3]*this.a[7] - this.a[4]*this.a[6],
        this.a[1]*this.a[6] - this.a[0]*this.a[7],
        this.a[0]*this.a[4] - this.a[1]*this.a[3]
      ]).multiplyScalar(1/f);
    };
    
    Mat3.prototype.multiply = function(mat){
      return mat3([
        this.a[0] * mat.a[0] + this.a[1] * mat.a[3] + this.a[2] * mat.a[6],
        this.a[0] * mat.a[1] + this.a[1] * mat.a[4] + this.a[2] * mat.a[7],
        this.a[0] * mat.a[2] + this.a[1] * mat.a[5] + this.a[2] * mat.a[8],
    
        this.a[3] * mat.a[0] + this.a[4] * mat.a[3] + this.a[5] * mat.a[6],
        this.a[3] * mat.a[1] + this.a[4] * mat.a[4] + this.a[5] * mat.a[7],
        this.a[3] * mat.a[2] + this.a[4] * mat.a[5] + this.a[5] * mat.a[8],
    
        this.a[6] * mat.a[0] + this.a[7] * mat.a[3] + this.a[8] * mat.a[6],
        this.a[6] * mat.a[1] + this.a[7] * mat.a[4] + this.a[8] * mat.a[7],
        this.a[6] * mat.a[2] + this.a[7] * mat.a[5] + this.a[8] * mat.a[8]
      ]);
    };
    
    Mat3.prototype.multiplyScalar = function(s){
      return mat3([
        this.a[0] * s, this.a[1] * s, this.arr[2] * s,
        this.a[3] * s, this.a[4] * s, this.arr[5] * s,
        this.a[6] * s, this.a[7] * s, this.arr[8] * s
      ]);
    };
    
    Mat3.prototype.transpose = function(){
      return mat3([
        this.a[0], this.a[3], this.a[6],
        this.a[1], this.a[4], this.a[7],
        this.a[2], this.a[5], this.a[8]
      ]);
    };

    function mat3(a){
      return new Mat3(a);
    };
    
    mat3.identity = function(){
      return mat3([1,0,0,0,1,0,0,0,1]);
    };
    
    // 2d rotation
    mat3.rotate = function(phi){
      var s = Math.sin(phi), c = Math.cos(phi);
      return mat3([c,s,0,-s,c,0,0,0,1]);
    };
    
    // 3d rotations
    mat3.rotate.z = mat3.rotate;
    
    mat3.rotate.y = function(phi){
      var s = Math.sin(phi), c = Math.cos(phi);
      return mat3([c,0,s,0,1,0,-s,0,c]);
    };
    
    mat3.rotate.x = function(phi){
      var s = Math.sin(phi), c = Math.cos(phi);
      return mat3([1,0,0,0,c,-s,0,s,c]);
    };
    
    mat3.scale = function(x, y, z){
      return mat3([x,0,0,0,y,0,0,0,z||1]);
    };
    
    mat3.translate = function(x, y){
      return mat3([1,0,x,0,1,y,0,0,1]);
    };
    
    mat3.Mat3 = Mat3;

    return mat3;

})();
require "sqlite3"
db = SQLite3::Database.new ":memory:"

rows = db.execute <<-SQL
  create table numbers (
    name varchar(30),
    val int
  );
SQL

{
  "one" => 1,
  "two" => 2,
}.each do |pair|
  db.execute "insert into numbers values ( ?, ? )", pair
end

p db.execute( "select * from numbers" ).to_a