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.
simplify
fn verify_sum(name_one: &str, name_two: &str) -> bool { sum(name_one) == sum(name_two) } fn sum(name: &str) -> u32 { name.bytes().map(u32::from).sum() }
- fn verify_sum(name_one: &str, name_two: &str) -> bool {
- sum(name_one) == sum(name_two)
- }
- fn sum(name: &str) -> u32 {
let mut prod = 0;for b in name.bytes(){prod += b as u32;}prod- name.bytes().map(u32::from).sum()
- }
from timeit import timeit from math import floor import numpy as np # 1. Can you implement another optimised function (sum_even_numbers2) using purely native python? # Can you timeit to show it's faster and describe why it is. # Can you also state if there are any pros or cons to how it is implemented? # 2. Can you implement another optimised function (sum_even_numbers3) using third party packages? # Can you timeit to show it's faster and describe why it is. # Can you also state if there are any pros or cons to how it is implemented? def sum_even_numbers1(numbers: list[int]) -> int: total = 0 for num in numbers: if floor(num/2) == num/2: total += num return total # Floating point calculation is not required # Only one division required # float required 32byte, memory heavy def sum_even_numbers2(numbers: list[int]) -> int: total = 0 for num in numbers: if num%2 == 0: total += num return total # needs to call that function from memory def sum_even_numbers3(numbers: list[int]) -> int: return sum(filter(lambda x: x > 0 and x%2 == 0, numbers)) # generators are often faster def sum_even_numbers4(numbers: list[int]) -> int: return sum(num for num in numbers if not num%2) # generators without if statements are almost always faster def sum_even_numbers5(numbers: list[int]) -> int: return sum(n * (not n&1) for n in numbers) # the max() function of python is written in C, and maths are even faster than a loop def sum_even_numbers6(numbers: list[int]) -> int: maxi = max(numbers) return maxi//2 * (maxi//2 + 1) # why keeping two divisions? def sum_even_numbers7(numbers: list[int]) -> int: return (maxi := max(numbers) >> 1) * (maxi + 1) # Simplicity: The code is simple and easy to understand. # No Floating-Point Operations: This implementation avoids floating-point operations, # which can introduce rounding errors. def sum_even_numbers8(numbers: list[int]) -> int: total = 0 for num in numbers: if num % 2 == 0: total += num return total # Conciseness: NumPy provides concise and expressive syntax for array operations. # Vectorized Operations: NumPy operations are usually vectorized, making them efficient # for large datasets. def sum_even_numbers9(numbers: list[int]) -> int: return np.sum(np.array(numbers)[np.array(numbers) % 2 == 0]) # bitwise AND operation checks if the least significant bit is 0, indicating an even number def sum_even_numbers10(numbers): return sum(x for x in numbers if not x & 1)
- from timeit import timeit
- from math import floor
- import numpy as np
- # 1. Can you implement another optimised function (sum_even_numbers2) using purely native python?
- # Can you timeit to show it's faster and describe why it is.
- # Can you also state if there are any pros or cons to how it is implemented?
- # 2. Can you implement another optimised function (sum_even_numbers3) using third party packages?
- # Can you timeit to show it's faster and describe why it is.
- # Can you also state if there are any pros or cons to how it is implemented?
- def sum_even_numbers1(numbers: list[int]) -> int:
- total = 0
- for num in numbers:
- if floor(num/2) == num/2:
- total += num
- return total
- # Floating point calculation is not required
- # Only one division required
- # float required 32byte, memory heavy
- def sum_even_numbers2(numbers: list[int]) -> int:
- total = 0
- for num in numbers:
- if num%2 == 0:
- total += num
- return total
- # needs to call that function from memory
- def sum_even_numbers3(numbers: list[int]) -> int:
- return sum(filter(lambda x: x > 0 and x%2 == 0, numbers))
- # generators are often faster
- def sum_even_numbers4(numbers: list[int]) -> int:
- return sum(num for num in numbers if not num%2)
- # generators without if statements are almost always faster
- def sum_even_numbers5(numbers: list[int]) -> int:
- return sum(n * (not n&1) for n in numbers)
- # the max() function of python is written in C, and maths are even faster than a loop
- def sum_even_numbers6(numbers: list[int]) -> int:
- maxi = max(numbers)
- return maxi//2 * (maxi//2 + 1)
- # why keeping two divisions?
- def sum_even_numbers7(numbers: list[int]) -> int:
return (maxi := max(numbers) >> 1) * (maxi + 1)- return (maxi := max(numbers) >> 1) * (maxi + 1)
- # Simplicity: The code is simple and easy to understand.
- # No Floating-Point Operations: This implementation avoids floating-point operations,
- # which can introduce rounding errors.
- def sum_even_numbers8(numbers: list[int]) -> int:
- total = 0
- for num in numbers:
- if num % 2 == 0:
- total += num
- return total
- # Conciseness: NumPy provides concise and expressive syntax for array operations.
- # Vectorized Operations: NumPy operations are usually vectorized, making them efficient
- # for large datasets.
- def sum_even_numbers9(numbers: list[int]) -> int:
- return np.sum(np.array(numbers)[np.array(numbers) % 2 == 0])
- # bitwise AND operation checks if the least significant bit is 0, indicating an even number
- def sum_even_numbers10(numbers):
- return sum(x for x in numbers if not x & 1)
import codewars_test as test import solution # or from solution import example @test.describe("Example") def test_group(): @test.it("test case") def test_case(): iterations = 100 list_length = 100000 input_data = [i for i in range(0, list_length)] print(timeit(lambda: sum_even_numbers1(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers2(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers3(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers4(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers5(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers6(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers7(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers8(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers9(input_data), number=iterations)) print(timeit(lambda: sum_even_numbers10(input_data), number=iterations))
- import codewars_test as test
- import solution # or from solution import example
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- iterations = 100
- list_length = 100000
- input_data = [i for i in range(0, list_length)]
- print(timeit(lambda: sum_even_numbers1(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers2(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers3(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers4(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers5(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers6(input_data), number=iterations))
print(timeit(lambda: sum_even_numbers7(input_data), number=iterations))- print(timeit(lambda: sum_even_numbers7(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers8(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers9(input_data), number=iterations))
- print(timeit(lambda: sum_even_numbers10(input_data), number=iterations))
public static class Kata { public static int SameCase(char a, char b) { return char.IsLetter(a) && char.IsLetter(b) ? (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0) : -1; } }
- public static class Kata
- {
- public static int SameCase(char a, char b)
- {
if (!char.IsLetter(a) || !char.IsLetter(b)){return -1;}return (char.IsUpper(a) == char.IsUpper(b)) ? 1 : 0;- return char.IsLetter(a) && char.IsLetter(b) ? (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0) : -1;
- }
- }