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
Best Practices
Code
Diff
  • function square_n_sum($array_of_numbers) {
      $acc = 0;
      foreach ($array_of_numbers as $el) {
        $acc += $el ^ 2;
      }
      return $acc;
    }
    echo square_n_sum(array(1,2,3,5,6)); // Should return 75
    echo "<br />";
    echo square_n_sum(array(1,2)); // Should return 5
    echo "<br />";
    echo square_n_sum(array(3,4)); // Should return 25
    echo "<br />";
    echo square_n_sum(array(1,2,3,4)); // Should return 30
    echo "<br />";
    echo square_n_sum(array(1,2,3,4,5,6,7,8,9,99)); // Should return 10086
    • function square_n_sum($array_of_numbers) {
    • for ($i = 0; $i < sizeof($array_of_numbers); $i++) {
    • $array_of_numbers[$i] = $array_of_numbers[$i] ** 2;
    • $acc = 0;
    • foreach ($array_of_numbers as $el) {
    • $acc += $el ^ 2;
    • }
    • $sum_of_squared_numbers = 0;
    • for ($i = 0; $i < sizeof($array_of_numbers); $i++) {
    • $sum_of_squared_numbers += $array_of_numbers[$i];
    • }
    • return $sum_of_squared_numbers;
    • return $acc;
    • }
    • echo square_n_sum(array(1,2,3,5,6)); // Should return 75
    • echo "<br />";
    • echo square_n_sum(array(1,2)); // Should return 5
    • echo "<br />";
    • echo square_n_sum(array(3,4)); // Should return 25
    • echo "<br />";
    • echo square_n_sum(array(1,2,3,4)); // Should return 30
    • echo "<br />";
    • echo square_n_sum(array(1,2,3,4,5,6,7,8,9,99)); // Should return 10086
Code
Diff
  • var merge = function(array1, array2, callbackFN){ 
     return callbackFN(array1,array2);
    }
    
    function callbackFN(a,b) {
      if(Array.isArray(a) && Array.isArray(b) && (a.length === b.length)){
        return a.map(function(c,i){ return c + b[i]; });
      } else {
        return 'both inputs must be Array and same size of elements';
      }
      
    }
    
    var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN);
    console.log(x);   //x should now equal [6, 8, 10, 12].
    
    • var merge = function(array1, array2, callbackFN){
    • return callbackFN(array1,array2);
    • }
    • function callbackFN(a,b) {
    • return a.map(function(c,i){return c + b[i];});
    • if(Array.isArray(a) && Array.isArray(b) && (a.length === b.length)){
    • return a.map(function(c,i){ return c + b[i]; });
    • } else {
    • return 'both inputs must be Array and same size of elements';
    • }
    • }
    • var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN);
    • var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN);
    • console.log(x); //x should now equal [6, 8, 10, 12].
Code
Diff
  • def power_of_two( n ):
        return bool(n and not (n&(n-1)))
    • def power_of_two( n ):
    • return n & ( n - 1 ) == 0
    • return bool(n and not (n&(n-1)))
Code
Diff
  • import java.util.*;
    import java.util.List;
    import java.util.Map;
    import java.util.Optional;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    /**
      Time Complexity  : O(N)
      Space Complexity : O(N)
    */
    class MaxOccurence {
        
      public static int findMax(int[] nums) {
        Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
                .boxed()
                .collect(Collectors.groupingBy(num -> num))
                .entrySet()
                .stream()
                .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
            
        return max.isPresent() ? max.get().getKey() : -1;
      }
    }
    • import java.util.*;
    • import java.util.List;
    • import java.util.Map;
    • import java.util.Optional;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • /**
    • Time Complexity : O(N)
    • Space Complexity : O(N)
    • */
    • class MaxOccurence {
    • public static int findMax(int[] nums) {
    • return findMaxOccurenceLinkedHashMap(nums);
    • Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
    • .boxed()
    • .collect(Collectors.groupingBy(num -> num))
    • .entrySet()
    • .stream()
    • .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
    • return max.isPresent() ? max.get().getKey() : -1;
    • }
    • private static int findMaxOccurenceLinkedHashMap(int[] nums) {
    • int maxKey = 0, maxNum = 0;
    • if(nums.length < 1) return -1;
    • if(nums.length == 1) return nums[0];
    • Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>(nums.length);
    • for(int i = 0, len = nums.length; i < len; i++) {
    • if(!counts.containsKey(nums[i])) {
    • counts.put(nums[i], 1);
    • } else {
    • counts.put(nums[i], (counts.get(nums[i]) + 1));
    • }
    • }
    • for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
    • if (entry.getValue() > maxNum) {
    • maxKey = entry.getKey();
    • maxNum = entry.getValue();
    • }
    • }
    • return maxKey;
    • }
    • }
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()));
      }
    }
    • /* 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>();
    • map.put(1, 1);
    • map.put(2, 1);
    • map.put(3, 1);
    • map.put(4, 1);
    • map.put(5, 1);
    • map.put(6, 1);
    • for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
    • int key = entry.getKey();
    • int value = entry.getValue();
    • System.out.println(key + " " + value);
    • }
    • }
    • 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()));
    • }
    • }
Sequences
Arrays
Data Types
Code
Diff
  • module Collatz where
    
    collatz :: Int -> [Int]
    collatz = iterate next
      where next n | even n = n `div` 2
                   | odd n = n * 3 + 1
    • module Collatz where
    • collatz :: Int -> [Int]
    • collatz n = n : collatz next
    • where next | even n = n `div` 2
    • | odd n = n * 3 + 1
    • collatz = iterate next
    • where next n | even n = n `div` 2
    • | odd n = n * 3 + 1

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;
    }
    
    • function fizzbuzzy(n) {
    • var cones = 1;
    • for (i = 0; i < n; i++){
    • if (i % 5 === 0) cones++;
    • if (i % 3 === 0) cones++;
    • !(i % 5) && cones++;
    • !(i % 3) && cones++;
    • }
    • return cones;
    • }