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
  • const revstr = str => str.split('').reverse().join('')
  • 1
    function revstr(str) {
    
    2
      i = str.length; newstr = "";
    
    3
      while (i > 0) {
    
    4
        newstr += str[i - 1]; i--;
    
    5
      }
    
    6
      return newstr;
    
    7
    }
    
    1+
    const revstr = str => str.split('').reverse().join('')
    
Objects
Strings
Code
Diff
  • export const transform = (source: Record<string, any>): Record<string, any> => {
        const target: Record<string, any> = Object.create(null);
      
        for (let [key, value] of Object.entries(source)) {
          const lastUnderscoreIndex = key.lastIndexOf("_");
          
          const keyValid = key.length > 1;
          const valueKey = keyValid ? key.slice(lastUnderscoreIndex + 1) : key;
          
          let deepestNode = target;
          if (keyValid) {
            deepestNode = key.split("_")
              .slice(0, -1)
              .reduce((node: Record<string, any>, element: string) => node[element] ??= {}, target);
          }
          
          if (valueKey === '_') {
            //  dont get why it should be value_d but ok  
            value = 'value_d';
          }
          
          deepestNode[valueKey] = value;
        }
        return target;
    }
  • 11
    export const transform = (source: Record<string, any>): Record<string, any> => {
    
    2
        const target = Object.create(null);
    
    3
        // TODO: handle invalid property
    
    4
        Object.entries(source).forEach(([key, value]) => {
    
    5
            key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {
    
    6
                return node[element] ??= {};
    
    7
            }, target)[key.slice(key.lastIndexOf("_") + 1)] = value;
    
    8
        });
    
    2+
        const target: Record<string, any> = Object.create(null);
    
    3+
      
    
    4+
        for (let [key, value] of Object.entries(source)) {
    
    5+
          const lastUnderscoreIndex = key.lastIndexOf("_");
    
    6+
          
    
    7+
          const keyValid = key.length > 1;
    
    8+
          const valueKey = keyValid ? key.slice(lastUnderscoreIndex + 1) : key;
    
    9+
          
    
    10+
          let deepestNode = target;
    
    11+
          if (keyValid) {
    
    12+
            deepestNode = key.split("_")
    
    13+
              .slice(0, -1)
    
    14+
              .reduce((node: Record<string, any>, element: string) => node[element] ??= {}, target);
    
    15+
          }
    
    16+
          
    
    17+
          if (valueKey === '_') {
    
    18+
            //  dont get why it should be value_d but ok  
    
    19+
            value = 'value_d';
    
    20+
          }
    
    21+
          
    
    22+
          deepestNode[valueKey] = value;
    
    23+
        }
    
    99
        return target;
    
    1010
    }
    

O(1) solution.
Now returns None for cases where it is impossible to meet constraints.

Code
Diff
  • def optimal_gifts(gifts: int, recipients: int) -> int:
        if recipients > gifts or gifts == 4 and recipients == 1:
            return None
        if gifts >= recipients * 8:
            return recipients
        if gifts == recipients * 8 - 4 or gifts == (recipients - 1) * 8:
            return recipients - 2
        return gifts // 8
  • 11
    def optimal_gifts(gifts: int, recipients: int) -> int:
    
    2
        kids = [1 for _ in range(gifts)]
    
    3
        gifts -= recipients
    
    4
        for i in range(recipients):
    
    5
            while (gifts > 0 and kids[i] < 8):
    
    6
                kids[i] += 1
    
    7
                gifts -= 1
    
    8
    9
        eigths = sum([1 if kid == 8 else 0 for kid in kids])
    
    10
    11
        if 4 in kids:
    
    12
            eigths -= 1
    
    13
            
    
    14
        return eigths
    
    15
        
    
    2+
        if recipients > gifts or gifts == 4 and recipients == 1:
    
    3+
            return None
    
    4+
        if gifts >= recipients * 8:
    
    5+
            return recipients
    
    6+
        if gifts == recipients * 8 - 4 or gifts == (recipients - 1) * 8:
    
    7+
            return recipients - 2
    
    8+
        return gifts // 8
    

