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.
The &mut self parameters make this a lot easier to work with. That might just have been a skill issue on my part for the typstate implementation. However it does provide some benefits over the straightforward implenetation.
This API can no longer distinguish between the states, so the .secret()
method has to return an Option
, and the login/logout functions can be called even if you're already logged in/logged out.
#[test] fn test() { let mut account = Account::new("password123".into(), "I don't like pizza".into()); assert_eq!(account.secret(), None); account.login("password123"); assert_eq!(account.secret(), Some("I don't like pizza")); account.logout(); assert_eq!(account.secret(), None); account.login("supersecret"); assert_eq!(account.secret(), None); }
- #[test]
- fn test() {
let logged_out = Account::new("password123".into(), "I don't like pizza".into());// logged_out.secret(); // does not compilelet login_attempt = logged_out.login("password123");assert!(login_attempt.is_ok());let logged_in = login_attempt.unwrap();assert_eq!(logged_in.secret(), "I don't like pizza");let logged_out = logged_in.logout();let login_attempt = logged_out.login("supersecret");assert!(login_attempt.is_err());- let mut account = Account::new("password123".into(), "I don't like pizza".into());
- assert_eq!(account.secret(), None);
- account.login("password123");
- assert_eq!(account.secret(), Some("I don't like pizza"));
- account.logout();
- assert_eq!(account.secret(), None);
- account.login("supersecret");
- assert_eq!(account.secret(), None);
- }
I kinda like this one, since the first element guaranteeing happens outside the fold. But the fold line is a bit long.
fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> { let mut iter = nums.iter().copied(); let [min, max] = [iter.next()?;2]; let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max))); Some(minmax) }
- fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> {
- let mut iter = nums.iter().copied();
let [mut smallest, mut largest] = [iter.next()?;2];for num in iter {(smallest, largest) = (smallest.min(num), largest.max(num));}Some((smallest, largest))- let [min, max] = [iter.next()?;2];
- let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max)));
- Some(minmax)
- }
Fancy typestate solution. Makes questionable API decisions, but it's a chance to use cool features.
use std::marker::PhantomData; #[derive(Debug, PartialEq, Eq)] enum AngleType { Acute, Right, Obtuse } #[derive(Debug, PartialEq, Eq)] enum SideType { Equilateral, Isoceles, Scalene } struct Complete; struct Incomplete; struct Triangle<T> { angles: [u32; 3], phantom: PhantomData<T> } 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 } } } impl Triangle<Complete> { fn new(a: u32, b: u32, c: u32) -> Self { Self { angles: [a, b, c], phantom: PhantomData::<Complete> } } } 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] } }
fn other_angle(a: u32, b: u32) -> Option<u32> {180_u32.checked_sub(a).and_then(|n| n.checked_sub(b)).filter(|&n| n != 0)- use std::marker::PhantomData;
- #[derive(Debug, PartialEq, Eq)]
- enum AngleType {
- Acute,
- Right,
- Obtuse
- }
- #[derive(Debug, PartialEq, Eq)]
- enum SideType {
- Equilateral,
- Isoceles,
- Scalene
- }
- struct Complete;
- struct Incomplete;
- struct Triangle<T> {
- angles: [u32; 3],
- phantom: PhantomData<T>
- }
- 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
- }
- }
- }
- impl Triangle<Complete> {
- fn new(a: u32, b: u32, c: u32) -> Self {
- Self { angles: [a, b, c], phantom: PhantomData::<Complete> }
- }
- }
fn other_angle_unchecked(a: u32, b: u32) -> u32 {180 - a - b- 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]
- }
- }
#[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 known assert_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); }
- #[test]
- fn test() {
assert_eq!(other_angle(30, 60), Some(90));assert_eq!(other_angle(60, 60), Some(60));assert_eq!(other_angle(43, 78), Some(59));assert_eq!(other_angle(10, 20), Some(150));assert_eq!(other_angle(10, 20), Some(150));assert_eq!(other_angle(90, 90), None);assert_eq!(other_angle(60, 150), None);- let triangle1 = Triangle::<Incomplete>::new(30, 60);
- let triangle2 = Triangle::<Complete>::new(60, 60, 60);
assert_eq!(other_angle_unchecked(30, 60), 90);assert_eq!(other_angle_unchecked(60, 60), 60);assert_eq!(other_angle_unchecked(43, 78), 59);assert_eq!(other_angle_unchecked(10, 20), 150);- assert_eq!(triangle1.other_angle(), 90);
- // assert_eq!(triangle2.other_angle(), 90); // does not compile because other angle is already known
- assert_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);
- }
def reverse_string(string): return string[::-1] # Example usage print(reverse_string("Hello, World!")) # "!dlroW, Hello" print(reverse_string("")) # "" print(reverse_string("a")) # "a" print(reverse_string("ab")) # "ba" print(reverse_string("abc")) # "cba"
reverse_string = lambda string: string[::-1]- def reverse_string(string):
- return string[::-1]
- # Example usage
- print(reverse_string("Hello, World!")) # "!dlroW, Hello"
- print(reverse_string("")) # ""
- print(reverse_string("a")) # "a"
- print(reverse_string("ab")) # "ba"
- print(reverse_string("abc")) # "cba"
:) (i don't know how to code)
public static class Kata { public static int SameCase(char a, char b) => !(char.IsLetter(a) && char.IsLetter(b)) ? -1 : char.IsUpper(a) == char.IsUpper(b) ? 1 : 0; }
- public static class Kata
- {
public static int SameCase(char a, char b)=> !(char.IsLetter(a) && char.IsLetter(b)) ? -1: char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;- public static int SameCase(char a, char b) => !(char.IsLetter(a) && char.IsLetter(b)) ? -1 : char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
- }
basically refactored
def max_sequence(arr): # Code to find maximum sum of subarray sum_b = sum(arr) for i in range(len(arr)): for k in range(len(arr)-i+1): sum_c = sum(arr[k:k+i]) if sum_c > sum_b: sum_b = sum_c return(sum_b)
- def max_sequence(arr):
- # Code to find maximum sum of subarray
l_a = len(arr)- sum_b = sum(arr)
for i in range(l_a):for k in range(l_a-i+1):- for i in range(len(arr)):
- for k in range(len(arr)-i+1):
- sum_c = sum(arr[k:k+i])
- if sum_c > sum_b: sum_b = sum_c
- return(sum_b)