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
  • class Solution {
        public static void main(String[] args) {
            System.out.println("What is next?");
        }
    }
  • 11
    class Solution {
    
    22
        public static void main(String[] args) {
    
    3
            System.out.println("Hello darkness, my old friend...");
    
    3+
            System.out.println("What is next?");
    
    44
        }
    
    55
    }
    

Implementation of Merge Sort.

  • Time Complexity: O(n log(n))

  • Space Complexity: O(n)

  • Preserves the input order of equal elements in the sorted output.

  • Type of Divide-and-Conquer algorithm.

  • Efficient at handling slow-to-access sequential media.

  • Well-suited for sorting huge amounts of data that does not fit into memory.

  • Wiki Link: Merge Sort

class Sort:

    def merge_sort(self, nums):
        if nums == None: return None

        if len(nums) > 1:
            mid = len(nums) // 2
            lefthalf = nums[:mid]
            righthalf = nums[mid:]

            self.merge_sort(lefthalf)
            self.merge_sort(righthalf)

            i = 0
            j = 0
            k = 0

            while i < len(lefthalf) and j < len(righthalf):
                if lefthalf[i] < righthalf[j]:
                    nums[k] = lefthalf[i]
                    i += 1
                else:
                    nums[k] = righthalf[j]
                    j += 1
                k += 1

            while i < len(lefthalf):
                nums[k] = lefthalf[i]
                i += 1
                k += 1

            while j < len(righthalf):
                nums[k] = righthalf[j]
                j += 1
                k += 1

        return nums
Sorting
Algorithms

Implementation of Insertion Sort.

  • Time Complexity: Worst case: O(n^2), Best case: O(n)

  • Space Complexity: O(1)

  • Useful when list size is small.

  • only scans as many elements as needed to determine the correct location.

  • More efficient than bubble or selection sort.

  • Efficient for data sets that are already substantially sorted.

  • Requires more writes because the inner loop can require shifting large sections of the sorted portion of the array.

  • Can sort a list as it receives it.

  • Wiki Link: Insertion Sort

class Sort:

    def insertion_sort(self, nums):
        if nums is None:
            return None

        for i in range(1, len(nums)):
            currentvalue = nums[i]
            position = i

            while position > 0 and nums[position - 1] > currentvalue:
                nums[position] = nums[position - 1]
                position -= 1

            nums[position] = currentvalue
        return nums
Sorting
Algorithms

Implementation of Selection Sort.

  • Time Complexity: O(n^2)
  • Space Complexity: O(1)
  • Useful when list size is small
  • Preferable to insertion sort in terms of number of writes (O(n) swaps versus O(n2) swaps), it almost always far exceeds (and never beats) the number of writes that cycle sort makes, as cycle sort is theoretically optimal in the number of writes. This can be important if writes are significantly more expensive than reads, such as with EEPROM or Flash memory, where every write lessens the lifespan of the memory.
  • Wiki Link: Selection Sort