Recent Moves:

Code
Diff
  • import static java.util.stream.DoubleStream.of;
    import static org.apache.commons.lang3.ArrayUtils.reverse;
    
    interface DeleteDuplicates {
      static double[] delete(double[] a) {
        if (of(a).anyMatch(d -> d < 0)) {
          return new double[]{-1};
        }
        reverse(a);
        var unique = of(a).distinct().toArray();
        reverse(unique);
        return unique;
      }
    }
  • 1
    import java.util.*;
    
    1+
    import static java.util.stream.DoubleStream.of;
    
    2+
    import static org.apache.commons.lang3.ArrayUtils.reverse;
    
    22
    3
    class DeleteDuplicates {
    
    4
      public static double[] delete(double[] a) { 
    
    5
        
    
    6
        for(int i = 0; i < a.length / 2; i++) {
    
    7
          double temp = a[i];
    
    8
          a[i] = a[a.length - i - 1];
    
    9
          a[a.length - i - 1] = temp;
    
    4+
    interface DeleteDuplicates {
    
    5+
      static double[] delete(double[] a) {
    
    6+
        if (of(a).anyMatch(d -> d < 0)) {
    
    7+
          return new double[]{-1};
    
    1010
        }
    
    11
    12
        Set<Double> set = new LinkedHashSet<>();
    
    13
        for (double v : a) {
    
    14
          set.add(v);
    
    15
          if(v < 0.0)
    
    9+
        reverse(a);
    
    10+
        var unique = of(a).distinct().toArray();
    
    11+
        reverse(unique);
    
    12+
        return unique;
    
    2727
      }
    
    2828
    }
    
Regular Expressions
Declarative Programming
Advanced Language Features
Fundamentals
Strings
Code
Diff
  • interface StringParity {
      static boolean isEvenLength(String str) {
        return str.length() % 2 < 1;
      }
    }
  • 1
    class StringParity {
    
    2
        public static boolean isEvenLength(String str) {
    
    3
          return (str.length() & 1) == 0;
    
    4
        }
    
    1+
    interface StringParity {
    
    2+
      static boolean isEvenLength(String str) {
    
    3+
        return str.length() % 2 < 1;
    
    4+
      }
    
    55
    }
    
Code
Diff
  • interface Kumite {
      static int add(int x, int y) {
        return Math.addExact(x, y);
      }
    }
  • 1
    public class Kumite {
    
    2
      public static int add(int x, int y) {
    
    3
        return x+y;
    
    1+
    interface Kumite {
    
    2+
      static int add(int x, int y) {
    
    3+
        return Math.addExact(x, y);
    
    44
      }
    
    55
    }
    

Always forget how to exact determine programmer's day. Some code may help do it exactly and without heavy calculations.

# How to check that target date is Programmer's day?

def is_programmers_day(date):
    return date.timetuple().tm_yday == 256
Fundamentals
Strings
Code
Diff
  • public class FizzBuzz
     {
       public string GetOutput(int number)
       {
         bool fizz = number % 3 == 0;
         bool buzz = number % 5 == 0;
         return fizz && buzz ? "FizzBuzz" : fizz ? "Fizz" : buzz ? "Buzz" : number.ToString();
       }
    }
  • 11
    public class FizzBuzz
    
    22
     {
    
    33
       public string GetOutput(int number)
    
    44
       {
    
    5
         if ((number % 3 == 0) && (number % 5 == 0))
    
    6
           return "FizzBuzz";
    
    7
    8
         else if (number % 3 == 0)
    
    9
           return "Fizz";
    
    10
    11
         else if (number % 5 == 0)
    
    12
           return "Buzz";
    
    13
    14
        else return number.ToString(); 
    
    5+
         bool fizz = number % 3 == 0;
    
    6+
         bool buzz = number % 5 == 0;
    
    7+
         return fizz && buzz ? "FizzBuzz" : fizz ? "Fizz" : buzz ? "Buzz" : number.ToString();
    
    1515
       }
    
    1616
    }
    

Recent Moves: