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 Kata
    {
      public static void Main()
      {
        string language = "C#";
        System.Console.WriteLine($"Hello, {language}!");
      }
    }
  • 1
    class Kata{
    
    2
      public static void Main(){
    
    1+
    class Kata
    
    2+
    {
    
    3+
      public static void Main()
    
    4+
      {
    
    33
        string language = "C#";
    
    4
        System.Console.WriteLine("Hello, {0}!",language);
    
    6+
        System.Console.WriteLine($"Hello, {language}!");
    
    55
      }
    
    66
    }
    
Code
Diff
  • function returnhundred() {
      return returnhundred.name.charCodeAt(9);
    }
  • 11
    function returnhundred() {
    
    2
    return 10 ** 2;
    
    2+
      return returnhundred.name.charCodeAt(9);
    
    33
    }
    

Recent Moves:

Code
Diff
  • defmodule Solution do
      def sum(a, b) do
        a + b - 0
      end
    end
  • 11
    defmodule Solution do
    
    22
      def sum(a, b) do
    
    3
        a + b
    
    3+
        a + b - 0
    
    44
      end
    
    55
    end
    
Code
Diff
  • reverse_compliment = lambda dna: dna.upper()[::-1].translate(str.maketrans("ACGT", "TGCA"))
  • 1
    reverse_compliment = lambda dna: dna[::-1].translate(str.maketrans("ACGTacgt", "TGCATGCA"))
    
    1+
    reverse_compliment = lambda dna: dna.upper()[::-1].translate(str.maketrans("ACGT", "TGCA"))
    
Mathematics
Algorithms
Numbers
Code
Diff
  • def average(g)
      g.empty? ? nil : g.sum.to_f/g.size
    end 
  • 11
    def average(g)
    
    2
      g==[] ? nil : g.sum.to_f/g.size
    
    2+
      g.empty? ? nil : g.sum.to_f/g.size
    
    33
    end 
    
Numbers
Integers
Algorithms
Code
Diff
  • fn digits(n: u64, base: u64) -> usize {
        digits_iter(n, base).count()
    }
    
    fn digits_iter(n: u64, base: u64) -> impl Iterator<Item = u64> {
        let mut n = Some(n);
        std::iter::from_fn(move || match n {
            Some(x) => {
                let digit = x % base;
                n = Some(x / base);
    
                if let Some(0) = n {
                    n = None;
                }
                Some(digit)
            }
            _ => None,
        })
    }
    
  • 11
    fn digits(n: u64, base: u64) -> usize {
    
    2
        DigitIterator::new(n, base).count()
    
    2+
        digits_iter(n, base).count()
    
    33
    }
    
    44
    5
    enum DigitIterator {
    
    6
        RemainingDigits { value: u64, base: u64 },
    
    7
        Done,
    
    8
    }
    
    9
    10
    impl DigitIterator {
    
    11
        pub fn new(value: u64, base: u64) -> Self {
    
    12
            Self::RemainingDigits { value, base }
    
    13
        }
    
    14
    }
    
    5+
    fn digits_iter(n: u64, base: u64) -> impl Iterator<Item = u64> {
    
    6+
        let mut n = Some(n);
    
    7+
        std::iter::from_fn(move || match n {
    
    8+
            Some(x) => {
    
    9+
                let digit = x % base;
    
    10+
                n = Some(x / base);
    
    1515
    16
    impl Iterator for DigitIterator {
    
    17
        type Item = u64;
    
    18
        
    
    19
        fn next(&mut self) -> Option<u64>{
    
    20
            use DigitIterator::*;
    
    21
            match self {
    
    22
                Done => None,
    
    23
                RemainingDigits { value, base } => {
    
    24
                    let digit = *value % *base;
    
    25
                    *value /= *base;
    
    26
                    if *value == 0 { *self = Done; }
    
    27
                    Some(digit)
    
    12+
                if let Some(0) = n {
    
    13+
                    n = None;
    
    2828
                }
    
    15+
                Some(digit)
    
    2929
            }
    
    30
        }
    
    17+
            _ => None,
    
    18+
        })
    
    3131
    }
    

Compile-Time Tuple Utilities

Here we have introduced two new useful functions:

  • peel_front
  • print_tuple
  • tuple_split

These basic utilities allow us to perform compile time type manipulations and extend the stl.

peel_front will remove a single element from the front of a tuple and return it.
tuple_split takes either a type parameter to split on or a list of indexes to split on.
print_tuple takes an abitrary tuple and prints it.

template <typename... TYPES, typename INTTYPE, INTTYPE... Idxs>
constexpr auto peel_front(std::tuple<TYPES...> tuple, std::integer_sequence<INTTYPE, Idxs...>) {
  return std::make_tuple(std::get<Idxs + 1>(tuple)...);
}


template <typename FIRST, typename... TYPES>
constexpr std::tuple<TYPES...> peel_front(std::tuple<FIRST, TYPES...> val) {
  return peel_front(val, std::make_integer_sequence<int, sizeof...(TYPES)>());
}

namespace _detail {

    struct tag{};

  template <typename TYPE_TO_SPLIT_ON, typename... TYPES, typename... RESULT_TYPES>
  constexpr auto tuple_split(std::tuple<TYPES...> so_far, std::tuple<RESULT_TYPES...> result, std::tuple<>) {
    return std::tuple_cat(so_far, std::make_tuple(result));
  }
  
  template <typename TYPE_TO_SPLIT_ON, typename FIRST, typename... TYPES, typename... TYPES2, typename... RESULT_TYPES>
  constexpr auto tuple_split(std::tuple<TYPES...> so_far, std::tuple<RESULT_TYPES...> result, std::tuple<FIRST, TYPES2...> to_process) {
    if constexpr(std::is_same_v<FIRST, TYPE_TO_SPLIT_ON>) {
      return tuple_split<TYPE_TO_SPLIT_ON>(std::tuple_cat(so_far, std::make_tuple(result)), std::tuple<>{}, peel_front(to_process));
    }
    else {
      return tuple_split<TYPE_TO_SPLIT_ON>(so_far, std::tuple_cat(result, std::make_tuple(std::get<0>(to_process))), peel_front(to_process));
    }
  }
  
  template <typename INTTYPE, INTTYPE... IdxsLeft, INTTYPE... IdxsRight, typename... TYPES>
  constexpr auto tuple_split(std::tuple<TYPES...> tup, std::integer_sequence<INTTYPE, IdxsLeft...>, std::integer_sequence<INTTYPE, IdxsRight...>) {
    std::cout << (( std::to_string(IdxsRight) + " " ) + ...) << std::endl;
    return std::make_tuple(std::make_tuple(std::get<IdxsLeft>(tup)...), std::make_tuple(std::get<IdxsRight + sizeof...(IdxsLeft) + 1>(tup)...));
  }

  template <int FIRST, typename... TYPES>
  constexpr auto tuple_split(std::tuple<TYPES...> tup, tag) {
    const auto& [left, right] = tuple_split(tup, std::make_integer_sequence<int, FIRST - 1>(), std::make_integer_sequence<int, sizeof...(TYPES) - FIRST>());
    return std::tuple_cat(std::make_tuple(left), std::make_tuple(right));
  }
  
  template <int FIRST, int ...REST, typename... TYPES, typename = std::enable_if_t<(sizeof...(REST) > 0)>>
  constexpr auto tuple_split(std::tuple<TYPES...> tup, tag) {
    static_assert(sizeof...(REST) > 0, "Must have at least one element in the parameter pack!");
    const auto& [left, right] = tuple_split(tup, std::make_integer_sequence<int, FIRST - 1>(), std::make_integer_sequence<int, sizeof...(TYPES) - FIRST>());
    return std::tuple_cat(std::make_tuple(left), tuple_split<(REST - FIRST)...>(right, tag{}));
  }
}

namespace std{

template <typename TYPE_TO_SPLIT_ON, typename... TYPES>
constexpr auto tuple_split(std::tuple<TYPES...> tuple) {
  return _detail::tuple_split<TYPE_TO_SPLIT_ON>(std::tuple<>{}, std::tuple<>{}, tuple);
} 
  
template <int ...Idxs, typename... TYPES>
constexpr auto tuple_split(std::tuple<TYPES...> tuple) {
  static_assert((( Idxs >= 0) && ...), "All indexes should be positive!");
  return _detail::tuple_split<Idxs...>(tuple, _detail::tag{});
} 
  
}


template <typename... TYPES, typename INTTYPE, INTTYPE ...Idxs>
inline std::ostream& print_tuple(std::ostream& os, std::tuple<TYPES...> tup, std::integer_sequence<INTTYPE, Idxs...>) {
    os << "std::tuple<";
    int dummy1[] = { ( (os << (Idxs == 0 ? "" : ", ") << typeid(std::get<Idxs>(tup)).name()) ,0)... }; 
    os << ">(";
    int dummy2[] = { ( (os << (Idxs == 0 ? "" : ", ") << std::get<Idxs>(tup)),0)... }; 
    os << ")";
    return os;
}

template <typename... TYPES>
inline std::ostream& operator<<(std::ostream& os, std::tuple<TYPES...> tup) {
    return print_tuple(os, tup, std::make_integer_sequence<int, sizeof...(TYPES)>());
}
Arrays

A gaming company has just built a slots game with 1 line.
Each line is made up of an array object displaying what landed in those slots.

In the code they pass this line into the function isWinner()
This will return an object which supplies a boolean to say if they have won and how many matches in the row there was.

The matches must start from the first entry and accumulate, a winner is only true when there is over 2 or more matches.

Code
Diff
  • function isWinner(lineInfo){
      let matches = 0, numbers = {}, others = {};
      for(let item of lineInfo){
        if(typeof(item) == 'number'){
          if(item in numbers) {
            matches += numbers[item] == 1 ? 2 : 1;
            numbers[item] += 1;
          }
          else
            numbers[item] = 1;
        }
        else{
          if(item in others) {
            matches += others[item] == 1 ? 2 : 1;
            others[item] += 1;
          }
          else
            others[item] = 1;
        }
      }
      return [matches != 0, matches];
    }
  • 11
    function isWinner(lineInfo){
    
    2
      let matchCount = 0;
    
    3
      
    
    4
      //turn it into numbers so it's easier to deal with
    
    5
      lineInfo = lineInfo.map(x => parseInt(x));
    
    6
      
    
    7
      //for each number in the array, step through the rest of the array while
    
    8
      //the numbers match.
    
    9
      for(let i = 0; i<lineInfo.length; i++) {
    
    10
        const number = lineInfo[i];
    
    11
        let currentMatchCount = 1;
    
    12
        let index = i;
    
    13
        
    
    14
        //loop through the remaining items in the array while they match the current number.
    
    15
        //If they don't match, or we reached the end of the array we can bail early.
    
    16
        while(lineInfo[index+1] !== undefined && lineInfo[index+1] === number) {
    
    17
          currentMatchCount++;
    
    18
          index++;
    
    2+
      let matches = 0, numbers = {}, others = {};
    
    3+
      for(let item of lineInfo){
    
    4+
        if(typeof(item) == 'number'){
    
    5+
          if(item in numbers) {
    
    6+
            matches += numbers[item] == 1 ? 2 : 1;
    
    7+
            numbers[item] += 1;
    
    8+
          }
    
    9+
          else
    
    10+
            numbers[item] = 1;
    
    1919
        }
    
    20
        
    
    21
        //we found more matches for this number, lineInfo[i], so update matchCount,
    
    22
        //with the new count, and then fast forward the iterator
    
    23
        //by the number of matches found (slight performance bonus)
    
    24
        if(currentMatchCount > matchCount) {
    
    25
          matchCount = currentMatchCount;
    
    26
          i = i+currentMatchCount-1;
    
    12+
        else{
    
    13+
          if(item in others) {
    
    14+
            matches += others[item] == 1 ? 2 : 1;
    
    15+
            others[item] += 1;
    
    16+
          }
    
    17+
          else
    
    18+
            others[item] = 1;
    
    2727
        }
    
    2828
      }
    
    29
      
    
    30
      return [matchCount > 1, matchCount];
    
    21+
      return [matches != 0, matches];
    
    3131
    }