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.
public class Kata { public static int SameCase(char a, char b) { if (!char.IsLetter(a) || !char.IsLetter(b)) { return -1; } return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0; } }
- public class Kata
- {
- public static int SameCase(char a, char b)
- {
bool isUpperA;bool isUpperB;if (a >= 97 && a <= 122){isUpperA = false;}else if (a >= 65 && a <= 90){isUpperA = true;}else{return -1;}if (b >= 97 && b <= 122){isUpperB = false;}else if (b >= 65 && b <= 90){isUpperB = true;}else- if (!char.IsLetter(a) || !char.IsLetter(b))
- {
- return -1;
- }
if ( isUpperA == isUpperB ){return 1;}else{return 0;}- return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0;
- }
import java.math.BigInteger; import java.util.stream.Collectors; public class MaxNumber { public static BigInteger print(long number) { return new BigInteger(Long.toString(number) .chars() .mapToObj(c -> String.valueOf((char) c)) .sorted((a, b) -> b.compareTo(a)) .collect(Collectors.joining())); } }
import java.util.Arrays;import java.util.Comparator;import java.util.stream.Collectors;- import java.math.BigInteger;
- import java.util.stream.Collectors;
- public class MaxNumber {
- public static BigInteger print(long number) {
String numeroMaximo = Long.toString(number).chars().mapToObj(Character::toString).sorted((a, b) -> b.compareTo(a)).collect(Collectors.joining());return new BigInteger(numeroMaximo);- return new BigInteger(Long.toString(number)
- .chars()
- .mapToObj(c -> String.valueOf((char) c))
- .sorted((a, b) -> b.compareTo(a))
- .collect(Collectors.joining()));
- }
#include <stdlib.h> #include <string.h> char *fanis(const char *input, int value) { char *r = malloc(strlen(input) + 1), *p = r; while (*input) *p++ = *input++ + value; return *p = '\0', r; }
- #include <stdlib.h>
- #include <string.h>
char *fanis(char *r, int k) {char *w = malloc(strlen(r) + 1), *p = w;while(w && *r) *p++ = k + *r++;return w;- char *fanis(const char *input, int value)
- {
- char *r = malloc(strlen(input) + 1), *p = r;
- while (*input) *p++ = *input++ + value;
- return *p = '\0', r;
- }
#include <criterion/criterion.h> char *fanis(const char *input, int value); void basic_test(const char *input, int value, const char *expected) { char *submited = fanis(input, value); cr_assert_str_eq(submited, expected); free(submited); } Test(the_multiply_function, should_pass_all_the_tests_provided) { basic_test("123", 2, "345"); basic_test("472", 1, "583"); }
- #include <criterion/criterion.h>
- char *fanis(const char *input, int value);
Test(the_multiply_function, should_pass_all_the_tests_provided) {cr_assert_str_eq(fanis("123", 2), "345");- void basic_test(const char *input, int value, const char *expected)
- {
- char *submited = fanis(input, value);
- cr_assert_str_eq(submited, expected);
- free(submited);
- }
- Test(the_multiply_function, should_pass_all_the_tests_provided)
- {
- basic_test("123", 2, "345");
- basic_test("472", 1, "583");
- }
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));
- }
- };