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

String.split Demo:

'123456'.split('');

// [ '1', '2', '3', '4', '5', '6' ]

How about:

'123456'.spliter(2);
// [ '12', '34', '56' ]

?

console.log(
  '123456'.split('')
);
Strings
Data Types

Let's try to grab the last character from a string.

def last_char(str):
  return str[-1]

This is my Pretty Print code for the Airport Arrivals/Departures flap display

Ref: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java

public class Dinglemouse {

  // Use CSS to display a pretty version of the flap display
  // From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
  public static String[] prettyPrint(final String[] lines) {
    String s = "<pre>";
    for (int y = 0; y < lines.length; y++) {
      s += "<div style=\"height:23px\">";
      for (int x = 0; x < lines[y].length(); x++) {
        s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
      }
      s += "</div>";
    }
    s+= "</pre>";
    System.out.println(s);
    return lines;
  }

}

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 3000 ms

from math import sqrt
def is_prime(n):
    if n < 2: return False
    for x in range(2, int(sqrt(n)) + 1):
        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

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