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.

Code
Diff
  • import java.util.List;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Primes {
        public static boolean isPrime(int n) {
            if (n == 2)
                return true;
    
            if (n % 2 == 0) return false;
    
            for (int i = 3; i * i <= n; i += 2) {
                if (n % i == 0)
                    return false;
            }
            return true;
        }
        
        public static List<Integer> generatePrimes(int min, int max) {
            if (!(min > 0 && max > 0))
                return null;
    
            return IntStream.range(min, max)
                    .filter(e -> isPrime(e))
                    .boxed()
                    .collect(Collectors.toList());
        }
    
    }
  • 1
    import java.util.*;
    
    1+
    import java.util.List;
    
    2+
    import java.util.stream.Collectors;
    
    3+
    import java.util.stream.IntStream;
    
    22
    33
    public class Primes {
    
    4
      public static List<Integer> generatePrimes(int min, int max) {
    
    5
        List<Integer> primes = new ArrayList<Integer>();
    
    6
        boolean isPrime = false;
    
    7
        
    
    8
        if((min > max) || min < 0 || max <= 0) {
    
    9
          return null;
    
    10
        }           
    
    11
                
    
    12
        for(int i = min; i <= max; i++) {
    
    13
          long endLimit = (long)Math.floor(Math.sqrt(i));
    
    14
          isPrime = true;
    
    15
          for(long j = 2; j <= endLimit; j++) {
    
    16
            if (i % j == 0) {
    
    17
              isPrime = false;
    
    18
              break;
    
    6+
        public static boolean isPrime(int n) {
    
    7+
            if (n == 2)
    
    8+
                return true;
    
    9+
    10+
            if (n % 2 == 0) return false;
    
    11+
    12+
            for (int i = 3; i * i <= n; i += 2) {
    
    13+
                if (n % i == 0)
    
    14+
                    return false;
    
    1919
            }
    
    20
          }
    
    21
        
    
    22
          if(isPrime) {
    
    23
            primes.add(i);
    
    24
          }
    
    16+
            return true;
    
    2525
        }
    
    2626
        
    
    27
        return primes;
    
    28
      }
    
    19+
        public static List<Integer> generatePrimes(int min, int max) {
    
    20+
            if (!(min > 0 && max > 0))
    
    21+
                return null;
    
    22+
    23+
            return IntStream.range(min, max)
    
    24+
                    .filter(e -> isPrime(e))
    
    25+
                    .boxed()
    
    26+
                    .collect(Collectors.toList());
    
    27+
        }
    
    28+
    2929
    }
    
Hashes
Data Structures
Code
Diff
  • /* HashMap Example */
     
    import java.util.*;
    import java.lang.*;
    import java.io.*;
     
    class HashMapDemo
    {
      public static void main (String[] args) throws java.lang.Exception {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
            put(1, 1);
            put(2, 1);
            put(3, 1);
            put(4, 1);
            put(5, 1);
            put(6, 1);
        }};
    
        map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
      }
    }
  • 11
    /* HashMap Example */
    
    22
     
    
    33
    import java.util.*;
    
    44
    import java.lang.*;
    
    55
    import java.io.*;
    
    66
     
    
    77
    class HashMapDemo
    
    88
    {
    
    9
    	public static void main (String[] args) throws java.lang.Exception
    
    10
    	{
    
    11
    		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    
    12
    		map.put(1, 1);
    
    13
    		map.put(2, 1);
    
    14
    		map.put(3, 1);
    
    15
    		map.put(4, 1);
    
    16
    		map.put(5, 1);
    
    17
    		map.put(6, 1);
    
    18
     
    
    19
    		for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    
    20
    			int key = entry.getKey();
    
    21
    			int value = entry.getValue();
    
    22
    			System.out.println(key + " " + value);
    
    23
    		}
    
    24
    	}
    
    9+
      public static void main (String[] args) throws java.lang.Exception {
    
    10+
        Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
    
    11+
            put(1, 1);
    
    12+
            put(2, 1);
    
    13+
            put(3, 1);
    
    14+
            put(4, 1);
    
    15+
            put(5, 1);
    
    16+
            put(6, 1);
    
    17+
        }};
    
    18+
    19+
        map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
    
    20+
      }
    
    2525
    }
    
