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.

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

This implementation of fibonacci follows https://www.nayuki.io/page/fast-fibonacci-algorithms, with a base case that uses math.pow.

Code
Diff
  • from math import sqrt
    
    def fib(x):
        if x < 70:
            a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5))
            b = 1.618033988749895 # (1 + sqrt(5)) / 2
            return round(a * b**x)
        elif x % 2 == 0:
            a = fib(x / 2)
            b = fib(x / 2 + 1)
            return a * (2 * fib(b) - a)
        else:
            # x % 2 == 1, by elimination
            a = fib((x - 1) / 2)
            b = fib((x + 1) / 2)
            return a * a + b * b
    
  • 1
    # Fibonacci #
    
    2
    33
    from math import sqrt
    
    44
    5
    c1 = (sqrt(5) - 1) / (2 * sqrt(5))
    
    6
    c2 = (sqrt(5) + 1) / (2 * sqrt(5))
    
    7
    8
    def f(x):
    
    9
        fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**x
    
    10
        return int(round(fibo))
    
    3+
    def fib(x):
    
    4+
        if x < 70:
    
    5+
            a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5))
    
    6+
            b = 1.618033988749895 # (1 + sqrt(5)) / 2
    
    7+
            return round(a * b**x)
    
    8+
        elif x % 2 == 0:
    
    9+
            a = fib(x / 2)
    
    10+
            b = fib(x / 2 + 1)
    
    11+
            return a * (2 * fib(b) - a)
    
    12+
        else:
    
    13+
            # x % 2 == 1, by elimination
    
    14+
            a = fib((x - 1) / 2)
    
    15+
            b = fib((x + 1) / 2)
    
    16+
            return a * a + b * b
    

Recent Moves:

Updated to test that it is working with new rust changes

Code
Diff
  • 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
    }
  • 88
    	if p & 1 == 1 {
    
    99
    		r = r * n % m;
    
    1010
    	}
    
    1111
    	
    
    1212
    	r
    
    1313
    }
    
    14
    15
    #[test]
    
    16
    fn test_powermod() {
    
    17
    	assert_eq!(powermod(2, 999999, 147), 50);
    
    18
    }
    
Code
Diff
  • defmodule Piapprox do
    
      def iter_pi(epsilon) do
        pi = :math.pi
        leibniz_stream
          |> Stream.with_index
          |> Enum.reduce_while(0, fn
            ({i, _}, acc) when abs(pi - acc) >= epsilon ->
              {:cont, acc + i}
            ({_, n}, acc) ->
              {:halt, [n, Float.round(acc, 10)]}
          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)
      end
    end
    
  • 11
    defmodule Piapprox do
    
    22
    33
      def iter_pi(epsilon) do
    
    4
        leibniz_stream |>
    
    5
        Enum.reduce_while(0, fn {i, n}, acc ->
    
    6
          if abs(:math.pi - acc) >= epsilon do
    
    7
            { :cont, acc + i }
    
    8
          else
    
    9
            { :halt, [n, Float.round(acc, 10)] }
    
    10
          end
    
    11
        end)
    
    4+
        pi = :math.pi
    
    5+
        leibniz_stream
    
    6+
          |> Stream.with_index
    
    7+
          |> Enum.reduce_while(0, fn
    
    8+
            ({i, _}, acc) when abs(pi - acc) >= epsilon ->
    
    9+
              {:cont, acc + i}
    
    10+
            ({_, n}, acc) ->
    
    11+
              {:halt, [n, Float.round(acc, 10)]}
    
    12+
          end)
    
    1212
      end
    
    1313
    1414
      defp leibniz_stream do
    
    1515
        Stream.unfold(1, fn
    
    1616
          n when rem(n,4) == 1 -> { 4/n, n+2 }
    
    1717
          n                    -> {-4/n, n+2 }
    
    18
        end) |> Stream.with_index
    
    19+
        end)
    
    1919
      end
    
    2020
    end
    

Recent Moves:

Bugs
// !!!
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>"

Passed and failing tests with imports etc

Code
Diff
  • class Person {
      String firstName;
      String lastName;
      
      Person(this.firstName,this.lastName);
      
      String get fullName => '$firstName $lastName';
    }
  • 1
    doubler(n) => n*2;
    
    1+
    class Person {
    
    2+
      String firstName;
    
    3+
      String lastName;
    
    4+
      
    
    5+
      Person(this.firstName,this.lastName);
    
    6+
      
    
    7+
      String get fullName => '$firstName $lastName';
    
    8+
    }
    

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

fn doubler(n:i32) -> i32 {
  n * 2
}