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.

# 90 degrees clockwise matrix rotate
def rotate(m):
    return list(zip(*m))[::-1]
Strings
Fundamentals
Basic Language Features
Algorithms

Obligatory Python golf solution. Don't really like the final version I ended up on.
I don't know APL, but this problem almost felt designed for it. The shortest solution I could think of was {s r c←⍵⋄k←+/q←c⍷s⋄(({⍵'0'[' '=⍵]}¨(-k)↑⍕r)@(k↑⍒q))s}, I'm sure it can be greatly improved by somebody who knows what they're doing though. (index origin is 0 btw

Code
Diff
  • # exponential time - fast enough when first testcase is commented out
    f=lambda s,r,c:s and(f(z:=s[:-1],r,c)+s[-1],f(z,r//10,c)+str(r%10))[c==s[-1]]
    # linear recursive
    f=lambda s,r,c:s and(c!=s[-1]and f(s[:-1],r,c)+s[-1]or f(s[:-1],r//10,c)+str(r%10))
    
    f=lambda s,r,c:"".join(x==c and[str(r%10),r:=r//10][0]or x for x in s[::-1])[::-1]
    
  • 1
    using System;
    
    2
    using System.Linq;
    
    3
    using System.Collections.Generic;
    
    1+
    # exponential time - fast enough when first testcase is commented out
    
    2+
    f=lambda s,r,c:s and(f(z:=s[:-1],r,c)+s[-1],f(z,r//10,c)+str(r%10))[c==s[-1]]
    
    3+
    # linear recursive
    
    4+
    f=lambda s,r,c:s and(c!=s[-1]and f(s[:-1],r,c)+s[-1]or f(s[:-1],r//10,c)+str(r%10))
    
    44
    5
    public class Test{
    
    6
      static void Main(){
    
    7
        Console.WriteLine(Replacer.sValueReplacer("0000##81#059671####=1811", 3, '#'));
    
    8
      }
    
    9
    }
    
    10
    11
    public class Replacer{
    
    12
    13
      public static string sValueReplacer(string source, int index, char char_to_replace)
    
    14
      {
    
    15
        string res = source;
    
    16
    17
        try
    
    18
        {
    
    19
          if (!string.IsNullOrEmpty(char_to_replace.ToString()))
    
    20
          {
    
    21
            if (res.Contains(char_to_replace.ToString()))
    
    22
            {
    
    23
               // Get ALL Indexes position of character
    
    24
               var Indexes = GetIndexes(res, char_to_replace.ToString());
    
    25
    26
               int max = GetMaxValue(Indexes.Count);
    
    27
    28
               while (index >= max) 
    
    29
               { 
    
    30
                index -= max; 
    
    31
               }
    
    32
    33
               var new_value = index.ToString().PadLeft(Indexes.Count, '0');
    
    34
    35
               for (int i = 0; i < Indexes.Count; i++)
    
    36
               {
    
    37
                res = res.Remove(Indexes[i], 1).Insert(Indexes[i], new_value[i].ToString());
    
    38
               }
    
    39
            }
    
    40
          }
    
    41
         }
    
    42
         catch (Exception)
    
    43
         {
    
    44
          res = source;
    
    45
         }
    
    46
    47
         return res;
    
    48
      }
    
    49
      
    
    50
      private static List<int> GetIndexes(string mainString, string toFind)
    
    51
      {
    
    52
        var Indexes = new List<int>();
    
    53
    54
        for (int i = mainString.IndexOf(toFind); i > -1; i = mainString.IndexOf(toFind, i + 1))
    
    55
        {
    
    56
          // for loop end when i=-1 (line.counter not found)
    
    57
          Indexes.Add(i);
    
    58
        }
    
    59
    60
        return Indexes;
    
    61
       }
    
    62
    63
       private static int GetMaxValue(int numIndexes)
    
    64
       {
    
    65
          int max = 0;
    
    66
    67
          for (int i = 0; i < numIndexes; i++)
    
    68
          {
    
    69
            if (i == 0)
    
    70
              max = 9;
    
    71
            else
    
    72
              max = max * 10 + 9;
    
    73
          }
    
    74
    75
          return max;
    
    76
        }
    
    77
    }
    
    6+
    f=lambda s,r,c:"".join(x==c and[str(r%10),r:=r//10][0]or x for x in s[::-1])[::-1]
    

Recent Moves:

Code
Diff
  • const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
  • 1
    function factorial (n) {
    
    2
      if (n <= 1) {
    
    3
        return 1;
    
    4
      } else {
    
    5
        return n * factorial(n - 1);
    
    6
      }
    
    7
    }
    
    1+
    const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
    

Recent Moves:

div_t is an structure and cause more memory usage you can use % and / operators instead.also you can use while(1) with break for this code.

Code
Diff
  • #include <stdlib.h>
    #include <math.h>
    
    int reverse_int(int int_reverse) {
      int o = 0;
      while (1) {
        o += ((int)int_reverse % 10);
        if (int_reverse /= 10) 
          o *= 10; 
        else break;
      }
      return o;
    }
  • 11
    #include <stdlib.h>
    
    2+
    #include <math.h>
    
    22
    33
    int reverse_int(int int_reverse) {
    
    4
        div_t div_x = div(int_reverse, 10);
    
    5
        int reverse_int = div_x.rem;
    
    6
        while (div_x.quot) {
    
    7
            div_x = div(div_x.quot, 10);
    
    8
            reverse_int *= 10;
    
    9
            reverse_int += div_x.rem;
    
    10
        }
    
    11
        return reverse_int;
    
    5+
      int o = 0;
    
    6+
      while (1) {
    
    7+
        o += ((int)int_reverse % 10);
    
    8+
        if (int_reverse /= 10) 
    
    9+
          o *= 10; 
    
    10+
        else break;
    
    11+
      }
    
    12+
      return o;
    
    1212
    }
    
Mathematics
Algorithms
Numbers

Floyd's

Code
Diff
  • let is_happy h =
      let rec succ n = 
        match n with
        | 0 -> 0
        | _ -> let n' = n mod 10 in n' * n' + succ (n / 10)
      in
      let rec floyd's tortoise hare =
        match hare |> succ |> succ with
        | 1 -> true
        | hare' -> 
            let tortoise' = succ tortoise in 
            tortoise' <> hare' && floyd's tortoise' hare'
      in
      floyd's h h
    ;;
  • 1
    def is_happy(h: int) -> bool:
    
    2
        """Returns `True` if `h` is happy, `False` otherwise."""
    
    3
        seen = set()
    
    4
        while h > 1 and h not in seen:
    
    5
            seen.add(h)
    
    6
            tot = 0
    
    7
            while h > 0:
    
    8
                tot += pow(h % 10, 2)
    
    9
                h //= 10
    
    10
            h = tot
    
    11
        return h == 1
    
    1+
    let is_happy h =
    
    2+
      let rec succ n = 
    
    3+
        match n with
    
    4+
        | 0 -> 0
    
    5+
        | _ -> let n' = n mod 10 in n' * n' + succ (n / 10)
    
    6+
      in
    
    7+
      let rec floyd's tortoise hare =
    
    8+
        match hare |> succ |> succ with
    
    9+
        | 1 -> true
    
    10+
        | hare' -> 
    
    11+
            let tortoise' = succ tortoise in 
    
    12+
            tortoise' <> hare' && floyd's tortoise' hare'
    
    13+
      in
    
    14+
      floyd's h h
    
    15+
    ;;
    

Recent Moves: