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.
What's new:
- Imported separate
std::
-s instead of entirenamespace std
- Implemented testing using string IO
#include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <algorithm> #include <cmath> #include <random> using std::cin; using std::cout; using std::endl; using std::istream; using std::make_pair; using std::mt19937; using std::ostream; using std::pair; using std::random_device; using std::string; using std::vector; string generateRandomNumber() { vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; // Initialize random number generator random_device rd; // Obtain a random number from hardware mt19937 g(rd()); // Seed the generator // Shuffle digits shuffle(digits.begin(), digits.end(), g); // Return the first 4 digits as a string return string(digits.begin(), digits.begin() + 4); } bool hasDuplicateDigits(const string &number) { vector<bool> seen(10, false); for (const auto c : number) { if (seen[c - '0']) return true; seen[c - '0'] = true; } return false; } bool isNumeric(const string &s) { return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); }); } string getUserGuess(istream &in = cin, ostream &out = cout) { string guess; out << "Enter your guess (a 4-digit number with non-repeating digits): "; in >> guess; return guess; } pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) { int correctNumbers = 0; int correctPosition = 0; for (int i = 0; i < 4; ++i) { if (randomNumber[i] == userGuess[i]) { correctNumbers++; correctPosition++; } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) { correctNumbers++; } } return make_pair(correctNumbers, correctPosition); } int acceptGuess(const string &expected, istream &in = cin, ostream &out = cout) { int attempts = 0; while (true) { string userGuess = getUserGuess(in, out); if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) { out << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl; continue; } auto [corNum, corPos] = checkGuess(expected, userGuess); out << "Correct numbers: " << corNum << " Correct position: " << corPos << endl; attempts++; if (corPos == 4) { out << "Congratulations! You guessed the number " << expected << " correctly in " << attempts << " attempts!" << endl; break; } } return attempts; } int _main(istream &in = cin, ostream &out = cout) { // Seed rand since random_shuffle _probably_ uses it. srand(static_cast<unsigned>(time(0))); string randomNumber = generateRandomNumber(); acceptGuess(randomNumber, in, out); return 0; }
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <random>
using namespace std;- using std::cin;
- using std::cout;
- using std::endl;
- using std::istream;
- using std::make_pair;
- using std::mt19937;
- using std::ostream;
- using std::pair;
- using std::random_device;
- using std::string;
- using std::vector;
- string generateRandomNumber() {
- vector<char> digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- // Initialize random number generator
- random_device rd; // Obtain a random number from hardware
- mt19937 g(rd()); // Seed the generator
- // Shuffle digits
- shuffle(digits.begin(), digits.end(), g);
- // Return the first 4 digits as a string
- return string(digits.begin(), digits.begin() + 4);
- }
- bool hasDuplicateDigits(const string &number) {
- vector<bool> seen(10, false);
- for (const auto c : number) {
- if (seen[c - '0']) return true;
- seen[c - '0'] = true;
- }
- return false;
- }
- bool isNumeric(const string &s) {
- return all_of(s.begin(), s.end(), [](char c){ return isdigit(c); });
- }
string getUserGuess() {- string getUserGuess(istream &in = cin, ostream &out = cout) {
- string guess;
cout << "Enter your guess (a 4-digit number with non-repeating digits): ";cin >> guess;- out << "Enter your guess (a 4-digit number with non-repeating digits): ";
- in >> guess;
- return guess;
- }
- pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
- int correctNumbers = 0;
- int correctPosition = 0;
- for (int i = 0; i < 4; ++i) {
- if (randomNumber[i] == userGuess[i]) {
- correctNumbers++;
- correctPosition++;
- } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
- correctNumbers++;
- }
- }
- return make_pair(correctNumbers, correctPosition);
- }
int _main() {// Seed rand since random_shuffle _probably_ uses it.srand(static_cast<unsigned>(time(0)));string randomNumber = generateRandomNumber();- int acceptGuess(const string &expected, istream &in = cin, ostream &out = cout) {
- int attempts = 0;
- while (true) {
string userGuess = getUserGuess();- string userGuess = getUserGuess(in, out);
- if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
cout << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;- out << "Invalid input. Please enter a 4-digit number with non-repeating digits." << endl;
- continue;
- }
auto [corNum, corPos] = checkGuess(randomNumber, userGuess);cout << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;- auto [corNum, corPos] = checkGuess(expected, userGuess);
- out << "Correct numbers: " << corNum << " Correct position: " << corPos << endl;
- attempts++;
- if (corPos == 4) {
cout << "Congratulations! You guessed the number " << randomNumber << " correctly in " << attempts << " attempts!" << endl;- out << "Congratulations! You guessed the number " << expected << " correctly in " << attempts << " attempts!" << endl;
- break;
- }
- }
- return attempts;
- }
- int _main(istream &in = cin, ostream &out = cout) {
- // Seed rand since random_shuffle _probably_ uses it.
- srand(static_cast<unsigned>(time(0)));
- string randomNumber = generateRandomNumber();
- acceptGuess(randomNumber, in, out);
- return 0;
- }
// Your game should output as followed in the description // Not sure how to put a test case for these type of codes // this feels a lot like it might've been an interesting // homework assignment or something, given the presence of a main // function and the lack of main functions in how kata/kumite work. // I'm still for it for the half-interesting aspect, but definitely // a bit odd. using std::istringstream; using std::ostringstream; Describe(Tests) { It(Guess_Test_1) { istringstream in("8903"); ostringstream out; acceptGuess("8903", in, out); Assert::That(out.str(), Equals("Enter your guess (a 4-digit number with non-repeating digits): " "Correct numbers: 4 Correct position: 4\n" "Congratulations! You guessed the number 8903 correctly in 1 attempts!\n")); } It(Guess_Test_2) { istringstream in("8903\n9012"); ostringstream out; acceptGuess("9012", in, out); Assert::That(out.str(), Equals("Enter your guess (a 4-digit number with non-repeating digits): " "Correct numbers: 2 Correct position: 0\n" "Enter your guess (a 4-digit number with non-repeating digits): " "Correct numbers: 4 Correct position: 4\n" "Congratulations! You guessed the number 9012 correctly in 2 attempts!\n")); } };
- // Your game should output as followed in the description
- // Not sure how to put a test case for these type of codes
- // this feels a lot like it might've been an interesting
- // homework assignment or something, given the presence of a main
- // function and the lack of main functions in how kata/kumite work.
- // I'm still for it for the half-interesting aspect, but definitely
// a bit odd.- // a bit odd.
- using std::istringstream;
- using std::ostringstream;
- Describe(Tests) {
- It(Guess_Test_1) {
- istringstream in("8903");
- ostringstream out;
- acceptGuess("8903", in, out);
- Assert::That(out.str(), Equals("Enter your guess (a 4-digit number with non-repeating digits): "
- "Correct numbers: 4 Correct position: 4\n"
- "Congratulations! You guessed the number 8903 correctly in 1 attempts!\n"));
- }
- It(Guess_Test_2) {
- istringstream in("8903\n9012");
- ostringstream out;
- acceptGuess("9012", in, out);
- Assert::That(out.str(), Equals("Enter your guess (a 4-digit number with non-repeating digits): "
- "Correct numbers: 2 Correct position: 0\n"
- "Enter your guess (a 4-digit number with non-repeating digits): "
- "Correct numbers: 4 Correct position: 4\n"
- "Congratulations! You guessed the number 9012 correctly in 2 attempts!\n"));
- }
- };
its a static amout of numbers, so it can be just a list, duh
get_fibs=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
get_fibs=lambda a=1,b=1:[(t:=a,a:=b,b:=b+t)[0]for _ in range(100)]- get_fibs=[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]
import codewars_test as test from solution import get_fibs @test.describe("Example") def test_group(): @test.it("test case") def test_case(): fibs = get_fibs test.assert_equals(fibs, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075]) test.assert_equals(len(fibs), 100)
- import codewars_test as test
- from solution import get_fibs
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
fibs = get_fibs()- fibs = get_fibs
- test.assert_equals(fibs, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, 20365011074, 32951280099, 53316291173, 86267571272, 139583862445, 225851433717, 365435296162, 591286729879, 956722026041, 1548008755920, 2504730781961, 4052739537881, 6557470319842, 10610209857723, 17167680177565, 27777890035288, 44945570212853, 72723460248141, 117669030460994, 190392490709135, 308061521170129, 498454011879264, 806515533049393, 1304969544928657, 2111485077978050, 3416454622906707, 5527939700884757, 8944394323791464, 14472334024676221, 23416728348467685, 37889062373143906, 61305790721611591, 99194853094755497, 160500643816367088, 259695496911122585, 420196140727489673, 679891637638612258, 1100087778366101931, 1779979416004714189, 2880067194370816120, 4660046610375530309, 7540113804746346429, 12200160415121876738, 19740274219868223167, 31940434634990099905, 51680708854858323072, 83621143489848422977, 135301852344706746049, 218922995834555169026, 354224848179261915075])
- test.assert_equals(len(fibs), 100)
def twin_sum_solutions(array) duplicates = [] find_duplicates(array).map{|e| e*2 } end def find_duplicates(array) duplicates = [] array.each do |element| duplicates << element if array.count(element) > 1 end duplicates.uniq end
- def twin_sum_solutions(array)
- duplicates = []
- find_duplicates(array).map{|e| e*2 }
- end
- def find_duplicates(array)
- duplicates = []
- array.each do |element|
- duplicates << element if array.count(element) > 1
- end
- duplicates.uniq
- end
function Sum-OfPositive($NumberArray) { [Func[int,bool]]$Delegate = {param($v); return $v -ge 0} [Linq.Enumerable]::Sum([Linq.Enumerable]::Where([int[]]$NumberArray,$Delegate)) }
- function Sum-OfPositive($NumberArray)
- {
($NumberArray | ?{$_ -gt 0} | Measure -sum).Sum- [Func[int,bool]]$Delegate = {param($v); return $v -ge 0}
- [Linq.Enumerable]::Sum([Linq.Enumerable]::Where([int[]]$NumberArray,$Delegate))
- }
const chai = require("chai"); const assert = chai.assert; describe("XOR", ()=> { it("some information about badal sherpuja", ()=> { assert.strictEqual(XOR(true,false), true); assert.strictEqual(XOR(false,true), true); }); it("my name is nikhil danekhu", ()=> { assert.strictEqual(XOR(true,true), false); assert.strictEqual(XOR(false,false), false); }); let fileContent = require("fs").readFileSync("/workspace/solution.txt", "utf8"); it("would you like to earn money", ()=> { assert.isTrue(fileContent.length <= 35, `Your solution is ${fileContent.length-35} character${fileContent.length!=36 ? 's' : ''} over the length limit`); }) it("handsome !=(=) and ==(=)", ()=> { assert.isFalse(fileContent.includes('!='), 'Your solution uses !=(=)'); assert.isFalse(fileContent.includes('=='), 'Your solution uses ==(=)'); }) it("should pass random tests", ()=> { let a; let b; for(let i=0;i<420;i++) { a = Math.random() < 0.5; b = Math.random() < 0.5; assert.strictEqual(XOR(a,b), a != b); } }) });
- const chai = require("chai");
- const assert = chai.assert;
- describe("XOR", ()=> {
it("should return true for different bools", ()=> {- it("some information about badal sherpuja", ()=> {
- assert.strictEqual(XOR(true,false), true);
- assert.strictEqual(XOR(false,true), true);
- });
it("should return false for same bools", ()=> {- it("my name is nikhil danekhu", ()=> {
- assert.strictEqual(XOR(true,true), false);
- assert.strictEqual(XOR(false,false), false);
- });
- let fileContent = require("fs").readFileSync("/workspace/solution.txt", "utf8");
it("should be shorter than 30 characters", ()=> {- it("would you like to earn money", ()=> {
- assert.isTrue(fileContent.length <= 35, `Your solution is ${fileContent.length-35} character${fileContent.length!=36 ? 's' : ''} over the length limit`);
- })
it("shouldn't use !=(=) and ==(=)", ()=> {- it("handsome !=(=) and ==(=)", ()=> {
- assert.isFalse(fileContent.includes('!='), 'Your solution uses !=(=)');
- assert.isFalse(fileContent.includes('=='), 'Your solution uses ==(=)');
- })
- it("should pass random tests", ()=> {
- let a; let b;
- for(let i=0;i<420;i++) {
- a = Math.random() < 0.5;
- b = Math.random() < 0.5;
- assert.strictEqual(XOR(a,b), a != b);
- }
- })
- });
Added a small benchmark, the output can be shown with "cargo test -- --nocapture" locally.
pub fn sort_desc(mut n: u64) -> u64 { let mut digit_counts = [0; 10]; while n > 0 { let digit = (n % 10) as usize; digit_counts[digit] += 1; n /= 10; } let mut result = 0; let mut fac = 1; for digit in 0..10 { for _ in 0..digit_counts[digit] { result += (digit as u64) * fac; fac *= 10; } } result } use std::iter::repeat; pub fn sort_desc_it(mut n: u64) -> u64 { let mut digit_counts = [0; 10]; while n > 0 { digit_counts[n as usize % 10] += 1; n /= 10; } digit_counts .into_iter() .enumerate() .rev() .flat_map(|(digit, count)| repeat(digit as u64).take(count)) .fold(0, |result, digit| result * 10 + digit) } use std::time::Instant; fn benchmark<F>(func: F, input: u64, iterations: u32) -> (u64, f64) where F: Fn(u64) -> u64, { let mut total_duration = 0.0; let mut result = 0; for _ in 0..iterations { let start = Instant::now(); result = func(input); let duration = start.elapsed(); total_duration += duration.as_secs_f64(); } let avg_duration = total_duration / iterations as f64; (result, avg_duration) }
- pub fn sort_desc(mut n: u64) -> u64 {
- let mut digit_counts = [0; 10];
- while n > 0 {
- let digit = (n % 10) as usize;
- digit_counts[digit] += 1;
- n /= 10;
- }
- let mut result = 0;
- let mut fac = 1;
- for digit in 0..10 {
- for _ in 0..digit_counts[digit] {
- result += (digit as u64) * fac;
- fac *= 10;
- }
- }
- result
- }
- use std::iter::repeat;
pub fn sort_desc(mut n: u64) -> u64 {- pub fn sort_desc_it(mut n: u64) -> u64 {
- let mut digit_counts = [0; 10];
- while n > 0 {
- digit_counts[n as usize % 10] += 1;
- n /= 10;
- }
- digit_counts
- .into_iter()
- .enumerate()
- .rev()
- .flat_map(|(digit, count)| repeat(digit as u64).take(count))
- .fold(0, |result, digit| result * 10 + digit)
}- }
- use std::time::Instant;
- fn benchmark<F>(func: F, input: u64, iterations: u32) -> (u64, f64)
- where
- F: Fn(u64) -> u64,
- {
- let mut total_duration = 0.0;
- let mut result = 0;
- for _ in 0..iterations {
- let start = Instant::now();
- result = func(input);
- let duration = start.elapsed();
- total_duration += duration.as_secs_f64();
- }
- let avg_duration = total_duration / iterations as f64;
- (result, avg_duration)
- }
#[cfg(test)] mod tests { use super::*; fn do_test(number: u64, expected: u64) { let actual = sort_desc(number); assert_eq!( actual, expected, "For {} expected {} but got {}", number, expected, actual ); } #[test] fn test_sort_desc() { do_test(0, 0); do_test(1, 1); do_test(12, 21); do_test(123, 321); do_test(310, 310); do_test(12034, 43210); do_test(12345, 54321); do_test(123456789, 987654321); do_test(1234567890, 9876543210); } #[test] fn benchmark_max_num() { let numbers = vec![ 1234567890, 9876543210, 112233445566778899, 987654321987654321, ]; let iterations = 1000; for (ix, func) in [sort_desc, sort_desc_it].iter().enumerate() { if ix == 0 { println!("sort_desc:"); } else { println!("sort_desc_it:"); } for &number in &numbers { let (result, avg_duration) = benchmark(func, number, iterations); println!( "Max number formed from {}: {} (average time: {:.9} seconds)", number, result, avg_duration ); } } } }
- #[cfg(test)]
- mod tests {
use super::sort_desc;- use super::*;
- fn do_test(number: u64, expected: u64) {
- let actual = sort_desc(number);
assert_eq!(actual, expected, "For {} expected {} but got {}", number, expected, actual);- assert_eq!(
- actual, expected,
- "For {} expected {} but got {}",
- number, expected, actual
- );
- }
- #[test]
- fn test_sort_desc() {
- do_test(0, 0);
- do_test(1, 1);
- do_test(12, 21);
- do_test(123, 321);
- do_test(310, 310);
- do_test(12034, 43210);
- do_test(12345, 54321);
- do_test(123456789, 987654321);
- do_test(1234567890, 9876543210);
- }
- #[test]
- fn benchmark_max_num() {
- let numbers = vec![
- 1234567890,
- 9876543210,
- 112233445566778899,
- 987654321987654321,
- ];
- let iterations = 1000;
- for (ix, func) in [sort_desc, sort_desc_it].iter().enumerate() {
- if ix == 0 {
- println!("sort_desc:");
- } else {
- println!("sort_desc_it:");
- }
- for &number in &numbers {
- let (result, avg_duration) = benchmark(func, number, iterations);
- println!(
- "Max number formed from {}: {} (average time: {:.9} seconds)",
- number, result, avg_duration
- );
- }
- }
- }
- }