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.
Make function negative-safe
def fibonacci(x): if x < 0: return None if x == 0 or x == 1: return x return fibonacci(x - 1) + fibonacci(x - 2)
- def fibonacci(x):
- if x < 0:
- return None
- if x == 0 or x == 1:
- return x
else :return fibonacci(x-1) + fibonacci(x-2)- return fibonacci(x - 1) + fibonacci(x - 2)
Test.describe('fibonacci(n)') Test.it('should calculate properly') test.assert_equals(fibonacci(8), 21) Test.it('should fail if n is negative') test.assert_equals(fibonacci(-1), None)
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)test.assert_equals(fibonacci(8),21 )# test.assert_not_equals(actual, expected, [optional] message)- Test.describe('fibonacci(n)')
- Test.it('should calculate properly')
- test.assert_equals(fibonacci(8), 21)
# You can use Test.describe and Test.it to write BDD style test groupings- Test.it('should fail if n is negative')
- test.assert_equals(fibonacci(-1), None)
The original solution did not cover for empty string.
Also, you can remove completly the sum part from the reduce as to make it simpler to read.
const sum = (a,b) => a+b; const getSum = array => array.reduce(sum,0);
const getSum = array => array.reduce((acc, i) => acc + i)- const sum = (a,b) => a+b;
- const getSum = array => array.reduce(sum,0);
Test.assertEquals(getSum([5,9,4,1]), 19, "looks like its wrong."); Test.assertEquals(getSum([4,0,4,50,8]), 66, "looks like its wrong."); Test.assertEquals(getSum([5,9,4,1,9,1,1,1,4,10]), 45, "looks like its wrong."); Test.assertEquals(getSum([]),0, "Error on empty array");
- Test.assertEquals(getSum([5,9,4,1]), 19, "looks like its wrong.");
- Test.assertEquals(getSum([4,0,4,50,8]), 66, "looks like its wrong.");
- Test.assertEquals(getSum([5,9,4,1,9,1,1,1,4,10]), 45, "looks like its wrong.");
- Test.assertEquals(getSum([]),0, "Error on empty array");
Just another way... A ternary way.
public class BiggerNum{ /** * @param a integer of param1 * @param b integer of param2 * @return the bigger integer of a and b * If a equals b, return either one */ public static int compare(int a, int b) { return a > b ? a : a < b ? b : a; } }
- public class BiggerNum{
- /**
- * @param a integer of param1
- * @param b integer of param2
- * @return the bigger integer of a and b
- * If a equals b, return either one
- */
- public static int compare(int a, int b) {
return Math.max(a,b);- return a > b ? a : a < b ? b : a;
- }
- }
def decrypt(message): decode = '' modifier = -1 shift = int((len(message) -1) / 2) for i in range(len(message)): shift += i * modifier decode += message[shift] modifier *= -1 return decode
import math- def decrypt(message):
# JUST DO ITreturn message- decode = ''
- modifier = -1
- shift = int((len(message) -1) / 2)
- for i in range(len(message)):
- shift += i * modifier
- decode += message[shift]
- modifier *= -1
- return decode
import random def pi_estimate(n, seed=0): random.seed(seed) n_inside = 0 for i in range(n): if random.random()**2 + random.random()**2 < 1: n_inside += 1 return 4 * (n_inside / n)
- import random
def PiEstimate(runs):random.seed(0)inner = 0- def pi_estimate(n, seed=0):
- random.seed(seed)
- n_inside = 0
- for i in range(n):
- if random.random()**2 + random.random()**2 < 1:
inner += 1return 4 * (inner / n)- n_inside += 1
- return 4 * (n_inside / n)
# TODO: Replace examples and use TDD development by writing your own tests # These are some of the methods available: # test.expect(boolean, [optional] message) # test.assert_equals(100000, 3.14844) # test.assert_equals(1000000, 3.14244) test.assert_equals(pi_estimate(1000, seed=42), 3.128) test.assert_equals(pi_estimate(1000000, seed=42), 3.140592) # You can use Test.describe and Test.it to write BDD style test groupings
- # TODO: Replace examples and use TDD development by writing your own tests
- # These are some of the methods available:
- # test.expect(boolean, [optional] message)
- # test.assert_equals(100000, 3.14844)
- # test.assert_equals(1000000, 3.14244)
test.assert_not_equals(2000000, 3.141634)- test.assert_equals(pi_estimate(1000, seed=42), 3.128)
- test.assert_equals(pi_estimate(1000000, seed=42), 3.140592)
- # You can use Test.describe and Test.it to write BDD style test groupings
Just added some additional classes and functions to help make it modular and so others can build ontop of it.
using System; using System.Collections.Generic; using System.Linq; class Kata { public static string Main(string language) { var salutations = new DeveloperGreeting().GreetMyDeveloper(language); Console.Write(salutations); return salutations; } } //Returns the developer greeting message. class DeveloperGreeting { public string GreetMyDeveloper(string language){ return new GreetingRepository().GetDeveloperGreetings(language).message; } } //Get greeting data from either a collection or database store. class GreetingRepository { public List<Greeting> greetings; public GreetingRepository() { greetings = new List<Greeting>(){ new Greeting(){language = "C#", message = "Hello my fellow C# developers, work hard."}, new Greeting() {language = "Java", message = "Hello my fellow Java developers, work hard."} }; } public Greeting GetDeveloperGreetings(string language){ var g = greetings.Where(x => x.language.Equals(language)).ToList(); return new Greeting(){ language = g[0].language, message = g[0].message}; } } //Greeting Model class class Greeting{ public string language {get; set;} public string message {get; set;} }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Kata {
public static string Main(string greeting, string language) {Console.Write("{0}, {1}!\n", greeting, language);return greeting + ", " + language + "!";- public static string Main(string language) {
- var salutations = new DeveloperGreeting().GreetMyDeveloper(language);
- Console.Write(salutations);
- return salutations;
- }
}- }
- //Returns the developer greeting message.
- class DeveloperGreeting {
- public string GreetMyDeveloper(string language){
- return new GreetingRepository().GetDeveloperGreetings(language).message;
- }
- }
- //Get greeting data from either a collection or database store.
- class GreetingRepository {
- public List<Greeting> greetings;
- public GreetingRepository() {
- greetings = new List<Greeting>(){
- new Greeting(){language = "C#", message = "Hello my fellow C# developers, work hard."},
- new Greeting() {language = "Java", message = "Hello my fellow Java developers, work hard."}
- };
- }
- public Greeting GetDeveloperGreetings(string language){
- var g = greetings.Where(x => x.language.Equals(language)).ToList();
- return new Greeting(){ language = g[0].language, message = g[0].message};
- }
- }
- //Greeting Model class
- class Greeting{
- public string language {get; set;}
- public string message {get; set;}
- }
using NUnit.Framework; using System; using System.IO; [TestFixture] public class KataTests { [Test] public void Prints_expected_output() { string language = "C#"; var originalConsoleOut = Console.Out; // preserve the original stream var writer = new StringWriter(); Console.SetOut(writer); //ready to listen to console Kata.Main(language); writer.Flush(); // when you're done, make sure everything is written out var myString = writer.GetStringBuilder().ToString(); Console.SetOut(originalConsoleOut); // restore Console output Assert.AreEqual("Hello my fellow C# developers, work hard.", myString); } }
- using NUnit.Framework;
- using System;
- using System.IO;
- [TestFixture]
- public class KataTests
- {
- [Test]
- public void Prints_expected_output()
- {
- string language = "C#";
string greeting = "Hello";- var originalConsoleOut = Console.Out; // preserve the original stream
- var writer = new StringWriter();
- Console.SetOut(writer);
- //ready to listen to console
Kata.Main(greeting, language);- Kata.Main(language);
- writer.Flush(); // when you're done, make sure everything is written out
- var myString = writer.GetStringBuilder().ToString();
- Console.SetOut(originalConsoleOut); // restore Console output
Assert.AreEqual(greeting + ", " + language + "!\n" , myString);- Assert.AreEqual("Hello my fellow C# developers, work hard.", myString);
- }
- }
One line :)
const validate = str => !(acc = 0) && str.split``.every(c => (acc += (c === '(' ? 1 : -1)) >= 0) && acc === 0;
const validate = str => {let acc = 0;for (let i = 0; i < str.length; i += 1) {acc = str[i] === '(' ? acc + 1 : acc - 1;if (acc < 0) return false;}return acc === 0;};- const validate = str => !(acc = 0) && str.split``.every(c => (acc += (c === '(' ? 1 : -1)) >= 0) && acc === 0;
describe("Test cases", function() { it("()", function() { Test.assertSimilar(validate("()"), true); }); it("(())", function() { Test.assertSimilar(validate("(())"), true); }); it("(()((((())))))", function() { Test.assertSimilar(validate("(()((((())))))"), true); }); it("(())(())", function() { Test.assertSimilar(validate("(())(())"), true); }); it("", function() { Test.assertSimilar(validate(""), true); }); it("((", function() { Test.assertSimilar(validate("(("), false); }); it("())(", function() { Test.assertSimilar(validate("())("), false); }); it("((())", function() { Test.assertSimilar(validate("((())"), false); }); it("(())())", function() { Test.assertSimilar(validate("(())())"), false); }); it("(()(()))))", function() { Test.assertSimilar(validate("(()(()))))"), false); }); it(")", function() { Test.assertSimilar(validate(")"), false); }); it("())(()", function() { Test.assertSimilar(validate("())(()"), false); }); });
const validate = str => {let acc = 0;for (let i = 0; i < str.length; i += 1) {acc = str[i] === '(' ? acc + 1 : acc - 1;if (acc < 0) return false;}return acc === 0;};- describe("Test cases", function() {
- it("()", function() {
- Test.assertSimilar(validate("()"), true);
- });
- it("(())", function() {
- Test.assertSimilar(validate("(())"), true);
- });
- it("(()((((())))))", function() {
- Test.assertSimilar(validate("(()((((())))))"), true);
- });
- it("(())(())", function() {
- Test.assertSimilar(validate("(())(())"), true);
- });
- it("", function() {
- Test.assertSimilar(validate(""), true);
- });
- it("((", function() {
- Test.assertSimilar(validate("(("), false);
- });
- it("())(", function() {
- Test.assertSimilar(validate("())("), false);
- });
- it("((())", function() {
- Test.assertSimilar(validate("((())"), false);
- });
- it("(())())", function() {
- Test.assertSimilar(validate("(())())"), false);
- });
- it("(()(()))))", function() {
- Test.assertSimilar(validate("(()(()))))"), false);
- });
- it(")", function() {
- Test.assertSimilar(validate(")"), false);
- });
- it("())(()", function() {
- Test.assertSimilar(validate("())(()"), false);
- });
- });
#include <cmath> static bool willTheyMeet( int startHour1, int endHour1, int startHour2, int endHour2) { return !( fmax(startHour1, endHour1) < fmin(startHour2, endHour2) || fmin(startHour1, endHour1) > fmax(startHour2, endHour2) ); };
- #include <cmath>
static float hourToRad(int hour){return (float) hour * (M_PI / 12.0);};- static bool willTheyMeet(
- int startHour1,
- int endHour1,
- int startHour2,
- int endHour2)
- {
auto a1 = hourToRad(startHour1);auto a2 = hourToRad(endHour1);auto b1 = hourToRad(startHour2);auto b2 = hourToRad(endHour2);if (a1 == b2 || a2 == b1)return false;float da = (a2 - a1) / 2.0;float db = (b2 - b1) / 2.0;float ma = (a2 + a1) / 2.0;float mb = (b2 + b1) / 2.0;float cda = cos(da);float cdb = cos(db);return cos(ma - b1) >= cda ||cos(ma - b2) >= cda ||cos(mb - a1) >= cdb ||cos(mb - a2) >= cdb;- return !(
- fmax(startHour1, endHour1) < fmin(startHour2, endHour2) ||
- fmin(startHour1, endHour1) > fmax(startHour2, endHour2)
- );
- };