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.
If you could not tell, I am very inexperienced in Ruby.
My easiest golf Kumite to beat by far.
def f r,n;r**r<n ?-1: r<2?1:(n-1).to_s(r).chars.map{|x|x.to_i+1}.reduce{|c,x|10*c+x}end
using System;using System.Linq;using System.Collections.Generic;public class NumbersFinder{public static int Generator(int size , int position){if ( Math.Pow( size , size ) < position )return -1;List<string> arr = new List<string>();for (int i = 0; i < size; ++i)arr.Add((i+1).ToString());List<string> newArr;while (arr.Count != Math.Pow(size,size)){newArr = new List<string>();for (int j = 0; j < arr.Count; j++)for (int i = 0; i < size; i++)newArr.Add(arr[j]+(i+1));arr.Clear();arr.AddRange(newArr);}return Convert.ToInt32(arr[position - 1]);}}- def f r,n;r**r<n ?-1: r<2?1:(n-1).to_s(r).chars.map{|x|x.to_i+1}.reduce{|c,x|10*c+x}end
# From Ruby 3.0, RSpec is used under the hood. # See https://rspec.info/ # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well. describe "Example" do it "should return the sum" do expect(f(1, 1)).to eq(1) expect(f(5, 2595)).to eq(51445) expect(f(7, 200017)).to eq(2573176) expect(f(4 , 257)).to eq(-1) # The following is still supported, but new tests should now use them. # Test.assert_equals(add(1, 1), 2) end end
using NUnit.Framework;using System;using System.Collections.Generic;[TestFixture]public class SolutionTest{[Test]public void MyTest(){Assert.AreEqual(1 , NumbersFinder.Generator(1 , 1));Assert.AreEqual(51445 , NumbersFinder.Generator(5 , 2595));Assert.AreEqual(2573176 , NumbersFinder.Generator(7 , 200017));Assert.AreEqual(-1 , NumbersFinder.Generator(4 , 257));}}- # From Ruby 3.0, RSpec is used under the hood.
- # See https://rspec.info/
- # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
- describe "Example" do
- it "should return the sum" do
- expect(f(1, 1)).to eq(1)
- expect(f(5, 2595)).to eq(51445)
- expect(f(7, 200017)).to eq(2573176)
- expect(f(4 , 257)).to eq(-1)
- # The following is still supported, but new tests should now use them.
- # Test.assert_equals(add(1, 1), 2)
- end
- end
module Format where import Data.Bits ((.|.)) import Data.Char (chr) f :: Integer -> [Char] f n = f' [chr $ (.|.) 48 $ fromIntegral $ mod n 10] (div n 10) where f' :: [Char] -> Integer -> [Char] f' sol 0 = sol f' acc n = f'' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) where f'' :: [Char] -> Integer -> [Char] f'' sol 0 = sol f'' acc n = f''' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) where f''' :: [Char] -> Integer -> [Char] f''' sol 0 = sol f''' acc n = f' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) $ (:) ',' acc) (div n 10)
def f(n):G = 3s = str(n)r = len(s) % Gdef make(s):if r:yield s[:r]for i in range(r, len(s), G):yield s[i:i+G]return ','.join(make(s))- module Format where
- import Data.Bits ((.|.))
- import Data.Char (chr)
- f :: Integer -> [Char]
- f n = f' [chr $ (.|.) 48 $ fromIntegral $ mod n 10] (div n 10) where
- f' :: [Char] -> Integer -> [Char]
- f' sol 0 = sol
- f' acc n = f'' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) where
- f'' :: [Char] -> Integer -> [Char]
- f'' sol 0 = sol
- f'' acc n = f''' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) where
- f''' :: [Char] -> Integer -> [Char]
- f''' sol 0 = sol
- f''' acc n = f' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) $ (:) ',' acc) (div n 10)
module FormatSpec where -- Tests can be written using Hspec http://hspec.github.io/ -- Replace this with your own tests. import Test.Hspec import Format -- `spec` of type `Spec` must exist spec :: Spec spec = do describe "Tests" $ do it "basic testing" $ do (f 100) `shouldBe` ("100" :: String) (f 1000) `shouldBe` ("1,000" :: String) (f 12345) `shouldBe` ("12,345" :: String) (f 123456789) `shouldBe` ("123,456,789" :: String) (f 10000000000) `shouldBe` ("10,000,000,000" :: String) -- the following line is optional for 8.2 main = hspec spec
import codewars_test as test# TODO Write testsimport solution # or from solution import example- module FormatSpec where
- -- Tests can be written using Hspec http://hspec.github.io/
- -- Replace this with your own tests.
test.assert_equals(f(100), '100')test.assert_equals(f(1000), '1,000')test.assert_equals(f(12345), '12,345')test.assert_equals(f(123456789), '123,456,789')test.assert_equals(f(10000000000), '10,000,000,000')- import Test.Hspec
- import Format
@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(1 + 1, 2)- -- `spec` of type `Spec` must exist
- spec :: Spec
- spec = do
- describe "Tests" $ do
- it "basic testing" $ do
- (f 100) `shouldBe` ("100" :: String)
- (f 1000) `shouldBe` ("1,000" :: String)
- (f 12345) `shouldBe` ("12,345" :: String)
- (f 123456789) `shouldBe` ("123,456,789" :: String)
- (f 10000000000) `shouldBe` ("10,000,000,000" :: String)
- -- the following line is optional for 8.2
- main = hspec spec
The same thing as my C solution, just more Pythonic because it is written in Python.
def reverse_int(int_reverse: int) -> int: div_mod: (int, int) = divmod(int_reverse, 10) reverse_int: int = div_mod[1] while div_mod[0]: div_mod = divmod(div_mod[0], 10) reverse_int *= 10 reverse_int += div_mod[1] return reverse_int
public class Algorithms {public static int reverseInt(int n) {int reversed = 0;while(n != 0){reversed = reversed * 10 + (n % 10);n /= 10;}return reversed;}}- def reverse_int(int_reverse: int) -> int:
- div_mod: (int, int) = divmod(int_reverse, 10)
- reverse_int: int = div_mod[1]
- while div_mod[0]:
- div_mod = divmod(div_mod[0], 10)
- reverse_int *= 10
- reverse_int += div_mod[1]
- return reverse_int
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): # Why do so many people not put Python testcases into this nice format? @test.it("test case") def test_case(): test.assert_equals(reverse_int(12345), 54321) test.assert_equals(reverse_int(0), 0)
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
// TODO: Replace examples and use TDD development by writing your own testspublic class SolutionTest {@Testpublic void reverseIntTest() {assertEquals(54321, Algorithms.reverseInt(12345));}}- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- # Why do so many people not put Python testcases into this nice format?
- @test.it("test case")
- def test_case():
- test.assert_equals(reverse_int(12345), 54321)
- test.assert_equals(reverse_int(0), 0)
A new approach to a trivial problem.
Also added a few testcases.
(* *) let (<<) f g x = f @@ g x ;; let platforms xs = let rec platforms' (acc, xs) = match xs with | x::[] -> acc x | x::xs'-> platforms' ((+) @@ acc x << abs << (-) x, xs') | [] -> failwith "only exists to make the compiler shut up" in match xs with | [] | _::[] -> 0 | x::xs' -> platforms' (abs << (-) x, xs') ;;
using System;public static class Kata{public static int CalculatingTheAmountEnergy(int[] coordinates){// here's your code}}- (* *)
- let (<<) f g x = f @@ g x ;;
- let platforms xs =
- let rec platforms' (acc, xs) =
- match xs with
- | x::[] -> acc x
- | x::xs'-> platforms' ((+) @@ acc x << abs << (-) x, xs')
- | [] -> failwith "only exists to make the compiler shut up"
- in
- match xs with
- | []
- | _::[] -> 0
- | x::xs' -> platforms' (abs << (-) x, xs')
- ;;
(* TODO: replace with your own tests, these are just how-to examples. * OUnit Spec example: * See http://ounit.forge.ocamlcore.org/api-ounit-2.0.0 for documentation *) module Tests = struct open OUnit let suite = [ "Suite Name" >::: [ "ascending the hill" >:: (fun _ -> assert_equal (platforms [1; 5; 10]) 9 ); "coming back down" >:: (fun _ -> assert_equal (platforms [10; 5; 1]) 9 ); "tutorial level" >:: (fun _ -> assert_equal (platforms [1; 5; 10; 3; 20]) 33 ); "plateau" >:: (fun _ -> assert_equal (platforms [1; 4; 9; 16; 16; 16; 16; 16; 9; 4; 1]) 30 ); "game journalist difficulty" >:: (fun _ -> assert_equal (platforms [0; 0; 1]) 1 ); "under development" >:: (fun _ -> assert_equal (platforms []) 0 ); "early access" >:: (fun _ -> assert_equal (platforms [2310986]) 0 ) ] ] ;; end
using NUnit.Framework;using System;- (* TODO: replace with your own tests, these are just how-to examples.
- * OUnit Spec example:
- * See http://ounit.forge.ocamlcore.org/api-ounit-2.0.0 for documentation
- *)
[TestFixture]public class SolutionTest{[TestCase(new int[] { 1, 5, 10}, ExpectedResult = 9)][TestCase(new int[] { 1, 5, 10, 3, 20}, ExpectedResult = 33)][TestCase(new int[] { 0, 0, 1}, ExpectedResult = 1)]public int Tests(int[] values) => Kata.CalculatingTheAmountEnergy(values);}- module Tests = struct
- open OUnit
- let suite = [
- "Suite Name" >:::
- [
- "ascending the hill" >:: (fun _ ->
- assert_equal (platforms [1; 5; 10]) 9
- );
- "coming back down" >:: (fun _ ->
- assert_equal (platforms [10; 5; 1]) 9
- );
- "tutorial level" >:: (fun _ ->
- assert_equal (platforms [1; 5; 10; 3; 20]) 33
- );
- "plateau" >:: (fun _ ->
- assert_equal (platforms [1; 4; 9; 16; 16; 16; 16; 16; 9; 4; 1]) 30
- );
- "game journalist difficulty" >:: (fun _ ->
- assert_equal (platforms [0; 0; 1]) 1
- );
- "under development" >:: (fun _ ->
- assert_equal (platforms []) 0
- );
- "early access" >:: (fun _ ->
- assert_equal (platforms [2310986]) 0
- )
- ]
- ]
- ;;
- end
public static class Kata { public static bool IsOdd(int input) { return (input % 2 != 0); } }
- public static class Kata
- {
- public static bool IsOdd(int input)
- {
return ((input % 10 == 1) || (input % 10 == 3) || (input % 10 == 5) || (input % 10 == 7) || (input % 10 == 9));- return (input % 2 != 0);
- }
- }
Determine the number of digits in a chosen base using an iterator
fn digits(n: u64, base: u64) -> usize { DigitIterator::new(n, base).count() } struct DigitIterator{ value: u64, base: u64, is_0: bool, first_iter: bool } impl DigitIterator{ pub fn new(value: u64, base: u64) -> Self{ Self{ value, base, is_0: value == 0, first_iter: true, } } } impl Iterator for DigitIterator{ type Item = u64; fn next(&mut self) -> Option<u64>{ if self.is_0 { if self.first_iter{ self.first_iter = false; Some(0) }else{ None } }else{ let old = self.value; self.value /= self.base; if old != 0{ Some(old % self.base) }else{ None } } } }
fn digits(mut n: u64) -> usize {let mut l = 1;while n >= 10 {n /= 10;l += 1;- fn digits(n: u64, base: u64) -> usize {
- DigitIterator::new(n, base).count()
- }
- struct DigitIterator{
- value: u64,
- base: u64,
- is_0: bool,
- first_iter: bool
- }
- impl DigitIterator{
- pub fn new(value: u64, base: u64) -> Self{
- Self{
- value,
- base,
- is_0: value == 0,
- first_iter: true,
- }
- }
- }
- impl Iterator for DigitIterator{
- type Item = u64;
- fn next(&mut self) -> Option<u64>{
- if self.is_0 {
- if self.first_iter{
- self.first_iter = false;
- Some(0)
- }else{
- None
- }
- }else{
- let old = self.value;
- self.value /= self.base;
- if old != 0{
- Some(old % self.base)
- }else{
- None
- }
- }
- }
l- }
#[test] fn pow10() { assert_eq!(digits(0, 10), 1); for i in 0..20 { assert_eq!(digits(POW10[i], 10), i+1); } assert_eq!(digits(std::u64::MAX, 10), 20); } #[test] fn pow10_minus_1() { for i in 1..20 { assert_eq!(digits(POW10[i] - 1, 10), i); } } #[test] fn pow10_half() { for i in 1..20 { assert_eq!(digits(POW10[i] / 2, 10), i); } } #[test] fn base2(){ for i in 1..20{ assert_eq!(digits(2u64.pow(i), 2), (i+1) as usize); } } #[test] fn base16(){ for i in 1..10{ assert_eq!(digits(16u64.pow(i), 16), (i+1) as usize); } }
- #[test]
- fn pow10() {
assert_eq!(digits(0), 1);- assert_eq!(digits(0, 10), 1);
- for i in 0..20 {
assert_eq!(digits(POW10[i]), i+1);- assert_eq!(digits(POW10[i], 10), i+1);
- }
assert_eq!(digits(std::u64::MAX), 20);- assert_eq!(digits(std::u64::MAX, 10), 20);
- }
- #[test]
- fn pow10_minus_1() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] - 1), i);- assert_eq!(digits(POW10[i] - 1, 10), i);
- }
- }
- #[test]
- fn pow10_half() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] / 2), i);- assert_eq!(digits(POW10[i] / 2, 10), i);
- }
- }
- #[test]
- fn base2(){
- for i in 1..20{
- assert_eq!(digits(2u64.pow(i), 2), (i+1) as usize);
- }
- }
- #[test]
- fn base16(){
- for i in 1..10{
- assert_eq!(digits(16u64.pow(i), 16), (i+1) as usize);
- }
- }
def find_max(arr): return sorted(arr)[-1]
- def find_max(arr):
return 0- return sorted(arr)[-1]
test.assert_equals(find_max([1, 2, 3, 4, 5]), 5) test.assert_equals(find_max([1, 3, 2, 1, 2]), 3) test.assert_equals(find_max([9, 1, 5, 6, 7]), 9) test.assert_equals(find_max([5, 4, 3, 2, 1]), 5)
# TODO: Replace examples and use TDD by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)# test.assert_equals(actual, expected, [optional] message)# test.assert_not_equals(actual, expected, [optional] message)# You can use Test.describe and Test.it to write BDD style test groupingstest.assert_equals(find_max([1, 2, 3, 4, 5]), 5)- test.assert_equals(find_max([1, 2, 3, 4, 5]), 5)
- test.assert_equals(find_max([1, 3, 2, 1, 2]), 3)
- test.assert_equals(find_max([9, 1, 5, 6, 7]), 9)
- test.assert_equals(find_max([5, 4, 3, 2, 1]), 5)