class Sort:

    def selection_sort(self, nums):
        if nums == None: return None

        for i in range(0, len(nums) - 1):
            minIndex = i

            for j in range(i + 1, len(nums)):
                if nums[j] < nums[minIndex]:
                    minIndex = j

            nums[i], nums[minIndex] = nums[minIndex], nums[i]
        return nums
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;
      }
    }
  • 11
    import java.util.*;
    
    2+
    import java.util.List;
    
    3+
    import java.util.Map;
    
    4+
    import java.util.Optional;
    
    5+
    import java.util.stream.Collectors;
    
    6+
    import java.util.stream.IntStream;
    
    22
    33
    /**
    
    44
      Time Complexity  : O(N)
    
    55
      Space Complexity : O(N)
    
    66
    */
    
    77
    class MaxOccurence {
    
    13+
        
    
    88
      public static int findMax(int[] nums) {
    
    9
        return findMaxOccurenceLinkedHashMap(nums);
    
    15+
        Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
    
    16+
                .boxed()
    
    17+
                .collect(Collectors.groupingBy(num -> num))
    
    18+
                .entrySet()
    
    19+
                .stream()
    
    20+
                .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
    
    21+
            
    
    22+
        return max.isPresent() ? max.get().getKey() : -1;
    
    1010
      }
    
    11
      
    
    12
      private static int findMaxOccurenceLinkedHashMap(int[] nums) {
    
    13
        int maxKey = 0, maxNum = 0;
    
    14
        
    
    15
        if(nums.length < 1) return -1;
    
    16
        if(nums.length == 1) return nums[0];
    
    17
        
    
    18
        Map<Integer, Integer> counts = new LinkedHashMap<Integer, Integer>(nums.length);
    
    19
        
    
    20
        for(int i = 0, len = nums.length; i < len; i++) {
    
    21
          if(!counts.containsKey(nums[i])) {
    
    22
            counts.put(nums[i], 1);
    
    23
          } else {
    
    24
            counts.put(nums[i], (counts.get(nums[i]) + 1));
    
    25
          }
    
    26
        }
    
    27
        
    
    28
        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {  
    
    29
          if (entry.getValue() > maxNum) {
    
    30
            maxKey = entry.getKey();
    
    31
            maxNum = entry.getValue();
    
    32
          }  
    
    33
        }  
    
    34
        
    
    35
        return maxKey;
    
    36
      } 
    
    3737
    }
    

Recent Moves:

A couple who work on a farm have asked you to create a program for them that will allow to them store the types of animals they have on the farm, these include:

-Cows
-Chickens
-Pigs

Using interfaces we can make the process of creating the classes for each animal easier. By using an animal interface we can group the animals together in the code.

interface AnimalsInterface
{
    public function getLegs();
    public function setLegs($noOfLegs);
    public function getDiet();
    public function setDiet($foodTheyEat);
}

class Cow implements AnimalsInterface
{
    private $legs;
    private $diet;
    
    public function getLegs()
    {
        return $this->legs;
    }
    public function setLegs($noOfLegs)
    {
        $this->legs = $noOfLegs;
        return $this;
    }
    public function getDiet()
    {
        return $this->diet;
    }
    public function setDiet($foodTheyEat)
    {
        $this->diet = $foodTheyEat;
        return $this;
    }
}

class Chicken implements AnimalsInterface
{
    private $legs;
    private $diet;
    
    public function getLegs()
    {
        return $this->legs;
    }
    public function setLegs($noOfLegs)
    {
        $this->legs = $noOfLegs;
        return $this;
    }
    public function getDiet()
    {
        return $this->diet;
    }
    public function setDiet($foodTheyEat)
    {
        $this->diet = $foodTheyEat;
        return $this;
    }
}

class Pig implements AnimalsInterface
{
    private $legs;
    private $diet;
    
    public function getLegs()
    {
        return $this->legs;
    }
    public function setLegs($noOfLegs)
    {
        $this->legs = $noOfLegs;
        return $this;
    }
    public function getDiet()
    {
        return $this->diet;
    }
    public function setDiet($foodTheyEat)
    {
        $this->diet = $foodTheyEat;
        return $this;
    }
}

class Farm
{
    private $animals = [];
    
    public function addAnimal(AnimalsInterface $animal)
    {
        array_push($this->animals, $animal)
    }
    public function displayAnimals()
    {
        foreach($this->animals as $animal) {
            echo $animal->getLegs();
            echo $animal->getDiet();
        }
    }
}

$cow = new Cow();
$cow->setLegs(4)->setDiet('Grass');
$chicken = new Chicken();
$chicken->setLegs(2)->setDiet('Seeds');
$pig = new Pig();
$pig->setLegs(4)->setDiet('Corn');
$farm = new Farm;
$farm->addAnimal($cow);
$farm->addAnimal($chicken);
$farm->addAnimal($pig);
$farm->displayAnimal();
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
    }