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.Arrays;
    
    interface HighLow {
      static int[] findLargestAndSmallest(int[] nums) {
        if(nums == null || nums.length == 0) return null;
    
        int min = Arrays.stream(nums).min().orElseThrow();
        int max = Arrays.stream(nums).max().orElseThrow();
        return new int[]{min, max};
      }
    }
    
  • 1+import java.util.Arrays;
    2+
    11 interface HighLow {
    2 static int[] findLargestAndSmallest(int[] nums) {
    3 // Change min and max below to be the highest and lowest values
    4 // in the nums array provided to the function
    5 int min = -9;
    6 int max = 9;
    7 return new int[]{min, max};
    8 }
    9
    10 }
    11
    4+ static int[] findLargestAndSmallest(int[] nums) {
    5+ if(nums == null || nums.length == 0) return null;
    1212
    7+ int min = Arrays.stream(nums).min().orElseThrow();
    8+ int max = Arrays.stream(nums).max().orElseThrow();
    9+ return new int[]{min, max};
    10+ }
    1313 }

Recent Moves:

Code
Diff
  • const parse = (value = '') => {
      // if valid email, return true, else return false
      let validEmail;
      const validCharacters = ["a","A", "b","B","c","C","d","D","e","E","f","F",
                               "g","G","h","H","i","I","j","J","k","K","l","L",
                               "m","M","n","N","o","O","p","P","q","Q","r","R",
                               "s","S","t","T","u","U","v","V","w","W","x","X",
                               "y","Y","z","Z","0","1","2","3","4","5","6","7",
                               "8","9","@","."]
      const emailArray = value.split("");
      for (let i = 0; i < emailArray.length; i++) {
        if (validCharacters.includes(value[i]) == true) {
          validEmail = true;
        }
        else {
          validEmail = false;
          break;
        }
      }
      return validEmail;
    }
  • 11 const parse = (value = '') => {
    22 // if valid email, return true, else return false
    3 return true;
    3+ let validEmail;
    4+ const validCharacters = ["a","A", "b","B","c","C","d","D","e","E","f","F",
    5+ "g","G","h","H","i","I","j","J","k","K","l","L",
    6+ "m","M","n","N","o","O","p","P","q","Q","r","R",
    7+ "s","S","t","T","u","U","v","V","w","W","x","X",
    8+ "y","Y","z","Z","0","1","2","3","4","5","6","7",
    9+ "8","9","@","."]
    10+ const emailArray = value.split("");
    11+ for (let i = 0; i < emailArray.length; i++) {
    12+ if (validCharacters.includes(value[i]) == true) {
    13+ validEmail = true;
    14+ }
    15+ else {
    16+ validEmail = false;
    17+ break;
    18+ }
    19+ }
    20+ return validEmail;
    44 }
