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

A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms

function isPrime(n) {
    if (n < 2) return false;
    for (var x = 2; x <= Math.floor(Math.sqrt(n)); x++) {if (n % x == 0) return false;}
    return true;
}

A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms

def is_prime(n)
    return false if n < 2
    for x in 2.. Math.sqrt(n).round
        return false if n % x == 0
    end
    return true
end

I received a fork to my version in python by the user Mc Code. The average runtime for different tries is under 500 ms. The half runtime that the previous version in Javascript, too (1000 ms)

function isPrime(n) {
    if (n < 2) return false;
    else if (n == 2) return true;
    else if (n % 2 === 0) return false;
    for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2) {if (n % x === 0) return false;}
    return true;
}

Again we can see that the runtime is less (more less than a half) than the previous version

def is_prime(n)
    return false if n < 2
    return true if n == 2
    return false if n % 2 == 0
    (3..Math.sqrt(n).round).step(2) do |x|
        return false if n % x == 0
    end
    return true
end

A very easy method importting the library prime in Ruby. It's a bit slower than the last code we've seen in Ruby

require 'prime'

def is_prime(n)
    Prime.prime?(n)
end

A probabilistic code for primality tests. There are some numbers that may give an incorrect result

import random
 
def decompose(n):
    exponentOfTwo = 0
    while n % 2 == 0:
        n = n/2
        exponentOfTwo += 1
    return exponentOfTwo, n
    
def isWitness(possibleWitness, p, exponent, remainder):
    possibleWitness = pow(possibleWitness, remainder, p)
    if possibleWitness == 1 or possibleWitness == p - 1:
        return False
    for _ in range(exponent):
        possibleWitness = pow(possibleWitness, 2, p)
        if possibleWitness == p - 1:
            return False
    return True
    
def is_prime(p, accuracy=100):
    if p == 2 or p == 3: return True
    if p < 2: return False
    exponent, remainder = decompose(p - 1)
    for _ in range(accuracy):
        possibleWitness = random.randint(2, p - 2)
        if isWitness(possibleWitness, p, exponent, remainder):
            return False
    return True

The checkWorkHours(dateTime time.Time) function accepts a time.Time object and returns true or false if the time given is within work hours.

Includes a function to test the outputs of your function.

package main

import (
	"fmt"
	"time"
)

func main() {
	testCheckWorkHours()
}

func checkWorkHours(dateTime time.Time) bool {
	beginWorkHour := 8
	endWorkHour := 18

	currentHour := dateTime.Hour()
	currentDay := dateTime.Weekday()

	if currentDay > 0 && currentDay < 6 {
		if currentHour < endWorkHour && currentHour >= beginWorkHour {
			return true
		}
	}

	return false
}

func testCheckWorkHours() {
	// Tests the function
	timeFormat := "2006-01-02 15:04:05 -0700 MST"

	// True Cases
	dateTime, _ := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Monday @ 3pm should be True: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")
	fmt.Println(fmt.Sprintf("Friday @ 5:59pm should be True: %t", checkWorkHours(dateTime)))

	// False Cases
	dateTime, _ = time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Sunday @ 3pm should be False: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Friday @ 6pm should be False: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Tuesday @ 7am should be False: %t", checkWorkHours(dateTime)))
}

Prints "Hello, Obj-c"

#import <Foundation/Foundation.h>

NSString *helloObjC(NSString *language) {
  NSLog(@"Hello, %@!\n", language);
  return "Hello Obj-C!";
}

We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.

from itertools import *
def product_(arr2D):
    return product(*arr2D)

def groups_combinations(arr2D):
    res = []
    for comb in product_(arr2D):
        res.append(comb)
    return [len(res), res]

We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.

def groups_combination_(a)
	  a[1..-1].inject(a[0]){ |m,v| m = m.product(v).map(&:flatten) }
end

def groups_combinations(arr2D)
    res = groups_combination_(arr2D)
    return [res.length, res]
end