Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad
Code
Diff
  • revstr=s=>s.split``.reverse().join``
    • revstr = str => str.split('').reverse().join('');
    • revstr=s=>s.split``.reverse().join``
Mathematics
Algorithms
Logic
Numbers
Data Types
Code
Diff
  • import static java.util.stream.LongStream.iterate;
    
    import java.util.HashSet;
    
    interface Kata {
      static boolean isHappy(long h) {
        var seen = new HashSet<>();
        while (seen.add(h)) {
          h = iterate(h, i -> i > 0, i -> i / 10).map(i -> (int) Math.pow(i % 10, 2)).sum();
        }
        return h == 1;
      }
    }
    
    • public class Kata {
    • private static Object[] root = {null,
    • new Object[][]{null, null, null, null, null, null, null, null, null, null, {new Boolean(true)}},
    • null,
    • null,
    • null,
    • null,
    • null,
    • null,
    • null,
    • null,
    • new Object[]{new Boolean(false)}
    • };
    • public static boolean isHappy(long current) {
    • Boolean[] result = {false};
    • outer:
    • for (; ; ) {
    • long next = 0;
    • Object[] node = root;
    • while (current > 0) {
    • int remainder = (int) (current % 10);
    • current /= 10;
    • next += remainder * remainder;
    • if (node[remainder] == null) {
    • node = (Object[]) (node[remainder] = new Object[remainder == 0 ? 10 : 11]);
    • while (current > 0) {
    • remainder = (int) (current % 10);
    • current /= 10;
    • next += remainder * remainder;
    • node = (Object[]) (node[remainder] = new Object[remainder == 0 ? 10 : 11]);
    • }
    • node[10] = result;
    • current = next;
    • continue outer;
    • }
    • node = (Object[]) node[remainder];
    • }
    • if (node[10] != null) {
    • return result[0] = (Boolean) ((Object[]) node[10])[0];
    • }
    • node[10] = result;
    • current = next;
    • import static java.util.stream.LongStream.iterate;
    • import java.util.HashSet;
    • interface Kata {
    • static boolean isHappy(long h) {
    • var seen = new HashSet<>();
    • while (seen.add(h)) {
    • h = iterate(h, i -> i > 0, i -> i / 10).map(i -> (int) Math.pow(i % 10, 2)).sum();
    • }
    • return h == 1;
    • }
    • }
Numbers
Data Types
Integers
Algorithms
Logic
Code
Diff
  • #include <math.h>
    #include <stdint.h>
    
    uint8_t number_of_digits(uint64_t n)
    {
        return log10(n) + 1;
    }
    • #include <stdlib.h>
    • #include <math.h>
    • #include <stdint.h>
    • uint_fast8_t number_of_digits(uint64_t n)
    • uint8_t number_of_digits(uint64_t n)
    • {
    • size_t x;
    • printf("%lu%n", n, &x);
    • return x;
    • return log10(n) + 1;
    • }

Using the string encoding described below, define a term hello which is equivalent to "Hello, world!"

Encodings

  • purity -> Let
  • true, false -> Church encoding
  • Number -> Church encoding (only used as chars)
  • char -> a number corresponding to ascii code
  • nil -> an empty string: \ _ -> false
  • cons -> add a char to a string: \ char string b1 . b1 true (\ b2 . b2 char string)

Fixed for existing bugs. Now with number literals.

Code
Diff
  • const code = String.raw`
    # Preloaded
    
    true = \ a b . a
    false = \ a b . b
    
    pair = \ a b c . c a b
    nil = \ _ . false
    cons = \ c s . pair true (pair c s)
    
    # Code
    hello = cons 72
          ( cons 101
          ( cons 108
          ( cons 108
          ( cons 111
          ( cons 44
          ( cons 32
          ( cons 119
          ( cons 111
          ( cons 114
          ( cons 108
          ( cons 100
          ( cons 33
                 nil
          ))))))))))))
    `
    • const code = String.raw`
    • # bools
    • # Preloaded
    • true = \ a b . a
    • false = \ a b . b
    • # numbers
    • zero = false
    • succ = \ n f x . f (n f x)
    • add = \ a b f x . a f (b f x)
    • mul = \ a b f . a (b f)
    • three = \ f x . f ( f ( f x ))
    • ten = succ (mul three three)
    • thirtytwo = succ (succ (mul ten three))
    • thirtythree = succ thirtytwo
    • fortyfour = succ (add ten thirtythree)
    • seventytwo = mul (succ ( succ (add three three))) (mul three three)
    • hundred = mul ten ten
    • hundredone = succ hundred
    • hundredeight = succ (add three (add three hundredone))
    • hundredeleven = add three hundredeight
    • hundredforteen = add three hundredeleven
    • hundrednineteen = succ (succ (add three hundredforteen))
    • # data
    • pair = \ a b c . c a b
    • nil = \ _ . false
    • cons = \ c s . pair true (pair c s)
    • # [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
    • hello = cons seventytwo
    • (cons hundredone
    • (cons hundredeight
    • (cons hundredeight
    • (cons hundredeleven
    • (cons fortyfour
    • (cons thirtytwo
    • (cons hundrednineteen
    • (cons hundredeleven
    • (cons hundredforteen
    • (cons hundredeight
    • (cons hundred
    • (cons thirtythree
    • nil ))))))))))))
    • # Code
    • hello = cons 72
    • ( cons 101
    • ( cons 108
    • ( cons 108
    • ( cons 111
    • ( cons 44
    • ( cons 32
    • ( cons 119
    • ( cons 111
    • ( cons 114
    • ( cons 108
    • ( cons 100
    • ( cons 33
    • nil
    • ))))))))))))
    • `

Be careful! You were previously implicitly defining a global fact when you really should not have done that. A lot of these problems can be solved by including that "use strict"; at the start of a function, which is especially important when you're starting out.
Also, factorial of 0 is 1, and factorial of negatives is undefined. It's also nice to have proper indentation to make programs a little easier to read.

Code
Diff
  • function factorial (n) {
      "use strict";
      if (n < 0) {
        return NaN
      }
      let i, fact
      i = fact = 1
      while (i <= n) { 
        fact *= i
        i++
      }
      return fact
    }
    • function factorial (n) {
    • if (n <= 0) return 0
    • let i = fact = 1
    • "use strict";
    • if (n < 0) {
    • return NaN
    • }
    • let i, fact
    • i = fact = 1
    • while (i <= n) {
    • fact *= i
    • i++
    • fact *= i
    • i++
    • }
    • return fact
    • }