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.
extension String { func insert(_ value: String, interval: Int) -> String { guard interval > 0 else { return self } var new = Array(self).map(String.init) (1..<count).filter { $0 % interval == 0 } .reversed() .forEach { new.insert(value, at: $0) } return new.joined() } }
- extension String {
- func insert(_ value: String, interval: Int) -> String {
guard interval > 0 && interval < count else { return self }let chars = Array(self)var result = ""var i = 0while i+interval<count {result.append(String(chars[i..<min(count,i+interval)])+value)i += interval}result.append(String(chars[i..<min(count,i+interval)]))return result- guard interval > 0 else { return self }
- var new = Array(self).map(String.init)
- (1..<count).filter { $0 % interval == 0 }
- .reversed()
- .forEach { new.insert(value, at: $0) }
- return new.joined()
- }
- }
const tab = (f, a, b, h) => Array.from({length:Math.ceil((b-a)/h)+1}, (v,i) => f(a+(i*h))).reduce((a,c) => a+=c,0)
const tab = (f, a, b, h) => {let sum = 0;for (let x = a; x <= b; x +=h){sum += f(x); //Filippov Ilya}return sum}- const tab = (f, a, b, h) => Array.from({length:Math.ceil((b-a)/h)+1}, (v,i) => f(a+(i*h))).reduce((a,c) => a+=c,0)
#include <stddef.h> static const char *nums[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; const char *digit_to_text(unsigned digit) { return digit < 10u ? nums[digit] : NULL; }
- #include <stddef.h>
const char* digit_to_text(unsigned digit){static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};if(digit > 9u){return NULL;}return nums[digit];- static const char *nums[] = {
- "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
- };
- const char *digit_to_text(unsigned digit) {
- return digit < 10u ? nums[digit] : NULL;
- }
#include <criterion/criterion.h> const char *digit_to_text(int digit); Test(digit_to_text, Test_Cases) { cr_assert_eq(digit_to_text(-1), NULL); cr_assert_str_eq(digit_to_text(0), "zero"); cr_assert_str_eq(digit_to_text(1), "one"); cr_assert_str_eq(digit_to_text(2), "two"); cr_assert_str_eq(digit_to_text(3), "three"); cr_assert_str_eq(digit_to_text(4), "four"); cr_assert_str_eq(digit_to_text(5), "five"); cr_assert_str_eq(digit_to_text(6), "six"); cr_assert_str_eq(digit_to_text(7), "seven"); cr_assert_str_eq(digit_to_text(8), "eight"); cr_assert_str_eq(digit_to_text(9), "nine"); cr_assert_eq(digit_to_text(10), NULL); }
- #include <criterion/criterion.h>
const char* digit_to_text(int digit);- const char *digit_to_text(int digit);
Test(digit_to_text, should_pass_all_the_tests_provided) {- Test(digit_to_text, Test_Cases)
- {
- cr_assert_eq(digit_to_text(-1), NULL);
- cr_assert_str_eq(digit_to_text(0), "zero");
- cr_assert_str_eq(digit_to_text(1), "one");
- cr_assert_str_eq(digit_to_text(2), "two");
- cr_assert_str_eq(digit_to_text(3), "three");
- cr_assert_str_eq(digit_to_text(4), "four");
- cr_assert_str_eq(digit_to_text(5), "five");
- cr_assert_str_eq(digit_to_text(6), "six");
- cr_assert_str_eq(digit_to_text(7), "seven");
- cr_assert_str_eq(digit_to_text(8), "eight");
- cr_assert_str_eq(digit_to_text(9), "nine");
- cr_assert_eq(digit_to_text(10), NULL);
- }
Now in C!
#include <stdint.h> #include <stdbool.h> #define MAX_ANGLE 180u typedef enum { ACUTE, RIGHT, OBTUSE, } AngleType; typedef enum { EQUILATERAL, ISOSCELES, SCALENE, } SideType; typedef struct { union { struct { uint32_t a; uint32_t b; uint32_t c; }; uint32_t angles[3]; }; } Triangle; bool TriangleIsValid(const Triangle* triangle); AngleType TriangleGetAngleType(const Triangle* triangle); SideType TriangleGetSideType(const Triangle* triangle); uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2); bool TriangleIsValid(const Triangle* triangle) { return triangle->a + triangle->b + triangle->c == 180u; } AngleType TriangleGetAngleType(const Triangle* triangle) { for(unsigned i = 0u; i < 3u; i++) { if(triangle->angles[i] > 90u) { return OBTUSE; } if(triangle->angles[i] == 90u) { return RIGHT; } } return ACUTE; } SideType TriangleGetSideType(const Triangle* triangle) { uint32_t a = triangle->a; uint32_t b = triangle->b; uint32_t c = triangle->c; if(a == b && b == c) { return EQUILATERAL; } if(a == b || b == c || c == a) { return ISOSCELES; } return SCALENE; } uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2) { return 180u - angle1 - angle2; }
use std::marker::PhantomData;- #include <stdint.h>
- #include <stdbool.h>
#[derive(Debug, PartialEq, Eq)]enum AngleType {Acute,Right,Obtuse}#[derive(Debug, PartialEq, Eq)]enum SideType {Equilateral,Isoceles,Scalene}struct Complete;struct Incomplete;- #define MAX_ANGLE 180u
struct Triangle<T> {angles: [u32; 3],phantom: PhantomData<T>- typedef enum
- {
- ACUTE,
- RIGHT,
- OBTUSE,
- } AngleType;
- typedef enum
- {
- EQUILATERAL,
- ISOSCELES,
- SCALENE,
- } SideType;
- typedef struct
- {
- union
- {
- struct
- {
- uint32_t a;
- uint32_t b;
- uint32_t c;
- };
- uint32_t angles[3];
- };
- } Triangle;
- bool TriangleIsValid(const Triangle* triangle);
- AngleType TriangleGetAngleType(const Triangle* triangle);
- SideType TriangleGetSideType(const Triangle* triangle);
- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
- bool TriangleIsValid(const Triangle* triangle)
- {
- return triangle->a + triangle->b + triangle->c == 180u;
- }
impl<T> Triangle<T> {fn angle_type(&self) -> AngleType {for angle in self.angles {if angle > 90 {return AngleType::Obtuse;}if angle == 90 {return AngleType::Right;}}AngleType::Acute}fn side_type(&self) -> SideType {let [a, b, c] = self.angles;if a == b && b == c {SideType::Equilateral} else if a == b || b == c || c == a {SideType::Isoceles} else {SideType::Scalene}}- AngleType TriangleGetAngleType(const Triangle* triangle)
- {
- for(unsigned i = 0u; i < 3u; i++)
- {
- if(triangle->angles[i] > 90u)
- {
- return OBTUSE;
- }
- if(triangle->angles[i] == 90u)
- {
- return RIGHT;
- }
- }
- return ACUTE;
- }
impl Triangle<Complete> {fn new(a: u32, b: u32, c: u32) -> Self {Self { angles: [a, b, c], phantom: PhantomData::<Complete> }}- SideType TriangleGetSideType(const Triangle* triangle)
- {
- uint32_t a = triangle->a;
- uint32_t b = triangle->b;
- uint32_t c = triangle->c;
- if(a == b && b == c)
- {
- return EQUILATERAL;
- }
- if(a == b || b == c || c == a)
- {
- return ISOSCELES;
- }
- return SCALENE;
- }
impl Triangle<Incomplete> {fn new(a: u32, b: u32) -> Self {Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> }}fn other_angle(&self) -> u32 {self.angles[2]}- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
- {
- return 180u - angle1 - angle2;
- }
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example. #include <criterion/criterion.h> #include <stdint.h> #include <stdbool.h> typedef enum { ACUTE, RIGHT, OBTUSE, } AngleType; typedef enum { EQUILATERAL, ISOSCELES, SCALENE, } SideType; typedef struct { union { struct { uint32_t a; uint32_t b; uint32_t c; }; uint32_t angles[3]; }; } Triangle; bool TriangleIsValid(const Triangle* triangle); AngleType TriangleGetAngleType(const Triangle* triangle); SideType TriangleGetSideType(const Triangle* triangle); uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2); Test(TriangleIsValid, should_pass_all_the_tests_provided) { Triangle good_triangle = {.angles = {60u, 60u, 60u}}; Triangle bad_triangle = {.angles = {90u, 50u, 50u}}; cr_assert_eq(TriangleIsValid(&good_triangle), true); cr_assert_eq(TriangleIsValid(&bad_triangle), false); } Test(TriangleGetAngleType, should_pass_all_the_tests_provided) { Triangle acute_triangle = {.angles = {70u, 50u, 60u}}; Triangle right_triangle = {.angles = {45u, 45u, 90u}}; Triangle obtuse_triangle = {.angles = {120u, 20u, 40u}}; cr_assert_eq(TriangleGetAngleType(´_triangle), ACUTE); cr_assert_eq(TriangleGetAngleType(&right_triangle), RIGHT); cr_assert_eq(TriangleGetAngleType(&obtuse_triangle), OBTUSE); } Test(TriangleGetSideType, should_pass_all_the_tests_provided) { Triangle equilateral_triangle = {.angles = {60u, 60u, 60u}}; Triangle isosceles_triangle = {.angles = {45u, 45u, 90u}}; Triangle scalene_triangle = {.angles = {120u, 20u, 40u}}; cr_assert_eq(TriangleGetSideType(&equilateral_triangle), EQUILATERAL); cr_assert_eq(TriangleGetSideType(&isosceles_triangle), ISOSCELES); cr_assert_eq(TriangleGetSideType(&scalene_triangle), SCALENE); } Test(TriangleGetThirdAngle, should_pass_all_the_tests_provided) { cr_assert_eq(TriangleGetThirdAngle(120u, 53u), 7u); cr_assert_eq(TriangleGetThirdAngle(21u, 62u), 97u); }
#[test]fn test() {let triangle1 = Triangle::<Incomplete>::new(30, 60);let triangle2 = Triangle::<Complete>::new(60, 60, 60);assert_eq!(triangle1.other_angle(), 90);// assert_eq!(triangle2.other_angle(), 90); // does not compile because other angle is already knownassert_eq!(triangle1.angle_type(), AngleType::Right);assert_eq!(triangle2.angle_type(), AngleType::Acute);assert_eq!(triangle1.side_type(), SideType::Scalene);assert_eq!(triangle2.side_type(), SideType::Equilateral);- // TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
- #include <criterion/criterion.h>
- #include <stdint.h>
- #include <stdbool.h>
- typedef enum
- {
- ACUTE,
- RIGHT,
- OBTUSE,
- } AngleType;
- typedef enum
- {
- EQUILATERAL,
- ISOSCELES,
- SCALENE,
- } SideType;
- typedef struct
- {
- union
- {
- struct
- {
- uint32_t a;
- uint32_t b;
- uint32_t c;
- };
- uint32_t angles[3];
- };
- } Triangle;
- bool TriangleIsValid(const Triangle* triangle);
- AngleType TriangleGetAngleType(const Triangle* triangle);
- SideType TriangleGetSideType(const Triangle* triangle);
- uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
- Test(TriangleIsValid, should_pass_all_the_tests_provided) {
- Triangle good_triangle = {.angles = {60u, 60u, 60u}};
- Triangle bad_triangle = {.angles = {90u, 50u, 50u}};
- cr_assert_eq(TriangleIsValid(&good_triangle), true);
- cr_assert_eq(TriangleIsValid(&bad_triangle), false);
- }
- Test(TriangleGetAngleType, should_pass_all_the_tests_provided) {
- Triangle acute_triangle = {.angles = {70u, 50u, 60u}};
- Triangle right_triangle = {.angles = {45u, 45u, 90u}};
- Triangle obtuse_triangle = {.angles = {120u, 20u, 40u}};
- cr_assert_eq(TriangleGetAngleType(´_triangle), ACUTE);
- cr_assert_eq(TriangleGetAngleType(&right_triangle), RIGHT);
- cr_assert_eq(TriangleGetAngleType(&obtuse_triangle), OBTUSE);
- }
- Test(TriangleGetSideType, should_pass_all_the_tests_provided) {
- Triangle equilateral_triangle = {.angles = {60u, 60u, 60u}};
- Triangle isosceles_triangle = {.angles = {45u, 45u, 90u}};
- Triangle scalene_triangle = {.angles = {120u, 20u, 40u}};
- cr_assert_eq(TriangleGetSideType(&equilateral_triangle), EQUILATERAL);
- cr_assert_eq(TriangleGetSideType(&isosceles_triangle), ISOSCELES);
- cr_assert_eq(TriangleGetSideType(&scalene_triangle), SCALENE);
- }
- Test(TriangleGetThirdAngle, should_pass_all_the_tests_provided) {
- cr_assert_eq(TriangleGetThirdAngle(120u, 53u), 7u);
- cr_assert_eq(TriangleGetThirdAngle(21u, 62u), 97u);
- }
'1' and '2' are technically of the same case + it seems more intuitive TBH
using System; public static class Kata { public static int SameCase(char a, char b) { if (!char.IsLetter(a) && !char.IsLetter(b)) return 1; if (!char.IsLetter(a) || !char.IsLetter(b)) return -1; return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0; } }
- using System;
- public static class Kata
- {
- public static int SameCase(char a, char b)
- {
- if (!char.IsLetter(a) && !char.IsLetter(b))
- return 1;
- if (!char.IsLetter(a) || !char.IsLetter(b))
return -1;- return -1;
- return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0;
- }
- }
namespace Solution { using NUnit.Framework; using System; [TestFixture] public class SolutionTest { [Test] public void TrueTests() { Assert.AreEqual(1, Kata.SameCase('a', 'u')); Assert.AreEqual(1, Kata.SameCase('A', 'U')); Assert.AreEqual(1, Kata.SameCase('Q', 'P')); Assert.AreEqual(1, Kata.SameCase('w', 'y')); Assert.AreEqual(1, Kata.SameCase('c', 'm')); Assert.AreEqual(1, Kata.SameCase('N', 'W')); Assert.AreEqual(1, Kata.SameCase('1', '2')); } [Test] public void FalseTests() { Assert.AreEqual(0, Kata.SameCase('a', 'U')); Assert.AreEqual(0, Kata.SameCase('A', 'u')); Assert.AreEqual(0, Kata.SameCase('Q', 'p')); Assert.AreEqual(0, Kata.SameCase('w', 'Y')); Assert.AreEqual(0, Kata.SameCase('c', 'M')); Assert.AreEqual(0, Kata.SameCase('N', 'w')); } [Test] public void NotLetters() { Assert.AreEqual(-1, Kata.SameCase('a', '*')); Assert.AreEqual(-1, Kata.SameCase('A', '%')); Assert.AreEqual(-1, Kata.SameCase('Q', '1')); Assert.AreEqual(-1, Kata.SameCase('w', '-')); Assert.AreEqual(-1, Kata.SameCase('c', '8')); Assert.AreEqual(-1, Kata.SameCase('N', ':')); } [Test] public void RandomTest() { string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890~/*-+:.,><.#@!&"; Random rand = new Random(); char char1 = 'a'; char char2 = 'a'; for (int i = 0; i < 200; i++) { char1 = chars[rand.Next(0,76)]; char2 = chars[rand.Next(0,76)]; Assert.AreEqual(SameCaseSolution(char1,char2), Kata.SameCase(char1, char2)); } } private static int SameCaseSolution(char a, char b) { if (!char.IsLetter(a) && !char.IsLetter(b)) return 1; if (!char.IsLetter(a) || !char.IsLetter(b)) return -1; if ((a >= 97) == (b >= 97)) return 1; return 0; } } }
- namespace Solution {
- using NUnit.Framework;
- using System;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void TrueTests()
- {
- Assert.AreEqual(1, Kata.SameCase('a', 'u'));
- Assert.AreEqual(1, Kata.SameCase('A', 'U'));
- Assert.AreEqual(1, Kata.SameCase('Q', 'P'));
- Assert.AreEqual(1, Kata.SameCase('w', 'y'));
- Assert.AreEqual(1, Kata.SameCase('c', 'm'));
- Assert.AreEqual(1, Kata.SameCase('N', 'W'));
- Assert.AreEqual(1, Kata.SameCase('1', '2'));
- }
- [Test]
- public void FalseTests()
- {
- Assert.AreEqual(0, Kata.SameCase('a', 'U'));
- Assert.AreEqual(0, Kata.SameCase('A', 'u'));
- Assert.AreEqual(0, Kata.SameCase('Q', 'p'));
- Assert.AreEqual(0, Kata.SameCase('w', 'Y'));
- Assert.AreEqual(0, Kata.SameCase('c', 'M'));
- Assert.AreEqual(0, Kata.SameCase('N', 'w'));
- }
- [Test]
- public void NotLetters()
- {
- Assert.AreEqual(-1, Kata.SameCase('a', '*'));
- Assert.AreEqual(-1, Kata.SameCase('A', '%'));
- Assert.AreEqual(-1, Kata.SameCase('Q', '1'));
- Assert.AreEqual(-1, Kata.SameCase('w', '-'));
- Assert.AreEqual(-1, Kata.SameCase('c', '8'));
- Assert.AreEqual(-1, Kata.SameCase('N', ':'));
- }
- [Test]
- public void RandomTest() {
- string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz1234567890~/*-+:.,><.#@!&";
- Random rand = new Random();
- char char1 = 'a';
- char char2 = 'a';
- for (int i = 0; i < 200; i++) {
- char1 = chars[rand.Next(0,76)];
- char2 = chars[rand.Next(0,76)];
- Assert.AreEqual(SameCaseSolution(char1,char2), Kata.SameCase(char1, char2));
- }
- }
- private static int SameCaseSolution(char a, char b) {
- if (!char.IsLetter(a) && !char.IsLetter(b)) return 1;
- if (!char.IsLetter(a) || !char.IsLetter(b)) return -1;
- if ((a >= 97) == (b >= 97)) return 1;
- return 0;
- }
- }
- }
made it fit on one line :)
max_sequence = lambda arr :max([sum(arr)] + [sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)])
def max_sequence(arr):# Code to find maximum sum of subarrayreturn max([sum(arr)] +[sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)])- max_sequence = lambda arr :max([sum(arr)] + [sum(arr[j:j+i]) for i in range(len(arr)) for j in range(len(arr) - i + 1)])