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.
In this varation, I have refactored the code so that the execute helper wraps up the transaction rollback as part of its method call. It also will try to parse the response as json and provide that as the 2nd argument, DRYing up the code as more tests are added.
describe "Solution" do it "should test for something" do RequestHelpers.execute('GET', SolutionController, 'show') do |response, json| Test.assert_equals(json['title'], 'Test') end end end
require "rack/utils"def with_dbActiveRecord::Base.transaction doyieldraise ActiveRecord::Rollbackendend- describe "Solution" do
- it "should test for something" do
with_db doresponse = RequestHelpers.execute('GET', SolutionController, 'show')json = JSON.parse(response.body)- RequestHelpers.execute('GET', SolutionController, 'show') do |response, json|
- Test.assert_equals(json['title'], 'Test')
- end
- end
- end
#include <iostream> #include <string> using namespace std ; int helloCplusplus(){ string str("Hello, C++!"); cout << str << endl; return 0; }
- #include <iostream>
- #include <string>
- using namespace std ;
- int helloCplusplus(){
string str = "Hello, C++!";cout << str << '\n';- string str("Hello, C++!");
- cout << str << endl;
- return 0;
- }
import java.util.Arrays; class Solution { public static int retSmallestPositiveInteger() { for(int i=1; true ; i++) { if (i % 10 == 0) continue; if(hasSameDigits(i, i*2, i*3, i*4 , i*5, i*6)) return i; } } private static boolean hasSameDigits(int a, int b, int c, int d, int e, int f) { char[] aDigits = Integer.toString(a).toCharArray(); char[] bDigits = Integer.toString(b).toCharArray(); char[] cDigits = Integer.toString(c).toCharArray(); char[] dDigits = Integer.toString(d).toCharArray(); char[] eDigits = Integer.toString(e).toCharArray(); char[] fDigits = Integer.toString(f).toCharArray(); Arrays.sort(aDigits); Arrays.sort(bDigits); Arrays.sort(cDigits); Arrays.sort(dDigits); Arrays.sort(eDigits); Arrays.sort(fDigits); return (Arrays.equals(aDigits, bDigits) && Arrays.equals(aDigits, cDigits) && Arrays.equals(aDigits, dDigits) && Arrays.equals(aDigits, eDigits) && Arrays.equals(aDigits, fDigits)); } }
import java.util.*;- import java.util.Arrays;
- class Solution {
- public static int retSmallestPositiveInteger() {
for(int i=1; ; i++) {if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))return i;- for(int i=1; true ; i++) {
- if (i % 10 == 0)
- continue;
- if(hasSameDigits(i, i*2, i*3, i*4 , i*5, i*6))
- return i;
- }
- }
private static boolean hasSameDigits(int x, int y) {char[] xdigits = Integer.toString(x).toCharArray();char[] ydigits = Integer.toString(y).toCharArray();Arrays.sort(xdigits);Arrays.sort(ydigits);return Arrays.equals(xdigits, ydigits);- private static boolean hasSameDigits(int a, int b, int c, int d, int e, int f) {
- char[] aDigits = Integer.toString(a).toCharArray();
- char[] bDigits = Integer.toString(b).toCharArray();
- char[] cDigits = Integer.toString(c).toCharArray();
- char[] dDigits = Integer.toString(d).toCharArray();
- char[] eDigits = Integer.toString(e).toCharArray();
- char[] fDigits = Integer.toString(f).toCharArray();
- Arrays.sort(aDigits);
- Arrays.sort(bDigits);
- Arrays.sort(cDigits);
- Arrays.sort(dDigits);
- Arrays.sort(eDigits);
- Arrays.sort(fDigits);
- return (Arrays.equals(aDigits, bDigits) && Arrays.equals(aDigits, cDigits) && Arrays.equals(aDigits, dDigits) && Arrays.equals(aDigits, eDigits) && Arrays.equals(aDigits, fDigits));
- }
- }
In the study of math, we usually assert (x,y) to set the point in 2-D condition. (x,y,z) to set the point in 3-D condition. As so on.
When we count the distance of two points in 2-D condition, like (x1,y1) and (x2,y2) . We do:
sqrt[(x1-x2)**2+(y1-y2)**2]
The same idea to apply in N-dimensions.
def distance_count(a,b): return sum([(i-j)**2 for (i,j) in zip(a,b)])**0.5
import numpy as np# calculate the distance between two points in 2d,3d ... nDdef distanceND(pA, pB, nD = None):return np.linalg.norm(np.array(pA) - np.array(pB))distance2D = distanceNDdistance3D =distanceND- def distance_count(a,b):
- return sum([(i-j)**2 for (i,j) in zip(a,b)])**0.5
test.assert_equals(distance_count([1,1],[2,2]),2**0.5) test.assert_equals(distance_count([2,1,1],[4,1,1]),2)
from math import sqrtdef distance2D_check(pA, pB):if pA == pB: return 0xA, yA = tuple(pA); xB, yB = tuple(pB)return sqrt((xA - xB)**2 + (yA - yB)**2)def distance3D_check(pA, pB):if pA == pB: return 0xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)def assertFuzzyEquals(actual, expected, msg=""):merr = 1e-10inrange = abs(actual - expected) <= merrif (inrange == False):msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}"msg = msg.format(expected, actual)return Test.expect(inrange, msg)test.describe("Basic Tests for 2D")pA = [1, 1]; pB = [2, 2]assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)pA = [0, 0]; pB = [5, 6]assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)pA = [-5, 4]; pB = [-10, 6]assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)pA = [-5.1, 4.0]; pB = [10.2, -6.3]assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)test.describe("Basic Tests for 3D")pA = [1, 1, 1]; pB = [2, 2, 2]assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)pA = [0, 0, 0]; pB = [7, 8, 9]assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)pA = [-5, 4, 7]; pB = [-10, 6, -18]assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)test.describe("Random Tests for 2D")test.describe("Integer coordinates")import randomfrom random import randintfor h in range(20):pA = [randint(-100, 100), randint(-100, 100)]pB = [randint(-100, 100), randint(-100, 100)]result = distance2D_check(pA, pB)res = distance2D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 2D")test.describe("Decimal coordinates")for h in range(20):pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()]result = distance2D_check(pA, pB)res = distance2D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 3D")test.describe("Integer coordinates")for h in range(20):pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]result = distance3D_check(pA, pB)res = distance3D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 3D")test.describe("Decimal coordinates")for h in range(10):pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()]result = distance3D_check(pA, pB)res = distance3D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))- test.assert_equals(distance_count([1,1],[2,2]),2**0.5)
- test.assert_equals(distance_count([2,1,1],[4,1,1]),2)
from math import sqrt def is_prime(n): return n > 1 and all(n % i for i in range(2, int(sqrt(n))+1))
import math- from math import sqrt
- def is_prime(n):
if n % 2 == 0 and n > 2:return Falsefor i in range(3, int(math.sqrt(n)) + 1, 2):if n % i == 0:return Falsereturn True- return n > 1 and all(n % i for i in range(2, int(sqrt(n))+1))
# write a add function that doesn't use the plus symbol def add(a, b) a - -b end # Make sure to view the test cases to see how this test now passes
- # write a add function that doesn't use the plus symbol
- def add(a, b)
a + b- a - -b
- end
# Make sure to view the test cases to see how this test fails- # Make sure to view the test cases to see how this test now passes
solution = File.open('/home/codewarrior/solution.txt', 'r').read describe "Check Solution" do it "should prevent the '+' symbol from being used anywhere in the code" do puts solution Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!") end it "should still perform addition nonetheless" do Test.assert_equals add(1, 1), 2, "One plus one should equal two" Test.assert_equals add(5, 4), 9 Test.assert_equals add(45, 987), 1032 Test.assert_not_equals add(5, 3), 2, "Your method should do addition and not subtraction" end end
- solution = File.open('/home/codewarrior/solution.txt', 'r').read
- describe "Check Solution" do
- it "should prevent the '+' symbol from being used anywhere in the code" do
- puts solution
- Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!")
- end
- it "should still perform addition nonetheless" do
- Test.assert_equals add(1, 1), 2, "One plus one should equal two"
- Test.assert_equals add(5, 4), 9
- Test.assert_equals add(45, 987), 1032
- Test.assert_not_equals add(5, 3), 2, "Your method should do addition and not subtraction"
- end
- end