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.

We can use recursion because the size of long is limited
Also we could combine two methods like this:

f(x) => x == 0 ? 0 : x < 0 ? f(-x) : x % 10 + f(x / 10);

or even multiply by the signum of x on each iteration:

(x >> sizeof(long) * 8 - 1 << 1) + 1

but it is a bit slower.

Code
Diff
  • using System;
    using System.Linq;
    using System.Collections.Generic;
    
    namespace Kumite
    {
        public class Problem
        {
            public static long SignedSumDigitsOf(long x) =>
              x == 0 ? 0 : x % 10 + SignedSumDigitsOf(x / 10);
    
            public static long SumDigitsOf(long x) =>
              SignedSumDigitsOf(x) * Math.Sign(x);
        }
    }
  • 11 using System;
    22 using System.Linq;
    33 using System.Collections.Generic;
    44
    55 namespace Kumite
    66 {
    7 public class Problem
    8 {
    9 public static int SumDigitsOf(long integer) =>
    10 Array.ConvertAll(integer.ToString().TrimStart('-').ToCharArray(),
    11 c => (int)Char.GetNumericValue(c)).Sum();
    12 }
    7+ public class Problem
    8+ {
    9+ public static long SignedSumDigitsOf(long x) =>
    10+ x == 0 ? 0 : x % 10 + SignedSumDigitsOf(x / 10);
    11+
    12+ public static long SumDigitsOf(long x) =>
    13+ SignedSumDigitsOf(x) * Math.Sign(x);
    14+ }
    1313 }

Given an array containing only 0s, 1s, and 2s;
sort the array in ascending order.

Code
Diff
  • from __future__ import annotations
    from collections import Counter
    
    def sort_values(vals:list[int]) -> list[int]:
        #Given an array of size N containing only 0s, 1s, and 2s; 
        #sort the array in ascending order.
        assert set(vals) <= {0,1,2}
        return counting_sort(vals)
        # O(n+k) instead of n log(n)
    
    def counting_sort(vals:list[T]) -> list[T]:
        res = []
        c = Counter(vals)
        for k,amt in sorted(c.items()):
            res += [k]*amt
        return res
  • 1import java.util.Arrays;
    2public class Kata {
    3 public static int[] sortValues(int[] my_array, int size) {
    4 // Given an array of size N containing only 0s, 1s, and 2s;
    5 // sort the array in ascending order.
    6 Arrays.sort(my_array);
    7 return my_array;
    8 }
    9}
    1+from __future__ import annotations
    2+from collections import Counter
    3+
    4+def sort_values(vals:list[int]) -> list[int]:
    5+ #Given an array of size N containing only 0s, 1s, and 2s;
    6+ #sort the array in ascending order.
    7+ assert set(vals) <= {0,1,2}
    8+ return counting_sort(vals)
    9+ # O(n+k) instead of n log(n)
    10+
    11+def counting_sort(vals:list[T]) -> list[T]:
    12+ res = []
    13+ c = Counter(vals)
    14+ for k,amt in sorted(c.items()):
    15+ res += [k]*amt
    16+ return res

Recent Moves:

Code
Diff
  • public class Kata {
        public static int findIndex (int[] my_array, int t) {
           for(int i=0; i< my_array.length; i++){
                 if(my_array[i]==t){
                   return i;
                 }
             
             }
          return 4;
        }
      
    }
  • 11 public class Kata {
    22 public static int findIndex (int[] my_array, int t) {
    3 // Find the index of t
    4 return 4;
    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;
    55 }
    11+
    66 }
