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.
This slight shuffle makes the function more robust when given non-array values. Not sure that's a desirable feature :-/
const flatten = a => Array.isArray(a) ? a.reduce((acc,item) => acc.concat(flatten(item)), []) : a;
const flatten = arr =>arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);- const flatten = a =>
- Array.isArray(a) ? a.reduce((acc,item) => acc.concat(flatten(item)), []) : a;
describe("Test cases", function() { it("[1, 2, [3, 5], [[4, 3], 2]]", function() { Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]); }); it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() { Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]); }); it("not arrays", function() { Test.assertEquals(flatten(123), 123); Test.assertEquals(flatten('abc'), 'abc'); }); });
const flatten = arr =>arr.reduce((acc, item) => acc.concat(Array.isArray(item) ? flatten(item) : item), []);- describe("Test cases", function() {
- it("[1, 2, [3, 5], [[4, 3], 2]]", function() {
- Test.assertSimilar(flatten([1, 2, [3, 5], [[4, 3], 2]]), [1, 2, 3, 5, 4, 3, 2]);
- });
- it("[[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]", function() {
- Test.assertSimilar(flatten([[1, [5], [], [[-3, 'hi']]], 'string', 10, [[[5]]]]), [1, 5, -3, 'hi', 'string', 10, 5]);
- });
- it("not arrays", function() {
- Test.assertEquals(flatten(123), 123);
- Test.assertEquals(flatten('abc'), 'abc');
- });
- });
using System; namespace Solution { class FizzBuzz { public static Func<int, string> convert = (input) => { var output = $"{input % 3 == 0 ? "Fizz" : string.Empty}{input % 5 == 0 ? "Buzz" : string.Empty}"; return !string.IsNullOrWhiteSpace(output) ? output : input.ToString(); }; } }
- using System;
- namespace Solution {
- class FizzBuzz {
public static string convert(int input){var output = (input % 3 == 0 ? "Fizz" : "") + (input % 5 == 0 ? "Buzz" : "");return output == "" ? input.ToString() : output;}- public static Func<int, string> convert = (input)
- => {
- var output = $"{input % 3 == 0 ? "Fizz" : string.Empty}{input % 5 == 0 ? "Buzz" : string.Empty}";
- return !string.IsNullOrWhiteSpace(output) ? output : input.ToString();
- };
- }
- }
changed id to string and check if result of the custom unique-method is correct
the problem now is, that the groovy-unique-method is not that slow as before....
import groovy.transform.EqualsAndHashCode import org.junit.Test import static java.lang.System.currentTimeMillis as now class UniqueTest { @Test void "Check if unique (10.000 items) exceeds 2 seconds"() { def users = createUsers(10000) long a = now() assert Uniquer.unique(users) instanceof List assert now() - a < 2000 } @Test void "Check if result is correct"() { def users = createUsers(10000) def customUniqueUsers = Uniquer.unique(users) println customUniqueUsers.size() def uniqueUsers = users.unique() assert customUniqueUsers.size() == uniqueUsers.size() customUniqueUsers.each { assert customUniqueUsers.contains(it) } } static List<User> createUsers(int times) { List<User> users = [] Random rand = new Random() times.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: new String(a))) } users } } @EqualsAndHashCode class User { String id }
- import groovy.transform.EqualsAndHashCode
- import org.junit.Test
- import static java.lang.System.currentTimeMillis as now
- class UniqueTest {
- @Test
void "Check if unique exceeds 2 seconds"() {List<User> users = []Random rand = new Random()10000.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: a)) }println "x"long a = now()assert Uniquer.unique(users) instanceof Listassert now() - a < 2000}- void "Check if unique (10.000 items) exceeds 2 seconds"() {
- def users = createUsers(10000)
- long a = now()
- assert Uniquer.unique(users) instanceof List
- assert now() - a < 2000
- }
- @Test
- void "Check if result is correct"() {
- def users = createUsers(10000)
- def customUniqueUsers = Uniquer.unique(users)
- println customUniqueUsers.size()
- def uniqueUsers = users.unique()
- assert customUniqueUsers.size() == uniqueUsers.size()
- customUniqueUsers.each {
- assert customUniqueUsers.contains(it)
- }
- }
- static List<User> createUsers(int times) {
- List<User> users = []
- Random rand = new Random()
- times.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: new String(a))) }
- users
- }
- }
- @EqualsAndHashCode
- class User {
byte[] idboolean equals(User user) {user.id == id}int hashCode() {((int) id[0]) << 8 | id[1]}- String id
- }
This regular expression is a little lack luster. I think it could be improved to account for hyphens and apostraphes that are supposed to be apart of a word. The objective here is to not accidentally capture punctuation mistakes.
wordCount=s=>s.trim().match(/[A-Za-z0-9]+/mg).length;
wordCount=s=>s.trim().split(' ').length- wordCount=s=>s.trim().match(/[A-Za-z0-9]+/mg).length;
// TODO: Replace examples and use TDD development by writing your own tests // These are some CW specific test methods available: // Test.expect(boolean, [optional] message) // Test.assertEquals(actual, expected, [optional] message) // Test.assertSimilar(actual, expected, [optional] message) // Test.assertNotEquals(actual, expected, [optional] message) // NodeJS assert is also automatically required for you. // assert(true) // assert.strictEqual({a: 1}, {a: 1}) // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]}) // You can also use Chai (http://chaijs.com/) by requiring it yourself // var expect = require("chai").expect; // var assert = require("chai").assert; // require("chai").should(); Test.assertEquals(wordCount(" v"), 1); Test.assertEquals(wordCount("Word"), 1); Test.assertEquals(wordCount("Wo r d"), 3); Test.assertEquals(wordCount("Bob the t-rex"), 4); Test.assertEquals(wordCount("Bob the t-rex . Has Typo period."), 7);
- // TODO: Replace examples and use TDD development by writing your own tests
- // These are some CW specific test methods available:
- // Test.expect(boolean, [optional] message)
- // Test.assertEquals(actual, expected, [optional] message)
- // Test.assertSimilar(actual, expected, [optional] message)
- // Test.assertNotEquals(actual, expected, [optional] message)
- // NodeJS assert is also automatically required for you.
- // assert(true)
- // assert.strictEqual({a: 1}, {a: 1})
- // assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
- // You can also use Chai (http://chaijs.com/) by requiring it yourself
- // var expect = require("chai").expect;
- // var assert = require("chai").assert;
- // require("chai").should();
- Test.assertEquals(wordCount(" v"), 1);
- Test.assertEquals(wordCount("Word"), 1);
- Test.assertEquals(wordCount("Wo r d"), 3);
Test.assertEquals(wordCount("Bob the t-rex"), 3);- Test.assertEquals(wordCount("Bob the t-rex"), 4);
- Test.assertEquals(wordCount("Bob the t-rex . Has Typo period."), 7);
const getDividors = n => { let result = []; for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) } return result; // output array won't be sorted }
const getDividors = (n, result = []) => {- const getDividors = n => {
- let result = [];
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)- for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
- if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
- return result; // output array won't be sorted
- }
const getDividors = n => { let result = []; for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) } return result.sort((a,b) => a - b); } describe("Test cases", function() { it("n = 200", function() { Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]); }); it("n = 560", function() { Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]); }); it("n = 755", function() { Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]); }); });
const getDividors = (n, result = []) => {- const getDividors = n => {
- let result = [];
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)- for (let i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
- if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
- return result.sort((a,b) => a - b);
- }
- describe("Test cases", function() {
- it("n = 200", function() {
- Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
- });
- it("n = 560", function() {
- Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
- });
- it("n = 755", function() {
- Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]);
- });
- });
Now it's even more prettier
import java.util.*; class Node { int value; Node left; Node right; public Node(int value) { this.value = value; } } class BST { public static boolean search(Node root, int key) { if (root == null) return false; if (root.value == key) return true; return root.value > key ? search(root.left, key) : search(root.right, key); } }
- import java.util.*;
- class Node {
- int value;
- Node left;
- Node right;
- public Node(int value) {
- this.value = value;
- }
- }
- class BST {
- public static boolean search(Node root, int key) {
if (root == null){return false;}else if(root.value == key) {- if (root == null)
- return false;
- if (root.value == key)
- return true;
} else{return root.value > key ? search(root.left, key) : search(root.right, key);}- return root.value > key ? search(root.left, key) : search(root.right, key);
- }
- }