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.
Fixed the end of the string problem. Now, the char array has a double null terminator character to denote that it has reached the end of the array.
#include <stdlib.h> #include <string.h> /* Heap allocate the string and return it. */ char* remove_string(const char *input, const int n) { if (n <= 0 || input == NULL) return NULL; for (int c = 1; !(*input == '\0' && *(input + 1) == '\0') && (c < n); (*input == '\0') ? c++ : c, input++); return (*input == '\0') ? NULL : strdup(input); }
- #include <stdlib.h>
- #include <string.h>
- /*
- Heap allocate the string and return it.
- */
- char* remove_string(const char *input, const int n)
- {
- if (n <= 0 || input == NULL) return NULL;
for (size_t i = 0, len = 0; i < (size_t)n; i++){input += len;if ((len = strlen(input) + 1) == 1) return NULL;}return strdup(input);- for (int c = 1; !(*input == '\0' && *(input + 1) == '\0') && (c < n); (*input == '\0') ? c++ : c, input++);
- return (*input == '\0') ? NULL : strdup(input);
- }
#include <criterion/criterion.h> #include <stdlib.h> #include <string.h> char* remove_string(const char *input, const int n); void tester(const char *input, size_t index, const char *exp) { char *result = remove_string(input, index); if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result); else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result); free(result); } Test(remove_string, should_pass_all_the_tests_provided) { tester("hello\0world\0\0", 1, "hello"); tester("well\0done\0\0", 2, "done"); tester("you\0are\0close\0\0", 3, "close"); tester("you\0almost\0finished\0the\0kumite\0\0", 5, "kumite"); tester("not\0the\0last\0string\0\0", 3, "last"); tester("edge\0case\0\0", 0, NULL); tester("another\0edge\0case\0\0", 4, NULL); }
- #include <criterion/criterion.h>
- #include <stdlib.h>
- #include <string.h>
- char* remove_string(const char *input, const int n);
- void tester(const char *input, size_t index, const char *exp)
- {
- char *result = remove_string(input, index);
- if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result);
- else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result);
- free(result);
- }
- Test(remove_string, should_pass_all_the_tests_provided)
- {
tester("hello\0world\0", 1, "hello");tester("well\0done\0", 2, "done");tester("you\0are\0close\0", 3, "close");tester("you\0almost\0finished\0the\0kumite\0", 5, "kumite");tester("not\0the\0last\0string\0", 3, "last");tester("edge\0case\0", 0, NULL);tester("another\0edge\0case\0", 4, NULL);- tester("hello\0world\0\0", 1, "hello");
- tester("well\0done\0\0", 2, "done");
- tester("you\0are\0close\0\0", 3, "close");
- tester("you\0almost\0finished\0the\0kumite\0\0", 5, "kumite");
- tester("not\0the\0last\0string\0\0", 3, "last");
- tester("edge\0case\0\0", 0, NULL);
- tester("another\0edge\0case\0\0", 4, NULL);
- }
import random import time def not_even(n): pizza_time = time.ctime() if n % 2 == 0: return 'its not even', pizza_time else: return 'its still not even', pizza_time return pizza_time n = random.randint(0, 10000) print(not_even(n))
odd_even=lambda n:int(__import__('requests').get(f'https://api.isevenapi.xyz/api/iseven/{n}/').json()['iseven'])- import random
- import time
- def not_even(n):
- pizza_time = time.ctime()
- if n % 2 == 0:
- return 'its not even', pizza_time
- else:
- return 'its still not even', pizza_time
- return pizza_time
- n = random.randint(0, 10000)
- print(not_even(n))
# TODO Write tests import solution # or from solution import example import time # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): if time.ctime() == time.ctime(): test.assert_equals('its not even', 'its not even') or test.assert_equals('its still even', 'its still even')
import codewars_test as testfrom solution import odd_even- # TODO Write tests
- import solution # or from solution import example
- import time
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
@test.it("test case 1: Testing Odd Numbers")def test_case():for n in range(1, 10, 2):test.assert_equals(odd_even(n), 0)@test.it("test case 2: Testing Even Numbers")- @test.it("test case")
- def test_case():
for n in range(0, 10, 2):test.assert_equals(odd_even(n), 1)- if time.ctime() == time.ctime():
- test.assert_equals('its not even', 'its not even') or test.assert_equals('its still even', 'its still even')
fn mean(x: &[f64]) -> f64 { let x_len_float = x.len() as f64; return x.iter().sum::<f64>() / x_len_float; }
fn mean(x: &[u64]) -> u64 {x.iter().sum::<u64>() / x.len() as u64- fn mean(x: &[f64]) -> f64 {
- let x_len_float = x.len() as f64;
- return x.iter().sum::<f64>() / x_len_float;
- }
#[test] fn test_positive_integers() { assert_eq!(mean(&[4_f64, 8_f64, 4_f64, 8_f64]), 6_f64); } #[test] fn test_floats() { let actual = mean(&[0.48, 3.28, -5.77, 3.49]); let expected = 0.3700000; const EPSILON: f64 = 1e-7; let diff = (actual-expected).abs(); assert!(diff < EPSILON, "Difference between the computed mean: {} and expected: {} is greater than {}", actual, expected, EPSILON); }
- #[test]
fn test() {assert_eq!(mean(&[4, 8, 4, 8]), 6);- fn test_positive_integers() {
- assert_eq!(mean(&[4_f64, 8_f64, 4_f64, 8_f64]), 6_f64);
- }
- #[test]
- fn test_floats() {
- let actual = mean(&[0.48, 3.28, -5.77, 3.49]);
- let expected = 0.3700000;
- const EPSILON: f64 = 1e-7;
- let diff = (actual-expected).abs();
- assert!(diff < EPSILON, "Difference between the computed mean: {} and expected: {} is greater than {}", actual, expected, EPSILON);
- }
less char -> Short-Circuiting instead of ternary
Probably would be considered less clean than the parent,
but also faster by avoiding streams.
#include <string> #include <vector> auto split(const std::string& str, char sep) { auto result = std::vector<std::string>{}; std::string::size_type pos = 0, pos2 = 0; while ((pos2 = str.find(sep, pos)) != std::string::npos) { result.push_back(str.substr(pos, pos2 - pos)); pos = pos2 + 1; } result.push_back(str.substr(pos)); return result; }
- #include <string>
#include <sstream>- #include <vector>
- auto split(const std::string& str, char sep) {
- auto result = std::vector<std::string>{};
auto stream = std::stringstream(str);auto buffer = std::string{};while (std::getline(stream, buffer, sep)) result.emplace_back(buffer);- std::string::size_type pos = 0, pos2 = 0;
- while ((pos2 = str.find(sep, pos)) != std::string::npos) {
- result.push_back(str.substr(pos, pos2 - pos));
- pos = pos2 + 1;
- }
- result.push_back(str.substr(pos));
- return result;
- }
Given an array of n
distinct integers in the range [0, n]
, find the missing integer.
#include <vector> #include <numeric> using namespace std; int missingNumber(vector<int> nums) { return (nums.size() * (nums.size() + 1))/2 - accumulate(nums.begin(), nums.end(), 0); }
- #include <vector>
#include <algorithm>- #include <numeric>
- using namespace std;
- int missingNumber(vector<int> nums) {
nums.push_back(nums.size());for (int i = 0; i < static_cast<int>(nums.size()); i++) {if (find(nums.begin(), nums.end(), i) == nums.end()) return i;}return nums.size() - 1;- return (nums.size() * (nums.size() + 1))/2 - accumulate(nums.begin(), nums.end(), 0);
- }