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.
function binarySearch(numbers, element) { let left = 0; let right = numbers.length - 1; while(left <= right) { let pivot = Math.floor((left + right) / 2); if(numbers[pivot]==element){ return pivot; } else if(numbers[pivot]>element){ right=pivot-1; } else{ left=pivot+1; } } return -1; }
- function binarySearch(numbers, element) {
- let left = 0;
- let right = numbers.length - 1;
let pivot = Math.floor((left + right) / 2);while(left < pivot && pivot < right) {if(element < numbers[pivot]) {right = pivot;} else {left = pivot;- while(left <= right) {
- let pivot = Math.floor((left + right) / 2);
- if(numbers[pivot]==element){
- return pivot;
- }
pivot = Math.floor((left + right) / 2);}if(element === numbers[pivot]) {return pivot;}- else if(numbers[pivot]>element){
- right=pivot-1;
- }
- else{
- left=pivot+1;
- }
- }
- return -1;
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("return index of searching element", function() { assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1); assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1); assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("return index of searching element", function() {
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 4), 3);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 2), 1);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 5, 6, 7], 8), -1);
- assert.strictEqual(binarySearch([1, 2, 3, 4, 9, 10, 16, 52], 52), 7);
- });
- });
this uses c++17 fold expressions to iterate over all the given arrays at the same time.
#include <vector> #include <iostream> #include <algorithm> constexpr auto iterateVectors = [](auto...vectors) { constexpr auto getSmallestVector = [](auto...vectors) { size_t end{ reinterpret_cast<size_t>(std::numeric_limits<size_t>::max) }; ((end = std::min(vectors.size(), end)), ...); return end; }; constexpr auto writeRow = [](size_t position, auto...vectors) -> void { const auto rowLength{ sizeof...(vectors) }; size_t i{ 0 }; ((std::cerr << vectors[position] << (++i < rowLength ? ':' : '\n')), ...); }; const size_t iterateFor{ getSmallestVector(vectors...) }; for (size_t i{ 0 }; i < iterateFor; i++) writeRow(i, vectors...); };
- #include <vector>
#include <utility>- #include <iostream>
template <class T, class U>void iterateTwoVectors(std::vector<T> A, std::vector<U> B)- #include <algorithm>
- constexpr auto iterateVectors = [](auto...vectors)
- {
- constexpr auto getSmallestVector = [](auto...vectors)
- {
- size_t end{ reinterpret_cast<size_t>(std::numeric_limits<size_t>::max) };
- ((end = std::min(vectors.size(), end)), ...);
- return end;
- };
- constexpr auto writeRow = [](size_t position, auto...vectors) -> void
- {
- const auto rowLength{ sizeof...(vectors) };
- size_t i{ 0 };
- ((std::cerr << vectors[position] << (++i < rowLength ? ':' : '\n')), ...);
- };
for (auto [a,b] = std::pair{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++){std::cout << *a << ":" << *b << "\n";}}- const size_t iterateFor{ getSmallestVector(vectors...) };
- for (size_t i{ 0 }; i < iterateFor; i++)
- writeRow(i, vectors...);
- };
// TODO: Replace examples and use TDD by writing your own tests Describe(iterating) { It(should_iterate_both) { std::vector<int> a{1, 2, 3}; std::vector<char> b{'a', 'b', 'c'}; std::vector<char> c{'a', 'b', 'c', 'd', 'e'}; iterateVectors(a, b, c); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(iterating)
- {
- It(should_iterate_both)
- {
iterateTwoVectors<int, char>({1, 'a', 3}, {'a', 'b'});- std::vector<int> a{1, 2, 3};
- std::vector<char> b{'a', 'b', 'c'};
- std::vector<char> c{'a', 'b', 'c', 'd', 'e'};
- iterateVectors(a, b, c);
- }
- };
RSpec works as expected with the extra diff
message.
puts # "Uncomment the test cases to see the errors"
puts "Uncomment the test cases to see the errors"- puts # "Uncomment the test cases to see the errors"
describe "Example" do it "Will log error" do # expect(false).to eq(true), "This is an error\nThis is a log" # expect("error").not_to eq("error"), "This is an error\nThis is a log" end end
- describe "Example" do
it "Will log error" doTest.expect(true)# Test.expect(false, "This is an error\nThis is a log")# Test.assert_equals(false, true, "This is an errorThis is a log")# Test.assert_not_equals("error", "error", "This is an errorThis is a log")- it "Will log error" do
- # expect(false).to eq(true), "This is an error
- This is a log"
- # expect("error").not_to eq("error"), "This is an error
- This is a log"
- end
- end
Trying to solve sequence problems without loops. This works with some triangle number maths.
function solution(num){ // find the nearest triangle number, use ceil to get to the current row const s = Math.ceil((Math.sqrt(8*num+1)-1)/2); // Gauss Summation to calculate the number of items in the "pyramid above" const t = ((s - 1) * (s))/2; return num - t; }
- function solution(num){
let times = 1;let count = 0;while(num > 0){count = numnum -= times;times++;}return count;- // find the nearest triangle number, use ceil to get to the current row
- const s = Math.ceil((Math.sqrt(8*num+1)-1)/2);
- // Gauss Summation to calculate the number of items in the "pyramid above"
- const t = ((s - 1) * (s))/2;
- return num - t;
- }
const { assert } = require("chai") function test(n, expected) { let actual = solution(n) it(`Expected ${expected}, got ${actual}`, () => { assert.strictEqual(actual, expected) }) } describe("basic tests", function(){ test(3,2); test(55,10); test(4,1); //200th triangle number test(20100, 200) test(20123, 23) })
- const { assert } = require("chai")
- function test(n, expected) {
- let actual = solution(n)
- it(`Expected ${expected}, got ${actual}`, () => {
- assert.strictEqual(actual, expected)
- })
- }
- describe("basic tests", function(){
- test(3,2);
- test(55,10);
- test(4,1);
- //200th triangle number
- test(20100, 200)
- test(20123, 23)
- })
def convert_decimal_roman(numero): num_romanos_dic = { 1: 'I', 5: 'V', 10: 'X', 50: 'L', 100: 'C', 500: 'D', 1000: 'M' } list_num_rom = [{'key': x, 'value': y} for x, y in num_romanos_dic.items()] size_num = len(numero) output = [] for i in range(size_num): num = numero[i:] num_unit = int(num) numextre_izq = int(num[0]) pref = [] for i in range(1, len(list_num_rom), 2): if num_unit >= list_num_rom[i-1]['key'] and num_unit < list_num_rom[i+1]['key']: pref.append(list_num_rom[i-1]['value']) pref.append(list_num_rom[i]['value']) pref.append(list_num_rom[i+1]['value']) if numextre_izq < 4 and numextre_izq > 0: output.append(pref[0]*numextre_izq) if numextre_izq == 4: output.extend(pref[0:2]) if numextre_izq == 5: output.append(pref[1]) if numextre_izq > 5 and numextre_izq < 9: output.append(pref[1] + pref[0]*(numextre_izq-5)) if numextre_izq == 9: output.extend(pref[::2]) output = ''.join(output) return output
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(convert_decimal_roman('69'), 'LXIX') test.assert_equals(convert_decimal_roman('874'), 'DCCCLXXIV') test.assert_equals(convert_decimal_roman('938'), 'CMXXXVIII') test.assert_equals(convert_decimal_roman('442'), 'CDXLII') test.assert_equals(convert_decimal_roman('323'), 'CCCXXIII') test.assert_equals(convert_decimal_roman('548'), 'DXLVIII') test.assert_equals(convert_decimal_roman('386'), 'CCCLXXXVI') test.assert_equals(convert_decimal_roman('962'), 'CMLXII') test.assert_equals(convert_decimal_roman('217'), 'CCXVII') test.assert_equals(convert_decimal_roman('771'), 'DCCLXXI') test.assert_equals(convert_decimal_roman('997'), 'CMXCVII') test.assert_equals(convert_decimal_roman('175'), 'CLXXV') test.assert_equals(convert_decimal_roman('873'), 'DCCCLXXIII') test.assert_equals(convert_decimal_roman('33'), 'XXXIII') test.assert_equals(convert_decimal_roman('508'), 'DVIII') test.assert_equals(convert_decimal_roman('743'), 'DCCXLIII') test.assert_equals(convert_decimal_roman('326'), 'CCCXXVI') test.assert_equals(convert_decimal_roman('858'), 'DCCCLVIII') test.assert_equals(convert_decimal_roman('707'), 'DCCVII') test.assert_equals(convert_decimal_roman('459'), 'CDLIX') test.assert_equals(convert_decimal_roman('976'), 'CMLXXVI') test.assert_equals(convert_decimal_roman('855'), 'DCCCLV') test.assert_equals(convert_decimal_roman('840'), 'DCCCXL') test.assert_equals(convert_decimal_roman('682'), 'DCLXXXII') test.assert_equals(convert_decimal_roman('506'), 'DVI') test.assert_equals(convert_decimal_roman('553'), 'DLIII') test.assert_equals(convert_decimal_roman('1'), 'I') test.assert_equals(convert_decimal_roman('48'), 'XLVIII') test.assert_equals(convert_decimal_roman('125'), 'CXXV') test.assert_equals(convert_decimal_roman('353'), 'CCCLIII') test.assert_equals(convert_decimal_roman('137'), 'CXXXVII') test.assert_equals(convert_decimal_roman('423'), 'CDXXIII') test.assert_equals(convert_decimal_roman('26'), 'XXVI') test.assert_equals(convert_decimal_roman('362'), 'CCCLXII') test.assert_equals(convert_decimal_roman('587'), 'DLXXXVII') test.assert_equals(convert_decimal_roman('599'), 'DXCIX') test.assert_equals(convert_decimal_roman('589'), 'DLXXXIX') test.assert_equals(convert_decimal_roman('619'), 'DCXIX') test.assert_equals(convert_decimal_roman('261'), 'CCLXI') test.assert_equals(convert_decimal_roman('480'), 'CDLXXX') test.assert_equals(convert_decimal_roman('410'), 'CDX') test.assert_equals(convert_decimal_roman('776'), 'DCCLXXVI') test.assert_equals(convert_decimal_roman('625'), 'DCXXV') test.assert_equals(convert_decimal_roman('116'), 'CXVI') test.assert_equals(convert_decimal_roman('112'), 'CXII') test.assert_equals(convert_decimal_roman('603'), 'DCIII') test.assert_equals(convert_decimal_roman('549'), 'DXLIX') test.assert_equals(convert_decimal_roman('838'), 'DCCCXXXVIII') test.assert_equals(convert_decimal_roman('846'), 'DCCCXLVI') test.assert_equals(convert_decimal_roman('756'), 'DCCLVI') test.assert_equals(convert_decimal_roman('333'), 'CCCXXXIII') test.assert_equals(convert_decimal_roman('325'), 'CCCXXV') test.assert_equals(convert_decimal_roman('154'), 'CLIV') test.assert_equals(convert_decimal_roman('104'), 'CIV') test.assert_equals(convert_decimal_roman('669'), 'DCLXIX') test.assert_equals(convert_decimal_roman('822'), 'DCCCXXII') test.assert_equals(convert_decimal_roman('51'), 'LI') test.assert_equals(convert_decimal_roman('372'), 'CCCLXXII') test.assert_equals(convert_decimal_roman('824'), 'DCCCXXIV') test.assert_equals(convert_decimal_roman('136'), 'CXXXVI') test.assert_equals(convert_decimal_roman('89'), 'LXXXIX') test.assert_equals(convert_decimal_roman('693'), 'DCXCIII') test.assert_equals(convert_decimal_roman('81'), 'LXXXI') test.assert_equals(convert_decimal_roman('681'), 'DCLXXXI') test.assert_equals(convert_decimal_roman('306'), 'CCCVI') test.assert_equals(convert_decimal_roman('398'), 'CCCXCVIII') test.assert_equals(convert_decimal_roman('554'), 'DLIV') test.assert_equals(convert_decimal_roman('651'), 'DCLI') test.assert_equals(convert_decimal_roman('403'), 'CDIII') test.assert_equals(convert_decimal_roman('485'), 'CDLXXXV') test.assert_equals(convert_decimal_roman('815'), 'DCCCXV') test.assert_equals(convert_decimal_roman('88'), 'LXXXVIII') test.assert_equals(convert_decimal_roman('53'), 'LIII') test.assert_equals(convert_decimal_roman('610'), 'DCX') test.assert_equals(convert_decimal_roman('410'), 'CDX') test.assert_equals(convert_decimal_roman('376'), 'CCCLXXVI') test.assert_equals(convert_decimal_roman('791'), 'DCCXCI') test.assert_equals(convert_decimal_roman('594'), 'DXCIV') test.assert_equals(convert_decimal_roman('486'), 'CDLXXXVI') test.assert_equals(convert_decimal_roman('165'), 'CLXV') test.assert_equals(convert_decimal_roman('812'), 'DCCCXII') test.assert_equals(convert_decimal_roman('224'), 'CCXXIV') test.assert_equals(convert_decimal_roman('185'), 'CLXXXV') test.assert_equals(convert_decimal_roman('833'), 'DCCCXXXIII') test.assert_equals(convert_decimal_roman('994'), 'CMXCIV') test.assert_equals(convert_decimal_roman('385'), 'CCCLXXXV') test.assert_equals(convert_decimal_roman('821'), 'DCCCXXI') test.assert_equals(convert_decimal_roman('572'), 'DLXXII') test.assert_equals(convert_decimal_roman('428'), 'CDXXVIII') test.assert_equals(convert_decimal_roman('918'), 'CMXVIII') test.assert_equals(convert_decimal_roman('86'), 'LXXXVI') test.assert_equals(convert_decimal_roman('238'), 'CCXXXVIII') test.assert_equals(convert_decimal_roman('206'), 'CCVI') test.assert_equals(convert_decimal_roman('56'), 'LVI') test.assert_equals(convert_decimal_roman('279'), 'CCLXXIX') test.assert_equals(convert_decimal_roman('72'), 'LXXII') test.assert_equals(convert_decimal_roman('380'), 'CCCLXXX') test.assert_equals(convert_decimal_roman('26'), 'XXVI') test.assert_equals(convert_decimal_roman('715'), 'DCCXV') test.assert_equals(convert_decimal_roman('168'), 'CLXVIII')
Input data now come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases. Modified the original test case and added a new one.
/*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/ const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => { /* Create an object with 12 keys and the input values*/ const monthlyCosts = { januaryBill : m1, februaryBill : m2, marchBill : m3, aprilBill : m4, mayBill : m5, juneBill : m6, julyBill : m7, augustBill : m8, septemberBill : m9, octoberBill : m10, novemberBill : m11, decemberBill : m12 } /*print the object*/ console.log(monthlyCosts); // Decide on a variable name and sum all the monthly charges const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0); // Return the value of the variable by typing it in after the `return` keyword below console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2))); /*toFixed converts the result to string, we have to convert it back to number*/ return parseFloat(totalCharges.toFixed(2)); }
const yearlyElectricCosts = () => {// Create an object with 12 keys with the correct values- /*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/
- const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => {
- /* Create an object with 12 keys and the input values*/
- const monthlyCosts = {
januaryBill : 43.11,februaryBill : 50.21,marchBill : 48.92,aprilBill : 62.36,mayBill : 54.64,juneBill : 49.30,julyBill : 61.93,augustBill : 68.54,septemberBill : 71.77,octoberBill : 70.28,novemberBill : 59.56,decemberBill : 62.04- januaryBill : m1,
- februaryBill : m2,
- marchBill : m3,
- aprilBill : m4,
- mayBill : m5,
- juneBill : m6,
- julyBill : m7,
- augustBill : m8,
- septemberBill : m9,
- octoberBill : m10,
- novemberBill : m11,
- decemberBill : m12
- }
// Decide on a variable name and add all the monthly charges- /*print the object*/
- console.log(monthlyCosts);
- // Decide on a variable name and sum all the monthly charges
- const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0);
- // Return the value of the variable by typing it in after the `return` keyword below
- console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2)));
- /*toFixed converts the result to string, we have to convert it back to number*/
- return parseFloat(totalCharges.toFixed(2));
- }
const { assert } = require("chai"); describe("Solution", function() { it("should test for something", function() { assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56, 62.04), 702.66) }); it("should test for something", function() { assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56), 640.62) }); });
- const { assert } = require("chai");
- describe("Solution", function() {
- it("should test for something", function() {
assert.strictEqual(yearlyElectricCosts(), 702.66)- assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56, 62.04), 702.66)
- });
- it("should test for something", function() {
- assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56), 640.62)
- });
- });