Code
Diff
  • function prime_checker(n) 
    {
      if(n <= 1)
      {
        return false;
      }
      for(var i = 2; i <= Math.sqrt(n); i++)
      {
        if(n%i == 0)
        {
          return false;
        }
      }
      return true;
    }
  • 1function prime_checker (n) {
    2 // Write a javascript function to check if N is a prime number
    1+function prime_checker(n)
    2+{
    3+ if(n <= 1)
    4+ {
    5+ return false;
    6+ }
    7+ for(var i = 2; i <= Math.sqrt(n); i++)
    8+ {
    9+ if(n%i == 0)
    10+ {
    11+ return false;
    12+ }
    13+ }
    14+ return true;
    33 }
Code
Diff
  • import org.apache.commons.lang3.ArrayUtils;
    
    public class Kata {
        public static int findIndex (int[] array, int target) {
          return ArrayUtils.indexOf(array, target);
        }
    }
  • 1+import org.apache.commons.lang3.ArrayUtils;
    2+
    11 public class Kata {
    2 public static int findIndex (int[] my_array, int t) {
    3 for(int i=0; i< my_array.length; i++){
    4 if(my_array[i]==t){
    5 return i;
    6 }
    7
    8 }
    9 return 4;
    4+ public static int findIndex (int[] array, int target) {
    5+ return ArrayUtils.indexOf(array, target);
    1010 }
    11
    1212 }

Pointers at the beginning and end of our index list (i = 0, j = end of index)
While i is less than j
if there is a mismatch return false !=
if not add i and take away -1 from j until i is equal to or larger than j

Code
Diff
  • public class Kata  {
      public static boolean detectPalindrome(String userWord){
     
            int i = 0, j = userWord.length() - 1;
     
            while (i < j) {
     
                if (userWord.charAt(i) != userWord.charAt(j))
                    return false;
              
                i++;
                j--;
            }
            return true;
        }
      }
  • 1public class Kata {
    2 public static boolean detectPalindrome(String userWord) {
    3 // Detect whether this string is a palindrome
    4 return true;
    1+public class Kata {
    2+ public static boolean detectPalindrome(String userWord){
    3+
    4+ int i = 0, j = userWord.length() - 1;
    5+
    6+ while (i < j) {
    7+
    8+ if (userWord.charAt(i) != userWord.charAt(j))
    9+ return false;
    10+
    11+ i++;
    12+ j--;
    13+ }
    14+ return true;
    15+ }
    55 }
    6}
Code
Diff
  • public class Kata {
        public static int findMax(int[] my_array) {
            // Write a method that returns the largest integer in the list.
            // You can assume that the list has at least one element.
            return 7;
        }
    }
    class SolutionTest {
        @Test
        void testSomething() {
            assertEquals(Kata.findMax(new int[]
  • 11 public class Kata {
    22 public static int findMax(int[] my_array) {
    33 // Write a method that returns the largest integer in the list.
    44 // You can assume that the list has at least one element.
    55 return 7;
    66 }
    77 }
    8+class SolutionTest {
    9+ @Test
    10+ void testSomething() {
    11+ assertEquals(Kata.findMax(new int[]
Arrays
Heaps
Trees
Data Structures

Works a bit like a heapq, but since max height is 1, we only need two lists to simulate it. (same, nxt)

Code
Diff
  • def to_be_flipped(mat):
        cost, seen, same, nxt = mat[0][0], {(0, 0)}, [(0, 0)], []
        while True:
            while same:
                cy, cx = same.pop()
                if cy == len(mat) - 1 and cx == len(mat[0]) - 1:
                    return cost
                for dy, dx in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                    y, x = cy+dy, cx+dx
                    if 0 <= y < len(mat) and 0 <= x < len(mat[0]) and (y, x) not in seen:
                        (same, nxt)[mat[y][x]].append((y,x))
                        seen.add((y, x))
            same, nxt = nxt, []
            cost += 1
    
  • 1from heapq import heapify, heappop, heappush
    2
    33 def to_be_flipped(mat):
    4 ori = [[n for n in r] for r in mat]
    5 mat = [[n and 10001 for n in r] for r in mat]
    6 mat[0][0] = 1
    7 queue = [(1, 0, 0)]
    8 heapify(queue)
    2+ cost, seen, same, nxt = mat[0][0], {(0, 0)}, [(0, 0)], []
    3+ while True:
    4+ while same:
    5+ cy, cx = same.pop()
    6+ if cy == len(mat) - 1 and cx == len(mat[0]) - 1:
    7+ return cost
    8+ for dy, dx in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
Code
Diff
  • import java.util.*;
    import java.util.LinkedList;
    
    public class Kata {
        public int index = -1;
        public LinkedList<String> linkedList;
        
        public Kata()
        {
            linkedList = new LinkedList<String>();
        }
        public LinkedList<String> AddItem(String itemToAdd, String whereToAddIt) {
          // When given a string, whereToAddIt, find that string in the LinkedList
          for (int i = 0; i < linkedList.size(); i++) {
      
                // Extracting each element in the Linked List
                String element = linkedList.get(i);
      
                // Checking if the extracted element is equal to
                // the element to be searched
                if (element == whereToAddIt) {
      
                    // Assigning the index of the element to a variable
                    index = i;
                    break;
                }
            }
            // add itemToAdd directly after whereToAddIt in the LinkedList
            int position = index + 1;
            linkedList.add(position, itemToAdd);
            return linkedList;
        }
        
        public LinkedList<String> RemoveItem(String whereToRemoveIt) {
             // When given a string, whereToRemoveIt, find that string
             // in the LinkedList and remove it from the linked list
          for (int i = 0; i < linkedList.size(); i++) {
      
                // Extracting each element in the Linked List
                String element = linkedList.get(i);
      
                // Checking if the extracted element is equal to
                // the element to be searched
                if (element == whereToRemoveIt) {
      
                    // Assigning the index of the element to a variable
                    index = i;
                    linkedList.remove(index);
                    break;
                }
            }
          return linkedList;
        }
      
        public LinkedList<String> MergeLists(LinkedList<String> listToMerge) {
             // Merge this linked list with the previous
            linkedList.addAll(listToMerge);
            return linkedList;
        }
        public void setList(LinkedList<String> newList) {
          linkedList = newList;
        }
        
    }
  • 11 import java.util.*;
    2+import java.util.LinkedList;
    22
    33 public class Kata {
    5+ public int index = -1;
    6+ public LinkedList<String> linkedList;
    7+
    8+ public Kata()
    9+ {
    10+ linkedList = new LinkedList<String>();
    11+ }
    12+ public LinkedList<String> AddItem(String itemToAdd, String whereToAddIt) {
    13+ // When given a string, whereToAddIt, find that string in the LinkedList
    14+ for (int i = 0; i < linkedList.size(); i++) {
    44
    5 private LinkedList<String> linkedList;
    16+ // Extracting each element in the Linked List
    17+ String element = linkedList.get(i);
    66
    7 public static void AddItem(String itemToAdd, String whereToAddIt) {
    8 // When given a string, whereToAddIt, find that string
    9 // in the LinkedList and add itemToAdd directly after whereToAddIt
    10 // in the LinkedList
    19+ // Checking if the extracted element is equal to
    20+ // the element to be searched
    21+ if (element == whereToAddIt) {
    22+
    23+ // Assigning the index of the element to a variable
    24+ index = i;
    1111 }
    1212
    13 public static void RemoveItem(String whereToRemoveIt) {
    34+ public LinkedList<String> RemoveItem(String whereToRemoveIt) {
    1414 // When given a string, whereToRemoveIt, find that string
    1515 // in the LinkedList and remove it from the linked list
    50+ }
    51+ }
    52+ return linkedList;
    1616 }
    1717
    18 public static void MergeLists(LinkedList<String> listToMerge) {
    55+ public LinkedList<String> MergeLists(LinkedList<String> listToMerge) {
    1919 // Merge this linked list with the previous
    57+ linkedList.addAll(listToMerge);
    58+ return linkedList;
    59+ }
    60+ public void setList(LinkedList<String> newList) {
    61+ linkedList = newList;
    2020 }
    2121
    2222 }
Permutations
Algorithms
Code
Diff
  • #include <map>
    #include <array>
    #include <string>
    #include <numeric> // accumulate
    using namespace std;
    double oc(char c, double d1, double d2) {
      switch (c) {
          case '+': return d1+d2; break;
          case '-': return d1-d2; break;
          case '/': return d1/d2; break;
          case '*': return d1*d2; break;
          default: return 0.0;
      }
    //  return 0.0;
    }
    
    double calculate(const array<int, 5>& nums, const string& ops) {
        return accumulate(next(nums.begin()),nums.end(),nums[0],
                              [&,i=0](auto acc,auto d) mutable {
                                  return oc(ops[i++],acc,d); });
    }
    
    pair<string, string> MinMaxEquations(const array<int, 5>& numbers) {
        map<double,string> results; // double first: insertion is sorted
        string ops = "+-*/";
    //  No need to sort here ?
        do {
            results[calculate(numbers, ops)]=ops;
        } while (next_permutation(ops.begin(), ops.end()));
        return {results.begin()->second,results.rbegin()->second};
    }
    
  • 11 #include <map>
    22 #include <array>
    33 #include <string>
    44 #include <numeric> // accumulate
    5
    5+using namespace std;
    66 double oc(char c, double d1, double d2) {
    77 switch (c) {
    8 case '+':return d1+d2;
    9 case '-':return d1-d2;
    10 case '/':return d1/d2;
    11 case '*':return d1*d2;
    8+ case '+': return d1+d2; break;
    9+ case '-': return d1-d2; break;
    10+ case '/': return d1/d2; break;
    11+ case '*': return d1*d2; break;
    1212 default: return 0.0;
    1313 }
    1414 // return 0.0;
    1515 }
    1616
    1717 double calculate(const array<int, 5>& nums, const string& ops) {
    18 return std::accumulate(std::next(nums.begin()),nums.end(),nums[0],
    18+ return accumulate(next(nums.begin()),nums.end(),nums[0],
    1919 [&,i=0](auto acc,auto d) mutable {
    2020 return oc(ops[i++],acc,d); });
    2121 }
    2222
    23std::pair<std::string, std::string> MinMaxEquations(const array<int, 5>& numbers) {
    24 std::map<double,string> results; // double first: insertion is sorted
    25 std::string ops = "+-*/";
    23+pair<string, string> MinMaxEquations(const array<int, 5>& numbers) {
    24+ map<double,string> results; // double first: insertion is sorted
    25+ string ops = "+-*/";
    2626 // No need to sort here ?
    2727 do {
    2828 results[calculate(numbers, ops)]=ops;
    29 } while (std::next_permutation(ops.begin(), ops.end()));
    29+ } while (next_permutation(ops.begin(), ops.end()));
    3030 return {results.begin()->second,results.rbegin()->second};
    3131 }