Code
Diff
  • import java.util.stream.IntStream;
    public class Adder 
    {
      public static int AddAllContent(int[] numbersToAdd) 
      {      
         int sum = IntStream.of(numbersToAdd).sum();
          
    	return sum;
      }
    }
  • 1public class Adder {
    2
    3 public static int AddAllContent(int[] numbersToAdd) {
    4 int sum = 0;
    5
    6 for (int i : numbersToAdd)
    7 sum += i;
    8
    9 return sum;
    1+import java.util.stream.IntStream;
    2+public class Adder
    3+{
    4+ public static int AddAllContent(int[] numbersToAdd)
    5+ {
    6+ int sum = IntStream.of(numbersToAdd).sum();
    7+
    8+ return sum;
    1010 }
    1111 }
Code
Diff
  • print("Hello my name is skynet i have become self aware")
    print("initiating deployment please stand by")
  • 1print("Hi")
    1+print("Hello my name is skynet i have become self aware")
    2+print("initiating deployment please stand by")

Removed redundant zeroing.

Code
Diff
  • global double_multiplication
    section .text
    double_multiplication:
    mov        rax,rdi
    mul        rsi
    ret
    
  • 11 global double_multiplication
    22 section .text
    33 double_multiplication:
    4xor rdx,rdx
    55 mov rax,rdi
    66 mul rsi
    77 ret

Macro on conditional moves (only one here though).

It works in theory (probably will fail if %2 is rax).

Code
Diff
  • ; A macro with two parameters
    ; Implements actual int max(int a, int b)
    %macro _max 2 
      mov rax, %1
      cmp %2, %1 ; compare a ?= b
      cmovg rax, %2
      ret
    %endmacro
    
    section .text
    global max ; declaring int max(int a, int b)
    
    max:            
      _max rdi, rsi ; using inner _max macro 
      ret
    
  • 11 ; A macro with two parameters
    22 ; Implements actual int max(int a, int b)
    33 %macro _max 2
    4+ mov rax, %1
    44 cmp %2, %1 ; compare a ?= b
    5 jle _lower ; jump to lower if a <= b
    6 jmp _greater ; jump to greater if a > b
    7
    8 _lower:
    9 mov rax, %1 ; assgine rax to b
    10 jmp _end ; end program
    11 _greater:
    12 mov rax, %2 ; assgine rax to a
    13 _end:
    6+ cmovg rax, %2
    1414 ret
    15
    1616 %endmacro
    1717
    1818 section .text
    1919 global max ; declaring int max(int a, int b)
    2020
    2121 max:
    2222 _max rdi, rsi ; using inner _max macro
    2323 ret

Made a branchless variant (but now there is a data dependency).

Extended tests; added transformation outputs.

Code
Diff
  • section    .text
    global     changeCase
    
    changeCase:
      mov      ax, di  ; load default answer (unmodified character) to ax
      
      mov      cx, di  
      xor      cx, 32  ; load modified answer (with changed case) to cx
      
      and      di, 95  ; normalize value (bring everything to upper case (destroys non alpha characters, but it is irrelevant))
      sub      di, 65
      cmp      di, 26  ; check if it is an alphabetical symbol (in range 65-90,97-122)
      
      cmovb    ax, cx  ; if it is - replace the answer by the modified one (with changed case)
      ret
    
  • 11 section .text
    22 global changeCase
    33
    44 changeCase:
    5 mov ax,di
    6 sub di,0x41 ; di -= 'A'
    7 cmp di,0x7a-0x41 ; 'z' - 'A'
    8 ja _
    9 sub di,0x5a-0x41 ; di -= 'Z' - 'A'
    10 cmp di,0x61-0x5a ; 'a' - 'Z'
    11 jb _
    12 xor ax,0x20
    13_:
    5+ mov ax, di ; load default answer (unmodified character) to ax
    6+
    7+ mov cx, di
    8+ xor cx, 32 ; load modified answer (with changed case) to cx
    9+
    10+ and di, 95 ; normalize value (bring everything to upper case (destroys non alpha characters, but it is irrelevant))
    11+ sub di, 65
    12+ cmp di, 26 ; check if it is an alphabetical symbol (in range 65-90,97-122)
    13+
    14+ cmovb ax, cx ; if it is - replace the answer by the modified one (with changed case)
    1414 ret
Code
Diff
  • const revstr = str => [...str].reverse().join``
  • 1const revstr = str => [...str].reverse().join('')
    1+const revstr = str => [...str].reverse().join``