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.
import random operations = ["+","-","*","/"] def calculate(operations): num = random.randint(0,1000) num2 = random.randint(0,1000) operation = operations[random.randint(0,3)]- import random
num = random.randint(0,1000)- operations = ["+","-","*","/"]
num2 = random.randint(0,1000)- def calculate(operations):
- num = random.randint(0,1000)
- num2 = random.randint(0,1000)
- operation = operations[random.randint(0,3)]
Small demo to show that reserve can save allocations.
Intentionally fails a test so it can print the number of allocations out.
on the small allocation aka 10 bytes, SSO kicks in and no allocation happens, but on the big string 1000 bytes the difference is pretty clear
#include <memory_resource> #include <string> #include <string_view> #include <utility> struct CountingResource : std::pmr::memory_resource { std::size_t allocs = 0; std::size_t bytes = 0; void* do_allocate(std::size_t n, std::size_t a) override { allocs++; bytes += n; return std::pmr::new_delete_resource()->allocate(n, a); } void do_deallocate(void* p, std::size_t n, std::size_t a) override { std::pmr::new_delete_resource()->deallocate(p, n, a); } bool do_is_equal(const memory_resource& other) const noexcept override { return this == &other; } }; std::pmr::string with_pre_alloc(std::string_view input, std::pmr::memory_resource* mr) { std::pmr::string result{mr}; result.reserve(input.size()); std::transform(input.begin(), input.end(), std::back_inserter(result), [](unsigned char c) -> char { return static_cast<char>(std::toupper(c)); }); return result; } std::pmr::string without_pre_alloc(std::string_view input, std::pmr::memory_resource* mr) { std::pmr::string result{mr}; std::transform(input.begin(), input.end(), std::back_inserter(result), [](unsigned char c) -> char { return static_cast<char>(std::toupper(c)); }); return result; }- #include <memory_resource>
- #include <string>
- #include <string_view>
#include <algorithm>- #include <utility>
using namespace std;- struct CountingResource : std::pmr::memory_resource {
- std::size_t allocs = 0;
- std::size_t bytes = 0;
string fun(string_view input)- void* do_allocate(std::size_t n, std::size_t a) override {
- allocs++;
- bytes += n;
- return std::pmr::new_delete_resource()->allocate(n, a);
- }
- void do_deallocate(void* p, std::size_t n, std::size_t a) override {
- std::pmr::new_delete_resource()->deallocate(p, n, a);
- }
- bool do_is_equal(const memory_resource& other) const noexcept override {
- return this == &other;
- }
- };
- std::pmr::string with_pre_alloc(std::string_view input, std::pmr::memory_resource* mr)
- {
- std::pmr::string result{mr};
- result.reserve(input.size());
- std::transform(input.begin(), input.end(), std::back_inserter(result),
- [](unsigned char c) -> char { return static_cast<char>(std::toupper(c)); });
- return result;
- }
- std::pmr::string without_pre_alloc(std::string_view input, std::pmr::memory_resource* mr)
- {
string result;transform(input.cbegin(), input.cend(), back_inserter(result), [](unsigned char c) -> char {return toupper(c);});- std::pmr::string result{mr};
- std::transform(input.begin(), input.end(), std::back_inserter(result),
- [](unsigned char c) -> char { return static_cast<char>(std::toupper(c)); });
- return result;
- }
#include <string> static std::string make_input(std::size_t n) { std::string s; s.reserve(n); for (std::size_t i = 0; i < n; ++i) s.push_back(static_cast<char>('a' + (i % 26))); return s; } static std::string make_expected(std::string_view in) { std::string out; out.reserve(in.size()); for (unsigned char c : std::string(in)) out.push_back(static_cast<char>(std::toupper(c))); return out; } static auto alloc_report(const CountingResource& r1, const CountingResource& r2) { return [&r1, &r2]() { std::ostringstream os; os << "Allocations:\n" << "with_pre_alloc allocs=" << r1.allocs << " bytes=" << r1.bytes << "\n" << "without_pre_alloc allocs=" << r2.allocs << " bytes=" << r2.bytes << "\n"; return os.str(); }; } Describe(kumite) { It(small_strings_use_sso) { auto input = make_input(10); auto expected = make_expected(input); CountingResource r1; auto a = with_pre_alloc(input, &r1); Assert::That(std::string_view(a), Equals(expected)); CountingResource r2; auto b = without_pre_alloc(input, &r2); Assert::That(std::string_view(b), Equals(expected)); //Hack to print the result Assert::That(false, Is().True(), alloc_report(r1, r2)); } It(compares_allocations_on_large_input) { auto input = make_input(2000); auto expected = make_expected(input); CountingResource r1; auto a = with_pre_alloc(input, &r1); Assert::That(std::string_view(a), Equals(expected)); CountingResource r2; auto b = without_pre_alloc(input, &r2); Assert::That(std::string_view(b), Equals(expected)); //Hack to print the result Assert::That(false, Is().True(), alloc_report(r1, r2)); } };void tester(std::string_view input, std::string_view expected)- #include <string>
- static std::string make_input(std::size_t n) {
- std::string s;
- s.reserve(n);
- for (std::size_t i = 0; i < n; ++i)
- s.push_back(static_cast<char>('a' + (i % 26)));
- return s;
- }
- static std::string make_expected(std::string_view in) {
- std::string out;
- out.reserve(in.size());
- for (unsigned char c : std::string(in)) out.push_back(static_cast<char>(std::toupper(c)));
- return out;
- }
- static auto alloc_report(const CountingResource& r1,
- const CountingResource& r2)
- {
auto result = fun(input);Assert::That(result, Equals(expected));- return [&r1, &r2]() {
- std::ostringstream os;
- os << "Allocations:\n"
- << "with_pre_alloc allocs=" << r1.allocs
- << " bytes=" << r1.bytes << "\n"
- << "without_pre_alloc allocs=" << r2.allocs
- << " bytes=" << r2.bytes << "\n";
- return os.str();
- };
- }
Describe(fun_tests)- Describe(kumite)
- {
It(should_upper_all_chars)- It(small_strings_use_sso)
- {
tester("Welcome", "WELCOME");tester("hello", "HELLO");tester("yellow", "YELLOW");tester("blue", "BLUE");tester("1a2b3c", "1A2B3C");- auto input = make_input(10);
- auto expected = make_expected(input);
- CountingResource r1;
- auto a = with_pre_alloc(input, &r1);
- Assert::That(std::string_view(a), Equals(expected));
- CountingResource r2;
- auto b = without_pre_alloc(input, &r2);
- Assert::That(std::string_view(b), Equals(expected));
- //Hack to print the result
- Assert::That(false, Is().True(), alloc_report(r1, r2));
- }
- It(compares_allocations_on_large_input)
- {
- auto input = make_input(2000);
- auto expected = make_expected(input);
- CountingResource r1;
- auto a = with_pre_alloc(input, &r1);
- Assert::That(std::string_view(a), Equals(expected));
- CountingResource r2;
- auto b = without_pre_alloc(input, &r2);
- Assert::That(std::string_view(b), Equals(expected));
- //Hack to print the result
- Assert::That(false, Is().True(), alloc_report(r1, r2));
- }
Well, point is to make it crazy, and chaotic, making it all into a bunzzled stuff, so yeah, good luck tryna get betta.
-The Chaos Dud, ethan330NEO...
final class WordChain { static boolean validate(String[]w,int n){ if(w==null||w.length<2||n<1)return false; int c=1;while(c<w.length<<1)c<<=1; String[]k=new String[c];int[]h=new int[c];int m=c-1; for(int i=0;i<w.length;i++){ String s=w[i];if(s==null||s.length()<n)return false; if(i>0)for(int j=0;j<n;j++) if((w[i-1].charAt(w[i-1].length()-n+j)|32)!=(s.charAt(j)|32))return false; int x=0;for(int j=0;j<s.length();j++)x=31*x+(s.charAt(j)|32); for(int j=x&m;;j=j+1&m){ String t=k[j]; if(t==null){k[j]=s;h[j]=x;break;} if(h[j]==x&&t.length()==s.length()){ int z=0,L=s.length(); while(z<L&&(t.charAt(z)|32)==(s.charAt(z)|32))z++; if(z==L)return false; } } } return true; } } //NUH UH NO ONE CAN CHAOS MORE THAN MEimport java.util.HashSet;import java.util.Set;- final class WordChain {
static boolean validate(String[] words, int n) {if (words == null || words.length < 2 || n < 1) return false;Set<String> seen = new HashSet<>();for (int i = 0; i < words.length; i++) {String w = words[i];if (w == null || w.length() < n) return false;String lower = w.toLowerCase();if (i > 0) {String prev = words[i - 1].toLowerCase();if (!prev.substring(prev.length() - n).equals(lower.substring(0, n)))return false;}if (!seen.add(lower)) return false;}return true;- static boolean validate(String[]w,int n){
- if(w==null||w.length<2||n<1)return false;
- int c=1;while(c<w.length<<1)c<<=1;
- String[]k=new String[c];int[]h=new int[c];int m=c-1;
- for(int i=0;i<w.length;i++){
- String s=w[i];if(s==null||s.length()<n)return false;
- if(i>0)for(int j=0;j<n;j++)
- if((w[i-1].charAt(w[i-1].length()-n+j)|32)!=(s.charAt(j)|32))return false;
- int x=0;for(int j=0;j<s.length();j++)x=31*x+(s.charAt(j)|32);
- for(int j=x&m;;j=j+1&m){
- String t=k[j];
- if(t==null){k[j]=s;h[j]=x;break;}
- if(h[j]==x&&t.length()==s.length()){
- int z=0,L=s.length();
- while(z<L&&(t.charAt(z)|32)==(s.charAt(z)|32))z++;
- if(z==L)return false;
- }
- }
- }
- return true;
- }
- }
- //NUH UH NO ONE CAN CHAOS MORE THAN ME
public class Kata { public static String getGrade(int score) { System.out.println("scroe " + score); for (Scores s : Scores.values()) { if (s.matches(score)) { System.out.println("scroe " + score + " name -> " + s.name()); return s.name(); } } return "Invalid"; } enum Scores { A(90,100), B(80, 89), C(70, 79), D(60, 69), F(0, 59); private final int min; private final int max; Scores(int min, int max) { this.min = min; this.max = max; } boolean matches (int score) { return score >= min && score <= max; } } }- public class Kata {
- public static String getGrade(int score) {
// Ваш код здесьreturn "";- System.out.println("scroe " + score);
- for (Scores s : Scores.values())
- {
- if (s.matches(score)) {
- System.out.println("scroe " + score + " name -> " + s.name());
- return s.name();
- }
- }
- return "Invalid";
- }
}- enum Scores {
- A(90,100),
- B(80, 89),
- C(70, 79),
- D(60, 69),
- F(0, 59);
- private final int min;
- private final int max;
- Scores(int min, int max) {
- this.min = min;
- this.max = max;
- }
- boolean matches (int score) {
- return score >= min && score <= max;
- }
- }
- }