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

This is a Python version of the Javascript equivialant

Create a function, getVillainName, that returns a villain name based on the user's birthday. (The birthday will be passed to the function as a valid Date object, so for simplicity, there's no need to worry about converting strings to dates.)

The first name will come from the month, and the last name will come from the last digit of the date.

Month -> first name

January -> "The Evil"
February -> "The Vile"
March -> "The Cruel"
April -> "The Trashy"
May -> "The Despicable"
June -> "The Embarrassing"
July -> "The Disreputable"
August -> "The Atrocious"
September -> "The Twirling"
October -> "The Orange"
November -> "The Terrifying"
December -> "The Awkward"
Last digit of date -> last name

0 -> "Mustache"
1 -> "Pickle"
2 -> "Hood Ornament"
3 -> "Raisin"
4 -> "Recycling Bin"
5 -> "Potato"
6 -> "Tomato"
7 -> "House Cat"
8 -> "Teaspoon"
9 -> "Laundry Basket"
The returned value should be a string in the form of "First Name Last Name".

For example, a birthday of November 18 would return "The Terrifying Teaspoon"

def  getVillianName(birthday):
    result = ""

    result = FIRSTNAME[birthday.strftime("%B")] + " "
    result += LASTNAME[birthday.strftime("%d")[1]]
    return result
Games
Fundamentals
Basic Language Features

This kata is about PING PONG game.
By receiveing a current score of the game and amount of serves per one player method should return a person ("first" or "second") who should do a next serve.
For example:

  • Score 0:0 and 2 serves per player - then whoShouldServe(0, 0, 2) === "first"
  • Score 4:5 and 2 serves per player - then whoShouldServe(4, 5, 2) === "second"
function whoShouldServe($scoreLeft, $scoreRight, $servesCount) {
  return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
};
echo 'ss';

lalala

function isNumEqual(n){
  console.log(n);
  return n;
}

As a tool for many mathy katas, there is a need to have a fast code to find the divisors of a number. Here I present one, that of course, may be improved. I will translate this code into python and ruby.

function sortNumber(a, b){return a - b;}

function divisors(n) {
    var fact = [];
    for (var i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
        if (n % i === 0) {
            fact.push(i);
            if (n / i !== i) fact.push(n/ i);
        }
    }
    fact.sort(sortNumber);
    return fact;
}

The same code translated into ruby from Javascript. The Javascript version is twice faster!!

def divisors(n)
    fact = [];
    for i in 1.. (Math.sqrt(n)).floor
        if n % i == 0 
            fact << i
            if n / i != i
                fact << n / i
            end
        end
    end
    fact.sort!
    return fact
end

The Python Version is also very fast but the Javascript version is the fastest of all.

  1. Javascript
    2 Python
  2. Ruby
from math import sqrt, floor

def divisors(n):
    fact = [];
    for i in range(1, int(floor(sqrt(n))) + 1):
        if n % i == 0:
            fact.append(i)
            if n / i != i:
                fact.append(n / i)
    fact.sort()
    return fact

Another way to find the divisors of a number is to make the prime factorization and then to combine them to get all the divisors. This code is three times faster than the one posted: https://www.codewars.com/kumite/580718d1e1136e46be0000b3?sel=580718d1e1136e46be0000b3

from math import floor, sqrt
from collections import Counter
def fac(n):
    step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
    maxq = long(floor(sqrt(n)))
    d = 1
    q = n % 2 == 0 and 2 or 3 
    while q <= maxq and n % q != 0:
        q = step(d)
        d += 1
    return q <= maxq and [q] + fac(n//q) or [n]
   
def divisors_(factors):
    div = [1]
    for p, r in factors.items():
        div = [d * p**e for d in div for e in range(r + 1)]
    div.sort()
    return div

def divisors(n):
    primefac = fac(n)
    primefacdict = Counter(primefac)
    return divisors_(primefacdict)

Efficient but it's not the faster.

def divisors(n):    
    return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))

Return the middle character of the string. If the string's length is odd, return the middle character. If the string's length is even, return the middle 2 characters.

function middleCharacter(str) {
 if (str.length % 2 !== 0) {
  return str.slice(str.length/2, str.length/2+1);
};
  return str.slice(str.length/2-1, str.length/2+1);
};