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.
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;
- }
- }
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; public class HappyTest { @Test void sample() { assertFalse(Kata.isHappy(3)); assertFalse(Kata.isHappy(4)); assertTrue(Kata.isHappy(7)); assertTrue(Kata.isHappy(19)); assertTrue(Kata.isHappy(103)); assertTrue(Kata.isHappy(487)); assertTrue(Kata.isHappy(1663)); assertFalse(Kata.isHappy(1665)); assertTrue(Kata.isHappy(1000000000)); assertFalse(Kata.isHappy(10000000001L)); } }
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- import static org.junit.jupiter.api.Assertions.assertFalse;
- import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO: Replace examples and use TDD by writing your own tests- import org.junit.jupiter.api.Test;
public class SolutionTest {@Testpublic void testSomething() {assertEquals(Kata.isHappy(3), false);assertEquals(Kata.isHappy(4), false);assertEquals(Kata.isHappy(7), true);assertEquals(Kata.isHappy(19), true);assertEquals(Kata.isHappy(103), true);assertEquals(Kata.isHappy(487), true);assertEquals(Kata.isHappy(1663), true);assertEquals(Kata.isHappy(1665), false);assertEquals(Kata.isHappy(1000000000), true);assertEquals(Kata.isHappy(10000000001l), false);}- public class HappyTest {
- @Test
- void sample() {
- assertFalse(Kata.isHappy(3));
- assertFalse(Kata.isHappy(4));
- assertTrue(Kata.isHappy(7));
- assertTrue(Kata.isHappy(19));
- assertTrue(Kata.isHappy(103));
- assertTrue(Kata.isHappy(487));
- assertTrue(Kata.isHappy(1663));
- assertFalse(Kata.isHappy(1665));
- assertTrue(Kata.isHappy(1000000000));
- assertFalse(Kata.isHappy(10000000001L));
- }
- }
#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 aschar
s) -
char
-> a number corresponding to ascii code -
nil
-> an empty string:\ _ -> false
-
cons
-> add achar
to astring
:\ char string b1 . b1 true (\ b2 . b2 char string)
Fixed for existing bugs. Now with number literals.
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
# numberszero = falsesucc = \ 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 thirtytwofortyfour = succ (add ten thirtythree)seventytwo = mul (succ ( succ (add three three))) (mul three three)hundred = mul ten tenhundredone = succ hundredhundredeight = succ (add three (add three hundredone))hundredeleven = add three hundredeighthundredforteen = add three hundredelevenhundrednineteen = 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 thirtythreenil ))))))))))))- # 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 chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold = 0; // const LC = require("LC"); const LC = { compile: () => compile(code), config: options } // Temporary. Would normally import, see line above. LC.config.purity = "Let"; LC.config.numEncoding = "Church"; const solution = LC.compile().hello; const T = new L('a', new L('b', new V('a'))); const F = new L('a', new L('b', new V('b'))); function _toString(term, res="") { if (term(T)(true)(false)) { const n = toInt(term(F)(T)); return _toString(term(F)(F), res+String.fromCharCode(n)); } else return res; } describe("Full tests", function() { it("Does it work?", function() { assert.equal(_toString(solution), "Hello, world!") }); });
- const chai = require("chai");
- const assert = chai.assert;
- chai.config.truncateThreshold = 0;
- // const LC = require("LC");
- const LC = { compile: () => compile(code), config: options } // Temporary. Would normally import, see line above.
LC.config.purity = "LetRec";- LC.config.purity = "Let";
- LC.config.numEncoding = "Church";
- const solution = LC.compile().hello;
- const T = new L('a', new L('b', new V('a')));
- const F = new L('a', new L('b', new V('b')));
- function _toString(term, res="") {
- if (term(T)(true)(false)) {
- const n = toInt(term(F)(T));
- return _toString(term(F)(F), res+String.fromCharCode(n));
- } else return res;
- }
- describe("Full tests", function() {
- it("Does it work?", function() {
- assert.equal(_toString(solution), "Hello, world!")
- });
- });
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.
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 0let i = fact = 1- "use strict";
- if (n < 0) {
- return NaN
- }
- let i, fact
- i = fact = 1
- while (i <= n) {
fact *= ii++- fact *= i
- i++
- }
- return fact
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("should test for something", function() { assert(factorial(0), 1); assert(factorial(1), 1); assert(factorial(5), 120); assert(factorial(8),40320); assert(factorial(15),1307674368000); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("should test for something", function() {
- assert(factorial(0), 1);
- assert(factorial(1), 1);
- assert(factorial(5), 120);
- assert(factorial(8),40320);
- assert(factorial(15),1307674368000);
- });
- });
// See https://pub.dartlang.org/packages/test import "package:test/test.dart"; import "package:solution/solution.dart"; import "dart:math"; void main() { group("Tests", () { test('1+1=', ()=> expect(addition(1, 1), equals(2))); test('2+0=', ()=> expect(addition(2, 0), equals(2))); test('123+321=', ()=> expect(addition(123, 321), equals(444))); Random r = Random(); int a = r.nextInt(1000); int b = r.nextInt(1000); test('Random', ()=> expect(addition(a, b), equals(a+b))); }); }
- // See https://pub.dartlang.org/packages/test
- import "package:test/test.dart";
- import "package:solution/solution.dart";
- import "dart:math";
- void main() {
- group("Tests", () {
- test('1+1=', ()=> expect(addition(1, 1), equals(2)));
- test('2+0=', ()=> expect(addition(2, 0), equals(2)));
- test('123+321=', ()=> expect(addition(123, 321), equals(444)));
- Random r = Random();
- int a = r.nextInt(1000);
- int b = r.nextInt(1000);
- test('Random', ()=> expect(addition(a, b), equals(a+b)));
- });
- }