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.

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))))

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)

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

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

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

lalala

function isNumEqual(n){
  console.log(n);
  return n;
}
Mathematics
Algorithms
Numbers
echo 'ss';
Strings
Code
Diff
  • function swap(arr, a, b) {
      let tmp = arr[a];
      arr[a] = arr[b];
      arr[b] = tmp;
      return arr;
    }
    
    function getNextShuffle(arr) {
      let n = arr.length;
      let j = n - 2;
      let l = n - 1;
      
      while (arr[j] >= arr[j + 1] && j > -1) {
        j--;
      }
      
      if (j === -1) {
        return false
      }
      
      while (arr[j] >= arr[l] && j < l) {
        l--
      }
    
      arr = swap(arr, j, l);
      arr = arr.slice(0, j + 1).concat(arr.slice(j + 1).reverse());
    
      return arr;
    }
    
    function allAnagrams(word, noRepeats) {
      let shuffle = word.split('').map((item, i) => i);
      let result = [];
      
      do {
        let anagram = shuffle.map((index) => word[index]).join('');
        if (!noRepeats || result.indexOf(anagram) < 0) {
          result.push(anagram);
        }
        shuffle = getNextShuffle(shuffle);
      } while (!!shuffle);
    
      return result
    }
  • 1
    function allAnagrams(word) {
    
    1+
    function swap(arr, a, b) {
    
    2+
      let tmp = arr[a];
    
    3+
      arr[a] = arr[b];
    
    4+
      arr[b] = tmp;
    
    5+
      return arr;
    
    6+
    }
    
    22
    3
    	if (word.length < 2) {
    
    4
    		return [word];
    
    5
    	} else {
    
    6
    		var allAnswers = [];
    
    7
    		for(var i = 0; i < word.length; i++) {
    
    8
    			var letter = word[i];
    
    9
    		  var shorterWord = word.substr(0, i) + word.substr(i+1, word.length - 1);
    
    10
    		  var shortWordArray = allAnagrams(shorterWord);	
    
    11
    		  for (var j = 0; j < shortWordArray.length; j++) {
    
    12
    			  allAnswers.push(letter + shortWordArray[j]); 
    
    13
    		  }
    
    14
    		
    
    15
       	}
    
    16
    	  return allAnswers;
    
    17
    	}
    
    8+
    function getNextShuffle(arr) {
    
    9+
      let n = arr.length;
    
    10+
      let j = n - 2;
    
    11+
      let l = n - 1;
    
    12+
      
    
    13+
      while (arr[j] >= arr[j + 1] && j > -1) {
    
    14+
        j--;
    
    15+
      }
    
    16+
      
    
    17+
      if (j === -1) {
    
    18+
        return false
    
    19+
      }
    
    20+
      
    
    21+
      while (arr[j] >= arr[l] && j < l) {
    
    22+
        l--
    
    23+
      }
    
    24+
    25+
      arr = swap(arr, j, l);
    
    26+
      arr = arr.slice(0, j + 1).concat(arr.slice(j + 1).reverse());
    
    27+
    28+
      return arr;
    
    29+
    }
    
    30+
    31+
    function allAnagrams(word, noRepeats) {
    
    32+
      let shuffle = word.split('').map((item, i) => i);
    
    33+
      let result = [];
    
    34+
      
    
    35+
      do {
    
    36+
        let anagram = shuffle.map((index) => word[index]).join('');
    
    37+
        if (!noRepeats || result.indexOf(anagram) < 0) {
    
    38+
          result.push(anagram);
    
    39+
        }
    
    40+
        shuffle = getNextShuffle(shuffle);
    
    41+
      } while (!!shuffle);
    
    42+
    43+
      return result
    
    1818
    }
    
Games
Fundamentals
Basic Language Features
Code
Diff
  • const whoShouldServe = (scoreLeft, scoreRight, servesCount) => {
      return Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second';
    };
  • 1
    function whoShouldServe($scoreLeft, $scoreRight, $servesCount) {
    
    2
      return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
    
    1+
    const whoShouldServe = (scoreLeft, scoreRight, servesCount) => {
    
    2+
      return Math.floor((scoreLeft + scoreRight) / servesCount) % 2 === 0 ? 'first' : 'second';
    
    33
    };