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
  • function Palindrome(str){
      // Case insensitive palindrome
      for (let i = 0, j = str.length - 1; i < j; i++, j--) {
        if (str[i].toLowerCase() !== str[j].toLowerCase()) return false;
      }
      return true;
    }
  • 11
    function Palindrome(str){
    
    22
      // Case insensitive palindrome
    
    3
      str = str.toUpperCase();
    
    4
      return str === str.split('').reverse().join('');
    
    3+
      for (let i = 0, j = str.length - 1; i < j; i++, j--) {
    
    4+
        if (str[i].toLowerCase() !== str[j].toLowerCase()) return false;
    
    5+
      }
    
    6+
      return true;
    
    55
    }
    
Code
Diff
  • module IndexOf
    
    import Data.List
    
    %access export
    
    total
    elemIndex' : DecEq a => (x : a) -> (ys : List a) -> {auto ok : Elem x ys} -> Nat
    elemIndex' x (x :: ys) {ok = Here} = 0
    elemIndex' x (y :: ys) {ok = There _} with (decEq x y)
                                             | Yes _ = 0
                                             | No _ = S (elemIndex' x ys)
    
    -- partial
    -- elemIndex' : (x : Int) -> (ys : List Int) -> Nat
    -- elemIndex' x ys =
    --   case elemIndex x ys of
    --     Just n => n
    
  • 11
    module IndexOf
    
    22
    33
    import Data.List
    
    44
    55
    %access export
    
    66
    77
    total
    
    8
    elemIndex' : (x : Int) -> (ys : List Int) -> {auto ok : Elem x ys} -> Nat
    
    9
    elemIndex' x (z :: zs) {ok} with (decEq x z)
    
    10
                                   | Yes _ = 0
    
    11
                                   | No _ with (ok)
    
    12
                                             | Here impossible
    
    13
                                             | There _ = S (elemIndex' x zs)
    
    8+
    elemIndex' : DecEq a => (x : a) -> (ys : List a) -> {auto ok : Elem x ys} -> Nat
    
    9+
    elemIndex' x (x :: ys) {ok = Here} = 0
    
    10+
    elemIndex' x (y :: ys) {ok = There _} with (decEq x y)
    
    11+
                                             | Yes _ = 0
    
    12+
                                             | No _ = S (elemIndex' x ys)
    
    1414
    1515
    -- partial
    
    1616
    -- elemIndex' : (x : Int) -> (ys : List Int) -> Nat
    
    1717
    -- elemIndex' x ys =
    
    1818
    --   case elemIndex x ys of
    
    1919
    --     Just n => n
    
Code
Diff
  • countlegs=lambda c,s,h:(c+s)*4+h*2
  • 1
    def countlegs(cow,sheep,chick):
    
    2
        return cow*4 + sheep*4 +chick*2
    
    1+
    countlegs=lambda c,s,h:(c+s)*4+h*2
    

Find Pythagorean triples with Euclid's Formula. The formula is - with a>b, find a squared + b squared, a squared - b squared, and 2 x a x b. Give the result as the the three answers in a list sorted from smallest to biggest

def find_triple(a, b):
    ans1 = a**2 + b**2
    ans2 = a**2 - b**2
    ans3 = 2 * a * b
    triple = [ans1, ans2, ans3]
    triple.sort()
    return triple

This simple function tells you how many bits of precision is long double in your environment.

Introduction:

Floating-point numbers on most platforms are encoded using IEEE 754.

  • float is usually 32-bit IEEE 754 (a.k.a. single precision).
  • double is usually 64-bit IEEE 754 (a.k.a. double precision).

But what about long double?

Well, the precision of long double strongly depends on the platform and compiler. It usually is 64-bit, 80-bit, or 128-bit.

The function in this kumite, i.e. get_long_double_precision(), will return the number of bits of precision of long double.

For more info

https://en.wikipedia.org/wiki/Long_double

#include <stdlib.h> // size_t
#include <stdint.h> // uint8_t, uint16_t

uint16_t get_long_double_precision(void);
uint8_t is_big_endian(void);

uint16_t get_long_double_precision(void) {
  size_t ldsize = sizeof(long double);
  uint8_t bigendian = is_big_endian();
  
  long double test = -0.0L;
  uint8_t* test_bytes = (uint8_t*) &test;
  
  uint16_t res = 0;
  
  for (size_t i = 0; i < ldsize; i += 1) {
    size_t offset = (bigendian == 0) ? i : (ldsize - 1 - i);
    res += 8;
    if (test_bytes[offset] == 0x80) {
      break;
    }
  }
  
  return res;
}


// utility function: returns 0x01 in case the current platform is big endian,
//   otherwise returns 0x00
uint8_t is_big_endian(void) {
  uint16_t test = 0x0100;
  return ((uint8_t*) &test)[0];
}