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.
Saved some brackets :)
Find length formatting string in stack allocated buffer.
use std::io::{Write, Cursor}; fn digits(mut n: u64) -> usize { let mut b = [0u8; 20]; let mut c = Cursor::new(&mut b[..]); write!(c, "{}", n).unwrap(); c.position() as usize }
- use std::io::{Write, Cursor};
- fn digits(mut n: u64) -> usize {
let mut l = 1;while n >= 10 {n /= 10;l += 1;}l- let mut b = [0u8; 20];
- let mut c = Cursor::new(&mut b[..]);
- write!(c, "{}", n).unwrap();
- c.position() as usize
- }
#[test] fn pow10() { assert_eq!(digits(0), 1); let mut n = 1; assert_eq!(digits(1), 1); for i in 1..20 { n*=10; assert_eq!(digits(n), i+1); } assert_eq!(digits(std::u64::MAX), 20); } #[test] fn pow10_minus_1() { let mut n = 1; for i in 1..20 { n*=10; assert_eq!(digits(n - 1), i); } } #[test] fn pow10_half() { let mut n = 1; for i in 1..20 { n*=10; assert_eq!(digits(n / 2), i); } }
- #[test]
- fn pow10() {
- assert_eq!(digits(0), 1);
for i in 0..20 {assert_eq!(digits(POW10[i]), i+1);- let mut n = 1;
- assert_eq!(digits(1), 1);
- for i in 1..20 {
- n*=10;
- assert_eq!(digits(n), i+1);
- }
- assert_eq!(digits(std::u64::MAX), 20);
- }
- #[test]
- fn pow10_minus_1() {
- let mut n = 1;
- for i in 1..20 {
assert_eq!(digits(POW10[i] - 1), i);- n*=10;
- assert_eq!(digits(n - 1), i);
- }
- }
- #[test]
- fn pow10_half() {
- let mut n = 1;
- for i in 1..20 {
assert_eq!(digits(POW10[i] / 2), i);- n*=10;
- assert_eq!(digits(n / 2), i);
- }
- }
{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies #-} {-# LANGUAGE OverloadedStrings, GADTs, FlexibleContexts, MultiParamTypeClasses #-} {-# LANGUAGE NoMonomorphismRestriction, GeneralizedNewtypeDeriving #-} module Movies where import Database.Persist (insertMany) import Database.Persist.Sqlite (runSqlite, runMigration) import Database.Persist.TH (mkPersist, mkMigrate, persistUpperCase, share, sqlSettings) share [mkPersist sqlSettings, mkMigrate "migrateTables"] [persistUpperCase| Movies title String year Int rating Int deriving Eq Show |] mkMoviesDB :: IO () mkMoviesDB = runSqlite "/tmp/movies.db" $ do runMigration migrateTables insertMany [ Movies "Rise of the Planet of the Apes" 2011 77 , Movies "Dawn of the Planet of the Apes" 2014 91 , Movies "Alien" 1979 97 , Movies "Aliens" 1986 98 , Movies "Mad Max" 1979 95 , Movies "Mad Max 2: The Road Warrior" 1981 100 ] return ()
- {-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies #-}
- {-# LANGUAGE OverloadedStrings, GADTs, FlexibleContexts, MultiParamTypeClasses #-}
- {-# LANGUAGE NoMonomorphismRestriction, GeneralizedNewtypeDeriving #-}
- module Movies where
- import Database.Persist (insertMany)
- import Database.Persist.Sqlite (runSqlite, runMigration)
- import Database.Persist.TH (mkPersist, mkMigrate, persistUpperCase, share, sqlSettings)
- share [mkPersist sqlSettings, mkMigrate "migrateTables"] [persistUpperCase|
- Movies
- title String
- year Int
- rating Int
- deriving Eq Show
- |]
- mkMoviesDB :: IO ()
- mkMoviesDB = runSqlite "/tmp/movies.db" $ do
- runMigration migrateTables
- insertMany
- [ Movies "Rise of the Planet of the Apes" 2011 77
- , Movies "Dawn of the Planet of the Apes" 2014 91
- , Movies "Alien" 1979 97
- , Movies "Aliens" 1986 98
- , Movies "Mad Max" 1979 95
- , Movies "Mad Max 2: The Road Warrior" 1981 100
- ]
- return ()
def is_prime(n) n.prime? end
- def is_prime(n)
return false if n < 2for x in 2.. Math.sqrt(n).roundreturn false if n % x == 0endreturn true- n.prime?
- end
require 'prime' def is_prime_check(n) n.prime? end describe "Basic Tests" do it "Low values of n" do Test.assert_equals(is_prime(1), false) Test.assert_equals(is_prime(2), true) Test.assert_equals(is_prime(3), true) Test.assert_equals(is_prime(5), true) Test.assert_equals(is_prime(7), true) Test.assert_equals(is_prime(11), true) Test.assert_equals(is_prime(13), true) Test.assert_equals(is_prime(15), false) Test.assert_equals(is_prime(18), false) Test.assert_equals(is_prime(21), false) end end describe "Random Tests" do it "Values of n between 1000 and 10e12" do 100.times do n = rand(1000..1000000000000) puts "Testing for n = #{n}" Test.assert_equals(is_prime(n), is_prime_check(n)) end end end
- require 'prime'
- def is_prime_check(n)
return false if n < 2for x in 2.. Math.sqrt(n).roundreturn false if n % x == 0endreturn true- n.prime?
- end
- describe "Basic Tests" do
- it "Low values of n" do
- Test.assert_equals(is_prime(1), false)
- Test.assert_equals(is_prime(2), true)
- Test.assert_equals(is_prime(3), true)
- Test.assert_equals(is_prime(5), true)
- Test.assert_equals(is_prime(7), true)
- Test.assert_equals(is_prime(11), true)
- Test.assert_equals(is_prime(13), true)
- Test.assert_equals(is_prime(15), false)
- Test.assert_equals(is_prime(18), false)
- Test.assert_equals(is_prime(21), false)
- end
- end
- describe "Random Tests" do
it "Values of n between 1000 and 10e12" dofor h in 1..100n = rand(1000..1000000000000)result = is_prime_check(n)res = is_prime(n)it "Testing for n = " + n.to_s doTest.assert_equals(res, result)end- it "Values of n between 1000 and 10e12" do
- 100.times do
- n = rand(1000..1000000000000)
- puts "Testing for n = #{n}"
- Test.assert_equals(is_prime(n), is_prime_check(n))
- end
- end
- end
def is_prime(n): return n == 2 or n > 1 and all(n % i for i in range(3, int(n**0.5)+1, 2))
- def is_prime(n):
return n > 1 and all(n % i for i in range(2, int(n**0.5)+1))- return n == 2 or n > 1 and all(n % i for i in range(3, int(n**0.5)+1, 2))
test.expect(is_prime(7) == True) test.expect(is_prime(17) == True) test.expect(is_prime(15) == False) test.expect(is_prime(21) == False) test.expect(is_prime(1729) == False) test.expect(is_prime(104729) == True) test.expect(is_prime(1299709) == True) test.expect(is_prime(1299721) == True) test.expect(is_prime(1299743) == True) test.expect(is_prime(1299763) == True) test.expect(is_prime(1299791) == True) test.expect(is_prime(1299811) == True) test.expect(is_prime(1299813) == False) test.expect(is_prime(1299817) == True) test.expect(is_prime(1299821) == True) test.expect(is_prime(1299827) == True) test.expect(is_prime(32416190071) == True) test.expect(is_prime(2**31-1) == True) test.expect(is_prime(2**40-87) == True) test.expect(is_prime(2**45-55) == True)
- test.expect(is_prime(7) == True)
- test.expect(is_prime(17) == True)
- test.expect(is_prime(15) == False)
- test.expect(is_prime(21) == False)
- test.expect(is_prime(1729) == False)
- test.expect(is_prime(104729) == True)
- test.expect(is_prime(1299709) == True)
- test.expect(is_prime(1299721) == True)
- test.expect(is_prime(1299743) == True)
- test.expect(is_prime(1299763) == True)
- test.expect(is_prime(1299791) == True)
- test.expect(is_prime(1299811) == True)
- test.expect(is_prime(1299813) == False)
- test.expect(is_prime(1299817) == True)
- test.expect(is_prime(1299821) == True)
- test.expect(is_prime(1299827) == True)
- test.expect(is_prime(32416190071) == True)
- test.expect(is_prime(2**31-1) == True)
- test.expect(is_prime(2**40-87) == True)
- test.expect(is_prime(2**45-55) == True)
using System; class Kata { public static string Main(string greeting, string language) { var g = Greeting(greeting, language); Console.WriteLine(g); return g; } public static string Greeting(string greeting, string language) => $"{greeting}, {language}!"; }
- using System;
- class Kata {
- public static string Main(string greeting, string language) {
Console.Write("{0}, {1}!\n", greeting, language);return greeting + ", " + language + "!";- var g = Greeting(greeting, language);
- Console.WriteLine(g);
- return g;
- }
- public static string Greeting(string greeting, string language) => $"{greeting}, {language}!";
- }
In this varation, I have refactored the code so that the execute helper wraps up the transaction rollback as part of its method call. It also will try to parse the response as json and provide that as the 2nd argument, DRYing up the code as more tests are added.
describe "Solution" do it "should test for something" do RequestHelpers.execute('GET', SolutionController, 'show') do |response, json| Test.assert_equals(json['title'], 'Test') end end end
require "rack/utils"def with_dbActiveRecord::Base.transaction doyieldraise ActiveRecord::Rollbackendend- describe "Solution" do
- it "should test for something" do
with_db doresponse = RequestHelpers.execute('GET', SolutionController, 'show')json = JSON.parse(response.body)- RequestHelpers.execute('GET', SolutionController, 'show') do |response, json|
- Test.assert_equals(json['title'], 'Test')
- end
- end
- end