Sequences
Arrays
Code
Diff
  • module Collatz where
    
    collatz :: Int -> [Int]
    collatz = iterate next
      where next n | even n = n `div` 2
                   | odd n = n * 3 + 1
  • 11
    module Collatz where
    
    22
    33
    collatz :: Int -> [Int]
    
    4
    collatz n = n : collatz next
    
    5
      where next | even n = n `div` 2
    
    6
                 | odd n = n * 3 + 1
    
    4+
    collatz = iterate next
    
    5+
      where next n | even n = n `div` 2
    
    6+
                   | odd n = n * 3 + 1
    
Code
Diff
  • var merge = function(array1, array2, callbackFN){ 
     return callbackFN(array1,array2);
    }
    
    function callbackFN(a,b) {
      return a.map(function(c,i){return c + b[i];});
    }
    
    var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
    console.log(x);   //x should now equal [6, 8, 10, 12].
    
  • 1
    var merge = function(array1, array2, callback){  
    
    2
      var array3 = [];
    
    3
    4
      if (array1.length !== array2.length) {
    
    5
        console.log("Array length mismatch");
    
    6
        return new Error("Array length mismatch");
    
    7
      } else {
    
    8
        length = array1.length;
    
    9
      }
    
    10
    11
      for (var i = 0; i < length; i++) {
    
    12
        array3[i] = callback(array1[i], array2[i]);
    
    13
      }
    
    14
      return array3;
    
    1+
    var merge = function(array1, array2, callbackFN){ 
    
    2+
     return callbackFN(array1,array2);
    
    1515
    }
    
    1616
    17
    var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){  
    
    18
      return a + b;
    
    19
    });
    
    20
    21
    //x should now equal [6, 8, 10, 12].
    
    22
    23
    var euclid = function(coords1, coords2){  
    
    24
      //Your code here.
    
    25
      //You should not use any loops and should
    
    26
      //instead use your original merge function.
    
    5+
    function callbackFN(a,b) {
    
    6+
      return a.map(function(c,i){return c + b[i];});
    
    2727
    }
    
    2828
    29
    var y = euclid([1.2, 3.67], [2.0, 4.4]);
    
    30
    31
    //y should now equal approximately 1.08.
    
    9+
    var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
    
    10+
    console.log(x);   //x should now equal [6, 8, 10, 12].
    

Recent Moves:

Arrays
Map/Reduce
Algorithms

For loops are commonly overused when other Javascript methods can lead to a cleaner and faster solution. A common example would be finding duplicates in an array.

This function called duplicates takes an array of numbers and returns a new array with the numbers duplicated in the original array ordered by value.

Your goal is to refactor this code to still find duplicates in an array and return those duplicates in a new array, but no longer use a for loop.

Note: numbers and their corresponding string representations should not be treated as duplicates (i.e., '1' !== 1).

Based on http://www.codewars.com/kata/find-duplicates/javascript

function duplicates(arr) {
  var out = [];  
  for(var x=0;x<arr.length-1;x++)
  {
    var ch = arr[x];
    for(var y=x+1;y<arr.length;y++)
    {
      var comp = arr[y];
      if (comp === ch && out.indexOf(comp) === -1)
      {
        out.push(comp);
        break;
      }
    }
  }
  out.sort();
  return out;
}

Ha doesn't really work but yeah

Code
Diff
  • function fizzbuzzy(n) {
      var cones = 1;
      for (i = 0; i < n; i++){
    		!(i % 5) && cones++;
        !(i % 3) && cones++;       
      }
      return cones;
    }
    
  • 11
    function fizzbuzzy(n) {
    
    22
      var cones = 1;
    
    33
      for (i = 0; i < n; i++){
    
    4
        if (i % 5 === 0) cones++;
    
    5
        if (i % 3 === 0) cones++;       
    
    4+
    		!(i % 5) && cones++;
    
    5+
        !(i % 3) && cones++;       
    
    66
      }
    
    77
      return cones;
    
    88
    }
    

Recent Moves: