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.
Correction pour agir comme la question originale decrit.
fn closest_to_zero(ints: &[i32]) -> i32 { *ints.iter().min_by_key(|x| (x.abs(), x.is_negative())).unwrap_or(&0) }
- fn closest_to_zero(ints: &[i32]) -> i32 {
*ints.iter().min_by_key(|x| x.abs()).unwrap_or(&0)- *ints.iter().min_by_key(|x| (x.abs(), x.is_negative())).unwrap_or(&0)
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_simple() { assert_eq!(closest_to_zero(&[7, 5, 9, 1, 4]), 1); } #[test] fn test_negative() { assert_eq!(closest_to_zero(&[7, -4, -3, -12, 5, 9, -2, 4]), -2); } #[test] fn test_same() { assert_eq!(closest_to_zero(&[-5, -5]), -5); } #[test] fn test_empty() { assert_eq!(closest_to_zero(&[]), 0); } #[test] fn test_with_zero() { assert_eq!(closest_to_zero(&[-5, 0, 1, 5]), 0); } #[test] fn test_same_distance() { assert_eq!(closest_to_zero(&[-5, -1, 1, 5]), 1); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_simple() {
- assert_eq!(closest_to_zero(&[7, 5, 9, 1, 4]), 1);
- }
- #[test]
- fn test_negative() {
- assert_eq!(closest_to_zero(&[7, -4, -3, -12, 5, 9, -2, 4]), -2);
- }
- #[test]
- fn test_same() {
- assert_eq!(closest_to_zero(&[-5, -5]), -5);
- }
- #[test]
- fn test_empty() {
- assert_eq!(closest_to_zero(&[]), 0);
- }
- #[test]
- fn test_with_zero() {
- assert_eq!(closest_to_zero(&[-5, 0, 1, 5]), 0);
- }
- #[test]
- fn test_same_distance() {
assert_eq!(closest_to_zero(&[-5, -1, 1, 5]), -1);- assert_eq!(closest_to_zero(&[-5, -1, 1, 5]), 1);
- }
- }
- Converted to python 🐍
import string PUNCTUATION = string.punctuation def test_password(password): # password must be no less than 8 characters long c1 = len(password) >= 8 # password must have at least 1 capital letter c2 = len([1 for t in password if t == t.upper()]) >= 1 # password must have at least 1 number c3 = len([1 for t in password if t.isdigit()]) >= 1 # password must have at least 1 special character c4 = len([1 for t in password if t in PUNCTUATION]) >= 1 # Return True if all conditions are True return all([c1,c2,c3,c4])
fn test_password(password: &str) -> bool {password.len() >= 8&& password.chars().any(|c| c.is_ascii_uppercase())&& password.chars().any(|c| c.is_ascii_digit())&& password.chars().any(|c| "!\"#$%&'()*+'-./;:<>=?".contains(c))}- import string
- PUNCTUATION = string.punctuation
- def test_password(password):
- # password must be no less than 8 characters long
- c1 = len(password) >= 8
- # password must have at least 1 capital letter
- c2 = len([1 for t in password if t == t.upper()]) >= 1
- # password must have at least 1 number
- c3 = len([1 for t in password if t.isdigit()]) >= 1
- # password must have at least 1 special character
- c4 = len([1 for t in password if t in PUNCTUATION]) >= 1
- # Return True if all conditions are True
- return all([c1,c2,c3,c4])
import codewars_test as test from solution import test_password # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case: True") def test_case(): test.assert_equals(test_password("Pas$word1"), True) test.assert_equals(test_password("ALLTH3TH!NGS"), True) @test.it("test case: False") def test_case(): test.assert_equals(test_password("password"), False) test.assert_equals(test_password("WodT$m1"), False)
#[test]fn test_good() {assert!(test_password("Pas$word1"));assert!(test_password("ALLTH3TH!NGS"));}- import codewars_test as test
- from solution import test_password
#[test]fn test_bad() {assert!(!test_password("password"));assert!(!test_password("WodT$m1"));}- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case: True")
- def test_case():
- test.assert_equals(test_password("Pas$word1"), True)
- test.assert_equals(test_password("ALLTH3TH!NGS"), True)
- @test.it("test case: False")
- def test_case():
- test.assert_equals(test_password("password"), False)
- test.assert_equals(test_password("WodT$m1"), False)
Two can play at that game 🦀 (admittedly less straightforward since Rust is more explicit around operations and potential errors)
fn reverse_int(n: u128) -> u128 { n.to_string().chars().rev().collect::<String>().parse().unwrap() }
def reverse_int(n):return int(str(n)[::-1])- fn reverse_int(n: u128) -> u128 {
- n.to_string().chars().rev().collect::<String>().parse().unwrap()
- }
#[test] fn tests() { let test_samples = [ (776, 677), (12345, 54321), (3121534312, 2134351213), (726376882009597984841891, 198148489795900288673627), ]; for (s, e) in test_samples { assert_eq!(reverse_int(s), e) } }
import codewars_test as testfrom solution import reverse_int- #[test]
- fn tests() {
- let test_samples = [
- (776, 677),
- (12345, 54321),
- (3121534312, 2134351213),
- (726376882009597984841891, 198148489795900288673627),
- ];
@test.describe("Example")def test_group():@test.it("test case")def test_case():test_samples = ((776,677),(12345,54321),(3121534312,2134351213),(726376882009597984841891,198148489795900288673627),)for s, e in test_samples:test.assert_equals(reverse_int(s), e)- for (s, e) in test_samples {
- assert_eq!(reverse_int(s), e)
- }
- }
- Converted to Python 🐍
class Kumite: def __init__(self, name1, name2): self.name1 = name1 self.name2 = name2 def solve(self): return None if not self.name1 or not self.name2 else ( sum([ord(t)for t in self.name1]) == sum([ord(t) for t in self.name2]))
class Kata {- class Kumite:
- def __init__(self, name1, name2):
- self.name1 = name1
- self.name2 = name2
public static String verifySum(String nameOne, String nameTwo) {if(nameOne == null || nameTwo == null) {return "NULL";}int sum = 0, lengthOne = nameOne.length(), lengthTwo = nameTwo.length();for(int i = 0; i < lengthOne; i++) {char c = nameOne.charAt(i);sum += c > 96 ? c - 32 : c;}for(int i = 0; i < lengthTwo; i++) {char c = nameTwo.charAt(i);sum -= c > 96 ? c - 32 : c;}return sum == 0 ? "TRUE" : "FALSE";}}- def solve(self):
- return None if not self.name1 or not self.name2 else (
- sum([ord(t)for t in self.name1]) == sum([ord(t) for t in self.name2]))
import codewars_test as test from solution import Kumite # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(Kumite("Sebastian", "Patricia").solve(), False) test.assert_equals(Kumite("Anna", "Nana").solve(), True) test.assert_equals(Kumite("seraph776", None).solve(), None) test.assert_equals(Kumite("", "codewarz").solve(), None)
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- import codewars_test as test
public class SolutionTest {@Testpublic void testName() {assertEquals("FALSE", Kata.verifySum("Sebastian", "Patricia"));assertEquals("TRUE", Kata.verifySum("Anna", "Nana"));assertEquals("NULL", Kata.verifySum("John", null));}}- from solution import Kumite
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(Kumite("Sebastian", "Patricia").solve(), False)
- test.assert_equals(Kumite("Anna", "Nana").solve(), True)
- test.assert_equals(Kumite("seraph776", None).solve(), None)
- test.assert_equals(Kumite("", "codewarz").solve(), None)
class IsPrimeNumber: """Returns True if n is a prime number, False otherwise""" def __init__(self, n): self.n = n def calculate(self): if self.n > 1: for i in range(2, int(self.n ** 0.5) + 1): if self.n % i == 0: return False return True # If no divisors found, it's prime else: return False class Fizz: """Returns True if n is divisible by 3, False otherwise""" def __init__(self, n): self.n = n def calculate(self): return self.n % 3 == 0 class Buzz: """Returns True if n is divisible by 5, False otherwise""" def __init__(self, n): self.n = n def calculate(self): return self.n % 5 == 0 class FizzBuzz: """Returns True if n is divisible by 3 and 5, False otherwise""" def __init__(self, n): self.n = n def calculate(self): return Fizz(self.n).calculate() and Buzz(self.n).calculate() class CodeWarKata776: """Executes the Fizz, Bizz, FizzBuzz Prime sequence.""" def __init__(self, n): self.n = n def calculate_prime(self): return IsPrimeNumber(self.n).calculate() def calculate_fizz(self): return Fizz(self.n).calculate() def calculate_buzz(self): return Buzz(self.n).calculate() def calculate_fizzbuzz(self): return FizzBuzz(self.n).calculate() def execute(self): if IsPrimeNumber(self.n).calculate(): return 'Prime' if FizzBuzz(self.n).calculate(): return 'FizzBuzz' elif Fizz(self.n).calculate(): return 'Fizz' elif Buzz(self.n).calculate(): return 'Buzz' else: return self.n
- class IsPrimeNumber:
- """Returns True if n is a prime number, False otherwise"""
- def __init__(self, n):
- self.n = n
- def calculate(self):
pass- if self.n > 1:
- for i in range(2, int(self.n ** 0.5) + 1):
- if self.n % i == 0:
- return False
- return True # If no divisors found, it's prime
- else:
- return False
- class Fizz:
- """Returns True if n is divisible by 3, False otherwise"""
- def __init__(self, n):
- self.n = n
- def calculate(self):
pass- return self.n % 3 == 0
- class Buzz:
- """Returns True if n is divisible by 5, False otherwise"""
- def __init__(self, n):
- self.n = n
- def calculate(self):
pass- return self.n % 5 == 0
- class FizzBuzz:
- """Returns True if n is divisible by 3 and 5, False otherwise"""
- def __init__(self, n):
- self.n = n
- def calculate(self):
pass- return Fizz(self.n).calculate() and Buzz(self.n).calculate()
- class CodeWarKata776:
- """Executes the Fizz, Bizz, FizzBuzz Prime sequence."""
- def __init__(self, n):
- self.n = n
- def calculate_prime(self):
pass- return IsPrimeNumber(self.n).calculate()
- def calculate_fizz(self):
pass- return Fizz(self.n).calculate()
- def calculate_buzz(self):
pass- return Buzz(self.n).calculate()
- def calculate_fizzbuzz(self):
pass- return FizzBuzz(self.n).calculate()
- def execute(self):
pass- if IsPrimeNumber(self.n).calculate():
- return 'Prime'
- if FizzBuzz(self.n).calculate():
- return 'FizzBuzz'
- elif Fizz(self.n).calculate():
- return 'Fizz'
- elif Buzz(self.n).calculate():
- return 'Buzz'
- else:
- return self.n
- Changed
for loop
tolist comprehension
def morse_code(msg): """Morse Code function""" output = '' morse_dict = { 'a': '.- ', 'b': '-... ', 'c': '-.-. ', 'd': '-.. ', 'e': '. ', 'f': '..-. ', 'g': '--. ', 'h': '.... ', 'i': '.. ', 'j': '.--- ', 'k': '-.- ', 'l': '.-.. ', 'm': '-- ', 'n': '-. ', 'o': '--- ', 'p': '.--. ', 'q': '--.- ', 'r': '.-. ', 's': '... ', 't': '- ', 'u': '..- ', 'v': '...- ', 'w': '.-- ', 'x': '-..- ', 'y': '-.-- ', 'z': '--.. ', '0': '----- ', '1': '.---- ', '2': '..--- ', '3': '...-- ', '4': '....- ', '5': '..... ', '6': '-.... ', '7': '--... ', '8': '---.. ', '9': '----. ', '.': '.-.-.- ', ',': '--..-- ', "'": '.----. ', '?': '..--.. ', ':': '---... ', '-': '-....- ', '/': '-..-. ', '[': '-.--. ', '(': '-.--. ', ']': '-.--.- ', ')': '-.--.- ', '"': '.-..-. ', '_': '..--.- ', '=': '-...- ', '+': '.-.-. ', '@': '.--.-. ', '!': '-.-.-- ', ' ': '/ ' } return output.join([morse_dict.get(letter) for letter in msg]).rstrip()
'''Morse Code function - remove match case and use dict for better time complexity'''- def morse_code(msg):
- """Morse Code function"""
- output = ''
- morse_dict = {
- 'a': '.- ',
- 'b': '-... ',
- 'c': '-.-. ',
- 'd': '-.. ',
- 'e': '. ',
- 'f': '..-. ',
- 'g': '--. ',
- 'h': '.... ',
- 'i': '.. ',
- 'j': '.--- ',
- 'k': '-.- ',
- 'l': '.-.. ',
- 'm': '-- ',
- 'n': '-. ',
- 'o': '--- ',
- 'p': '.--. ',
- 'q': '--.- ',
- 'r': '.-. ',
- 's': '... ',
- 't': '- ',
- 'u': '..- ',
- 'v': '...- ',
- 'w': '.-- ',
- 'x': '-..- ',
- 'y': '-.-- ',
- 'z': '--.. ',
- '0': '----- ',
- '1': '.---- ',
- '2': '..--- ',
- '3': '...-- ',
- '4': '....- ',
- '5': '..... ',
- '6': '-.... ',
- '7': '--... ',
- '8': '---.. ',
- '9': '----. ',
- '.': '.-.-.- ',
- ',': '--..-- ',
- "'": '.----. ',
- '?': '..--.. ',
- ':': '---... ',
- '-': '-....- ',
- '/': '-..-. ',
- '[': '-.--. ',
- '(': '-.--. ',
- ']': '-.--.- ',
- ')': '-.--.- ',
- '"': '.-..-. ',
- '_': '..--.- ',
- '=': '-...- ',
- '+': '.-.-. ',
- '@': '.--.-. ',
- '!': '-.-.-- ',
- ' ': '/ '
- }
for letter in msg:output += output.join([morse_dict.get(letter)])return output.rstrip()- return output.join([morse_dict.get(letter) for letter in msg]).rstrip()
import codewars_test as test from solution import morse_code # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(morse_code('sos'), '... --- ...' ) test.assert_equals(morse_code('hello world'),'.... . .-.. .-.. --- / .-- --- .-. .-.. -..') test.assert_equals(morse_code('you passed!'), '-.-- --- ..- / .--. .- ... ... . -.. -.-.--' ) test.assert_equals(morse_code('1+1=2'), '.---- .-.-. .---- -...- ..---' ) test.assert_equals(morse_code('(1+2)=5?'), '-.--. .---- .-.-. ..--- -.--.- -...- ..... ..--..' ) test.assert_equals(morse_code('seraph776@codewarz.com'), '... . .-. .- .--. .... --... --... -.... .--.-. -.-. --- -.. . .-- .- .-. --.. .-.-.- -.-. --- --' ) test.assert_equals(morse_code('lorem ipsum is simply dummy text of the printing and typesetting industry.'), '.-.. --- .-. . -- / .. .--. ... ..- -- / .. ... / ... .. -- .--. .-.. -.-- / -.. ..- -- -- -.-- / - . -..- - / --- ..-. / - .... . / .--. .-. .. -. - .. -. --. / .- -. -.. / - -.-- .--. . ... . - - .. -. --. / .. -. -.. ..- ... - .-. -.-- .-.-.-' )
- import codewars_test as test
- from solution import morse_code
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
def test_case():print(morse_code('sos'))test.assert_equals(morse_code('sos'), '... --- ...' )print(morse_code('hello world'))test.assert_equals(morse_code('hello world'),'.... . .-.. .-.. --- / .-- --- .-. .-.. -..')print(morse_code('you passed!'))test.assert_equals(morse_code('you passed!'), '-.-- --- ..- / .--. .- ... ... . -.. -.-.--' )print(morse_code('1+1=2'))test.assert_equals(morse_code('1+1=2'), '.---- .-.-. .---- -...- ..---' )print(morse_code('(1+2)=5?'))test.assert_equals(morse_code('(1+2)=5?'), '-.--. .---- .-.-. ..--- -.--.- -...- ..... ..--..' )print(morse_code('seraph776@codewarz.com'))test.assert_equals(morse_code('seraph776@codewarz.com'), '... . .-. .- .--. .... --... --... -.... .--.-. -.-. --- -.. . .-- .- .-. --.. .-.-.- -.-. --- --' )print(morse_code('lorem ipsum is simply dummy text of the printing and typesetting industry.'))- def test_case():
- test.assert_equals(morse_code('sos'), '... --- ...' )
- test.assert_equals(morse_code('hello world'),'.... . .-.. .-.. --- / .-- --- .-. .-.. -..')
- test.assert_equals(morse_code('you passed!'), '-.-- --- ..- / .--. .- ... ... . -.. -.-.--' )
- test.assert_equals(morse_code('1+1=2'), '.---- .-.-. .---- -...- ..---' )
- test.assert_equals(morse_code('(1+2)=5?'), '-.--. .---- .-.-. ..--- -.--.- -...- ..... ..--..' )
- test.assert_equals(morse_code('seraph776@codewarz.com'), '... . .-. .- .--. .... --... --... -.... .--.-. -.-. --- -.. . .-- .- .-. --.. .-.-.- -.-. --- --' )
- test.assert_equals(morse_code('lorem ipsum is simply dummy text of the printing and typesetting industry.'),
'.-.. --- .-. . -- / .. .--. ... ..- -- / .. ... / ... .. -- .--. .-.. -.-- / -.. ..- -- -- -.-- / - . -..- - / --- ..-. / - .... . / .--. .-. .. -. - .. -. --. / .- -. -.. / - -.-- .--. . ... . - - .. -. --. / .. -. -.. ..- ... - .-. -.-- .-.-.-' )- '.-.. --- .-. . -- / .. .--. ... ..- -- / .. ... / ... .. -- .--. .-.. -.-- / -.. ..- -- -- -.-- / - . -..- - / --- ..-. / - .... . / .--. .-. .. -. - .. -. --. / .- -. -.. / - -.-- .--. . ... . - - .. -. --. / .. -. -.. ..- ... - .-. -.-- .-.-.-' )