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.
The solution code is written in MoonScript.
import sort, reduce from require "pl.seq" export max_number = => ~~(reduce "..", sort "#{@}"\gmatch".")\reverse!
maxNumber=n=>eval([...n+``].sort().reverse().join``+`n`)- import sort, reduce from require "pl.seq"
- export max_number = => ~~(reduce "..", sort "#{@}"\gmatch".")\reverse!
require "setup" describe("Solution", function() it("should return maximum format of number", function() assert.are.same(4, max_number(4)) assert.are.same(21, max_number(12)) assert.are.same(110, max_number(101), 110) assert.are.same(754000000000000000, max_number(400000005000007000)) assert.are.same(988777666444322200, max_number(307778062924466824)) end) end)
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.const chai = require("chai");const assert = chai.assert;// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- require "setup"
describe("Solution", function() {it("should_return_maximum_format_of_number", function() {assert.strictEqual(maxNumber(4n), 4n);assert.strictEqual(maxNumber(12n), 21n);assert.strictEqual(maxNumber(101n), 110n);assert.strictEqual(maxNumber(400000005000007000n), 754000000000000000n);assert.strictEqual(maxNumber(307778062924466824n), 988777666444322200n);});});- describe("Solution", function()
- it("should return maximum format of number", function()
- assert.are.same(4, max_number(4))
- assert.are.same(21, max_number(12))
- assert.are.same(110, max_number(101), 110)
- assert.are.same(754000000000000000, max_number(400000005000007000))
- assert.are.same(988777666444322200, max_number(307778062924466824))
- end)
- end)
Improve readability
interface ListOfBoxes { code: string bomb?: boolean boxes?: ListOfBoxes[] } export function findTheBomb (listOfBoxes: ListOfBoxes [], path: string[] = []): string | null { if (!listOfBoxes.length) { return "No Boxes No Bomb!" } for (const box of listOfBoxes) { const newPath = [...path, box.code] if (box.bomb) { return newPath.join(" > ") } if (box.boxes) { const found = findTheBomb(box.boxes, newPath) if (found) { return found } } } return null } const listBoxes1: ListOfBoxes [] = [ { code: "B1", boxes: [ { code: "B1.1" }, { code: "B1.2" } ] }, { code: "B2", boxes: [ { code: "B2.1", boxes: [ { code: "B2.1.1" }, { code: "B2.1.2", bomb: true } ] }, { code: "B2.2" } ] } ] const listBoxes2: ListOfBoxes [] = [ { "code": "B1.2", "boxes": [ { "code": "B1.2.2", "boxes": [ { "code": "B1.2.2.6" } ] }, { "code": "B1.2.7" }, { "code": "B1.2.4" }, { "code": "B1.2.0" }, { "code": "B1.2.9", "boxes": [ { "code": "B1.2.9.0", "bomb": true, } ] } ] }, { "code": "B2.8", "boxes": [ { "code": "B2.8.0" }, { "code": "B2.8.7" }, { "code": "B2.8.6" }, { "code": "B2.8.3" } ] }, { "code": "B3.6", "boxes": [ { "code": "B3.6.5" }, { "code": "B3.6.7", "boxes": [ { "code": "B3.6.7.9" } ] } ] } ] const listBoxes3: ListOfBoxes [] = [ { "code": "B1.6", "boxes": [ { "code": "B1.6.9" }, { "code": "B1.6.2" } ] }, { "code": "B2.4", "boxes": [ { "code": "B2.4.0" }, { "code": "B2.4.3", "boxes": [ { "code": "B2.4.3.9", "bomb": true } ] }, { "code": "B2.4.7" } ] } ] findTheBomb([]) findTheBomb(listBoxes1); findTheBomb(listBoxes2); findTheBomb(listBoxes3);
- interface ListOfBoxes {
code: string;bomb?: boolean;boxes?: ListOfBoxes[];- code: string
- bomb?: boolean
- boxes?: ListOfBoxes[]
- }
- export function findTheBomb (listOfBoxes: ListOfBoxes [], path: string[] = []): string | null {
// You Can Code Below Hereif(listOfBoxes.length > 0){for (const box of listOfBoxes) {const newPath = [...path, box.code];if (box.bomb) {return newPath.join(" > ");}if (box.boxes) {const found = findTheBomb(box.boxes, newPath);if (found) {return found;}}}return null;}else{- if (!listOfBoxes.length) {
- return "No Boxes No Bomb!"
- }
- for (const box of listOfBoxes) {
- const newPath = [...path, box.code]
- if (box.bomb) {
- return newPath.join(" > ")
- }
- if (box.boxes) {
- const found = findTheBomb(box.boxes, newPath)
- if (found) {
- return found
- }
- }
- }
- return null
- }
- const listBoxes1: ListOfBoxes [] = [
- {
- code: "B1",
- boxes: [
- {
- code: "B1.1"
- },
- {
- code: "B1.2"
- }
- ]
- },
- {
- code: "B2",
- boxes: [
- {
- code: "B2.1",
- boxes: [
- {
- code: "B2.1.1"
- },
- {
- code: "B2.1.2",
- bomb: true
- }
- ]
- },
- {
- code: "B2.2"
- }
- ]
- }
- ]
- const listBoxes2: ListOfBoxes [] = [
- {
- "code": "B1.2",
- "boxes": [
- {
- "code": "B1.2.2",
- "boxes": [
- {
- "code": "B1.2.2.6"
- }
- ]
- },
- {
- "code": "B1.2.7"
- },
- {
- "code": "B1.2.4"
- },
- {
- "code": "B1.2.0"
- },
- {
- "code": "B1.2.9",
- "boxes": [
- {
- "code": "B1.2.9.0",
- "bomb": true,
- }
- ]
- }
- ]
- },
- {
- "code": "B2.8",
- "boxes": [
- {
- "code": "B2.8.0"
- },
- {
- "code": "B2.8.7"
- },
- {
- "code": "B2.8.6"
- },
- {
- "code": "B2.8.3"
- }
- ]
- },
- {
- "code": "B3.6",
- "boxes": [
- {
- "code": "B3.6.5"
- },
- {
- "code": "B3.6.7",
- "boxes": [
- {
- "code": "B3.6.7.9"
- }
- ]
- }
- ]
- }
- ]
- const listBoxes3: ListOfBoxes [] = [
- {
- "code": "B1.6",
- "boxes": [
- {
- "code": "B1.6.9"
- },
- {
- "code": "B1.6.2"
- }
- ]
- },
- {
- "code": "B2.4",
- "boxes": [
- {
- "code": "B2.4.0"
- },
- {
- "code": "B2.4.3",
- "boxes": [
- {
- "code": "B2.4.3.9",
- "bomb": true
- }
- ]
- },
- {
- "code": "B2.4.7"
- }
- ]
- }
- ]
- findTheBomb([])
- findTheBomb(listBoxes1);
- findTheBomb(listBoxes2);
- findTheBomb(listBoxes3);
function rgbToHsv(rgb) { const [r, g, b] = rgb.map(value => value / 255); const v = Math.max(r, g, b); const min = Math.min(r, g, b); const delta = v - min; const s = v === 0 ? 0 : delta / v; let h = 0; const getRes = (h, s, v) => [Math.round(h), Math.round(s * 100), Math.round(v * 100)]; if (s === 0) return getRes(h, s, v); if (v === r) { h = (g - b) / delta; } else if (v === g) { h = 2 + (b - r) / delta; } else { h = 4 + (r - g) / delta; } h *= 60; if (h < 0) h += 360; return getRes(h, s, v); }
- function rgbToHsv(rgb) {
- const [r, g, b] = rgb.map(value => value / 255);
- const v = Math.max(r, g, b);
- const min = Math.min(r, g, b);
- const delta = v - min;
- const s = v === 0 ? 0 : delta / v;
- let h = 0;
if (s !== 0) {if (v === r) {h = (g - b) / delta;} else if (v === g) {h = 2 + (b - r) / delta;} else {h = 4 + (r - g) / delta;}h *= 60;if (h < 0) h += 360;- const getRes = (h, s, v) => [Math.round(h), Math.round(s * 100), Math.round(v * 100)];
- if (s === 0) return getRes(h, s, v);
- if (v === r) {
- h = (g - b) / delta;
- } else if (v === g) {
- h = 2 + (b - r) / delta;
- } else {
- h = 4 + (r - g) / delta;
- }
- h *= 60;
- if (h < 0) h += 360;
return [Math.round(h), Math.round(s * 100), Math.round(v * 100)];- return getRes(h, s, v);
- }
#include <string> #include <string_view> #include <vector> #include <numeric> #include <sstream> std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , ") { if (name.empty()) return ""; std::stringstream ss; ss << name.front(); std::for_each(std::next(name.cbegin()), name.cend(), [&ss, delim](auto const& s) { ss << delim << s; }); return ss.str(); }
- #include <string>
- #include <string_view>
- #include <vector>
- #include <numeric>
- #include <sstream>
std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , "){if (name.empty()) return "";return std::accumulate(std::next(name.begin()),name.end(),name.front(),[&](auto &s, auto &n){return s.append(delim).append(n);});- std::string Jointup(const std::vector<std::string> &name , const std::string_view delim = " , ")
- {
- if (name.empty()) return "";
- std::stringstream ss;
- ss << name.front();
- std::for_each(std::next(name.cbegin()), name.cend(), [&ss, delim](auto const& s) { ss << delim << s; });
- return ss.str();
- }
the solution is incredibly slow :: try .. 999999999 that's far from long long limits, butt still it takes forever for our soulution to calculate result. monte carlo algorithm is more fun than just straight bruteforce
we can replace big chunk of multiplications with comparison with constants,cutting square with R-size diagonal.. and go crazy a bit further.
also x++ is a "C" idiom, and it works the same way as ++x for integers in C++, so why bother with "more effective" way from books ++x.. we already can traverse collections without ++ing iterators.. and pow is overkill for one multiplication.. you use it for allocating space in stack and nothing more.. we can use inline one for same purpose.
obviously tests were designed for certain number of dots, but we can greatly increase it thus getting better results, so my approach slaps alot more dots in uniformal grid and gets to pi alot closer on the same n and alot faster :) say random generator slapped dots in uniformal grid fashion all over the 1sq quadrant .. and we use this possibility to speed up our algorithm
#include <random> std::uniform_real_distribution<long double> dis(0.,1.); // Default is double std::random_device rd; std::mt19937 gen(rd()); inline long double ppow(const long double& x) { return x * x; } long double pi_estimate_yours(long long n, int seed=0) { gen.seed(seed); long long inside = 0; for (auto i=0; i!=n; i++) { if (ppow(dis(gen))+ppow(dis(gen)) < 1.){ inside++; } } return (4.*inside)/n; } long double divisor(long double num) { return sqrt((2.*sqrt(2.) * static_cast<double>(num)) / (2. - sqrt(2.))); } // // i // long double pi_estimate(long double n, int seed = 0) { gen.seed(seed); // warnings long double div = divisor(n); long double R = 1.L; long double Rsq = R * R; long double A = sqrt(0.5L); long double Rbit = 1.L / div; std::cout << Rbit << std::endl; long double Rnum = div; long double Anum = A / Rbit; long double inside_accum = Anum * Anum + (Rnum - Anum) * Anum; // regular grid long double yisq = 0; long double subaccum = 0; for (long double yi = A; yi < R; yi += Rbit) { yisq = yi * yi; for (long double xi = A * (R - yi)/(R - A); xi < A; xi += Rbit) { if (yisq + xi * xi < Rsq) { subaccum++; } else break; } } inside_accum += 2 * subaccum; long double pee = inside_accum * 4. / (Rnum * Rnum); return (pee); }
- #include <random>
- std::uniform_real_distribution<long double> dis(0.,1.); // Default is double
- std::random_device rd;
- std::mt19937 gen(rd());
long double pi_estimate(long long n, int seed=0) {- inline long double ppow(const long double& x) {
- return x * x;
- }
- long double pi_estimate_yours(long long n, int seed=0) {
- gen.seed(seed);
long long inside=0;for (auto i=0; i!=n; ++i) if (std::pow(dis(gen),2)+std::pow(dis(gen),2) < 1.) ++inside;- long long inside = 0;
- for (auto i=0; i!=n; i++) {
- if (ppow(dis(gen))+ppow(dis(gen)) < 1.){
- inside++;
- }
- }
- return (4.*inside)/n;
- }
- long double divisor(long double num) {
- return sqrt((2.*sqrt(2.) * static_cast<double>(num)) / (2. - sqrt(2.)));
- }
- //
- // i
- //
- long double pi_estimate(long double n, int seed = 0) {
- gen.seed(seed); // warnings
- long double div = divisor(n);
- long double R = 1.L;
- long double Rsq = R * R;
- long double A = sqrt(0.5L);
- long double Rbit = 1.L / div;
- std::cout << Rbit << std::endl;
- long double Rnum = div;
- long double Anum = A / Rbit;
- long double inside_accum = Anum * Anum + (Rnum - Anum) * Anum; // regular grid
- long double yisq = 0;
- long double subaccum = 0;
- for (long double yi = A; yi < R; yi += Rbit) {
- yisq = yi * yi;
- for (long double xi = A * (R - yi)/(R - A); xi < A; xi += Rbit) {
- if (yisq + xi * xi < Rsq) {
- subaccum++;
- } else break;
- }
- }
- inside_accum += 2 * subaccum;
- long double pee = inside_accum * 4. / (Rnum * Rnum);
- return (pee);
- }
// TODO: Replace examples and use TDD development by writing your own tests #include <limits> #include <iostream> #include <iomanip> bool AreEqual(long double v1, long double v2, long double delta) {return std::abs(v2-v1)<=delta;} Describe(BASE) { It(Basics) { //Assert::That(AreEqual(pi_estimate(1000,42), 3.176,0.001),Equals(true)); //Assert::That(AreEqual(pi_estimate(1000),3.088,0.001),Equals(true)); //Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,0.00001),Equals(true)); //Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,1e-18),Equals(true)); //Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1417696,1e-20),Equals(true)); Assert::That(AreEqual(pi_estimate(1'000'000),3.14,0.001),Equals(false)); } };
- // TODO: Replace examples and use TDD development by writing your own tests
- #include <limits>
- #include <iostream>
- #include <iomanip>
- bool AreEqual(long double v1, long double v2, long double delta) {return std::abs(v2-v1)<=delta;}
- Describe(BASE)
- {
- It(Basics)
- {
Assert::That(AreEqual(pi_estimate(1000,42), 3.176,0.001),Equals(true));Assert::That(AreEqual(pi_estimate(1000),3.088,0.001),Equals(true));Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,0.00001),Equals(true));Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,1e-18),Equals(true));Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1417696,1e-20),Equals(true));- //Assert::That(AreEqual(pi_estimate(1000,42), 3.176,0.001),Equals(true));
- //Assert::That(AreEqual(pi_estimate(1000),3.088,0.001),Equals(true));
- //Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,0.00001),Equals(true));
- //Assert::That(AreEqual(pi_estimate(1'000'000),3.13758,1e-18),Equals(true));
- //Assert::That(AreEqual(pi_estimate(10'000'000,123456789),3.1417696,1e-20),Equals(true));
- Assert::That(AreEqual(pi_estimate(1'000'000),3.14,0.001),Equals(false));
- }
- };
Description is not accurate: if you pass by value, you cannot return both a bool (whether the vector is ordered or not) and a sorted vector ????
#include <vector> #include <algorithm> #include <utility> bool Ordering(const std::vector<int> &num) { if (num.size()<=1) return true; return std::none_of(std::next(num.cbegin()),num.end(), [prev=num[0]](int val) mutable { return val<std::exchange(prev,val); }); }
using namespace std;int Ordering(vector<int> num) //1,0,3{vector <int> result;for(int i = 0 ; i < num.size() ; i++){sort(num.begin() , num.end());result.push_back(num[i]);}return result;}- #include <vector>
- #include <algorithm>
- #include <utility>
- bool Ordering(const std::vector<int> &num) {
- if (num.size()<=1) return true;
- return std::none_of(std::next(num.cbegin()),num.end(),
- [prev=num[0]](int val) mutable {
- return val<std::exchange(prev,val);
- });
- }
// TODO: Replace examples and use TDD by writing your own tests #include <limits> Describe(Ordering_) // Cannot have the same name as the function ??!! { It(Order_To_Greatest) { Assert::That(Ordering({1,2,3,4,5}), Equals(true)); Assert::That(Ordering({2,1,3,4,5}), Equals(false)); Assert::That(Ordering({}), Equals(true)); Assert::That(Ordering({1}), Equals(true)); Assert::That(Ordering({1,1,1,1,1,1,1,1,0,1,1,1,1,1,1}), Equals(false)); Assert::That(Ordering({1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}), Equals(true)); const auto maxint=std::numeric_limits<int>::max(); Assert::That(Ordering({1,1,1,1,1,1,maxint,maxint,maxint}), Equals(true)); } };
- // TODO: Replace examples and use TDD by writing your own tests
Describe(Ordering)- #include <limits>
- Describe(Ordering_) // Cannot have the same name as the function ??!!
- {
- It(Order_To_Greatest)
- {
Assert::That({1,2,3,4,5}, Equals({1,2,3,4,5}));Assert::That({2,1,3,4,5}, Equals({1,2,3,4,5})- Assert::That(Ordering({1,2,3,4,5}), Equals(true));
- Assert::That(Ordering({2,1,3,4,5}), Equals(false));
- Assert::That(Ordering({}), Equals(true));
- Assert::That(Ordering({1}), Equals(true));
- Assert::That(Ordering({1,1,1,1,1,1,1,1,0,1,1,1,1,1,1}), Equals(false));
- Assert::That(Ordering({1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}), Equals(true));
- const auto maxint=std::numeric_limits<int>::max();
- Assert::That(Ordering({1,1,1,1,1,1,maxint,maxint,maxint}), Equals(true));
- }
- };
local function find_squares_in_array(arr) local sum = 0 for _, v in ipairs(arr) do sum = sum + v ^ 2 end return sum end return find_squares_in_array
from numpy import square- local function find_squares_in_array(arr)
- local sum = 0
- for _, v in ipairs(arr) do
- sum = sum + v ^ 2
- end
- return sum
- end
def find_squares_in_array(arr):return sum(map(square, arr))- return find_squares_in_array
local find_squares_in_array = require "solution" it("Sample Tests", function() assert.are.same(0, find_squares_in_array {}) assert.are.same(55, find_squares_in_array { 1, 2, 3, 4, 5 }) assert.are.same(16, find_squares_in_array { 4 }) assert.are.same(0, find_squares_in_array { 0, 0, 0, 0 }) end)
from solution import find_squares_in_arrayimport codewars_test as test- local find_squares_in_array = require "solution"
@test.describe("find_squares_in_array")def sample_tests():@test.it('Sample Tests')def sample_tests():test.assert_equals(find_squares_in_array([]), 0)test.assert_equals(find_squares_in_array([1, 2, 3, 4, 5]), 55)test.assert_equals(find_squares_in_array([4]), 16)test.assert_equals(find_squares_in_array([0, 0, 0, 0]), 0)- it("Sample Tests", function()
- assert.are.same(0, find_squares_in_array {})
- assert.are.same(55, find_squares_in_array { 1, 2, 3, 4, 5 })
- assert.are.same(16, find_squares_in_array { 4 })
- assert.are.same(0, find_squares_in_array { 0, 0, 0, 0 })
- end)