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.
This implementation of fibonacci follows https://www.nayuki.io/page/fast-fibonacci-algorithms, with a base case that uses math.pow.
from math import sqrt def fib(x): if x < 70: a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5)) b = 1.618033988749895 # (1 + sqrt(5)) / 2 return round(a * b**x) elif x % 2 == 0: a = fib(x / 2) b = fib(x / 2 + 1) return a * (2 * fib(b) - a) else: # x % 2 == 1, by elimination a = fib((x - 1) / 2) b = fib((x + 1) / 2) return a * a + b * b
# Fibonacci #- from math import sqrt
c1 = (sqrt(5) - 1) / (2 * sqrt(5))c2 = (sqrt(5) + 1) / (2 * sqrt(5))def f(x):fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**xreturn int(round(fibo))- def fib(x):
- if x < 70:
- a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5))
- b = 1.618033988749895 # (1 + sqrt(5)) / 2
- return round(a * b**x)
- elif x % 2 == 0:
- a = fib(x / 2)
- b = fib(x / 2 + 1)
- return a * (2 * fib(b) - a)
- else:
- # x % 2 == 1, by elimination
- a = fib((x - 1) / 2)
- b = fib((x + 1) / 2)
- return a * a + b * b
test.assert_equals(1, fib(0)) test.assert_equals(1, fib(1)) test.assert_equals(2, fib(2)) test.assert_equals(3, fib(3)) test.assert_equals(5, fib(4)) test.assert_equals(8, fib(5)) a = 0 b = 1 for i in range(1000): test.assert_equals(b, fib(i)) tmp = a a = b b = tmp + b
test.assert_equals(5, f(4), "")print f(1)print f(2)print f(3)print f(4)print f(5)- test.assert_equals(1, fib(0))
- test.assert_equals(1, fib(1))
- test.assert_equals(2, fib(2))
- test.assert_equals(3, fib(3))
- test.assert_equals(5, fib(4))
- test.assert_equals(8, fib(5))
- a = 0
- b = 1
- for i in range(1000):
- test.assert_equals(b, fib(i))
- tmp = a
- a = b
- b = tmp + b
Updated to test that it is working with new rust changes
pub fn powermod(n: u64, p: u64, m: u64) -> u64 { if p == 0 { return 1 % m } if p == 1 { return n % m } let mut r = powermod(n, p / 2, m); r = r * r % m; if p & 1 == 1 { r = r * n % m; } r }
- pub fn powermod(n: u64, p: u64, m: u64) -> u64 {
- if p == 0 { return 1 % m }
- if p == 1 { return n % m }
- let mut r = powermod(n, p / 2, m);
- r = r * r % m;
- if p & 1 == 1 {
- r = r * n % m;
- }
- r
}#[test]fn test_powermod() {assert_eq!(powermod(2, 999999, 147), 50);- }
#[test] fn test_powermod() { assert_eq!(powermod(2, 999999, 147), 50); } #[test] #[should_panic] fn returns_expected() { assert_eq!("actual", "expected"); }
- #[test]
- fn test_powermod() {
- assert_eq!(powermod(2, 999999, 147), 50);
- }
- #[test]
- #[should_panic]
- fn returns_expected() {
- assert_eq!("actual", "expected");
- }
defmodule Piapprox do def iter_pi(epsilon) do pi = :math.pi leibniz_stream |> Stream.with_index |> Enum.reduce_while(0, fn ({i, _}, acc) when abs(pi - acc) >= epsilon -> {:cont, acc + i} ({_, n}, acc) -> {:halt, [n, Float.round(acc, 10)]} end) end defp leibniz_stream do Stream.unfold(1, fn n when rem(n,4) == 1 -> { 4/n, n+2 } n -> {-4/n, n+2 } end) end end
- defmodule Piapprox do
- def iter_pi(epsilon) do
leibniz_stream |>Enum.reduce_while(0, fn {i, n}, acc ->if abs(:math.pi - acc) >= epsilon do{ :cont, acc + i }else{ :halt, [n, Float.round(acc, 10)] }endend)- pi = :math.pi
- leibniz_stream
- |> Stream.with_index
- |> Enum.reduce_while(0, fn
- ({i, _}, acc) when abs(pi - acc) >= epsilon ->
- {:cont, acc + i}
- ({_, n}, acc) ->
- {:halt, [n, Float.round(acc, 10)]}
- end)
- end
- defp leibniz_stream do
- Stream.unfold(1, fn
- n when rem(n,4) == 1 -> { 4/n, n+2 }
- n -> {-4/n, n+2 }
end) |> Stream.with_index- end)
- end
- end
Passed and failing tests with imports etc
class Person { String firstName; String lastName; Person(this.firstName,this.lastName); String get fullName => '$firstName $lastName'; }
doubler(n) => n*2;- class Person {
- String firstName;
- String lastName;
- Person(this.firstName,this.lastName);
- String get fullName => '$firstName $lastName';
- }
test('Can create a new person', () { var p = new Person('Bill', 'Smith'); expect(p, new isInstanceOf<Person>()); }); test('Has firstName and lastName', () { var p = new Person('John', 'James'); expect(p.firstName, equals('John')); expect(p.lastName, equals('James')); }); test('Has fullName', () { var p = new Person('Jane', 'Doe'); expect(p.fullName, equals('Jane Doe')); }); test('Has age', () { expect(new Person('Bob','Frank').age, equals(23)); },skip: 'Testing if something can be skipped'); test('Can use async import', () async { var s = new Stream.fromIterable([1,2,3,4,5]); expect(await s.last, equals(5)); }); test('Can handle failed tests', () { expect(new Person('Bill','Smith').firstName,equals('John')); });
// Create your own tests here using the Test package (https://github.com/dart-lang/test)// Here is some boilerplate:test('Can double stuffs', () {expect(doubler(10), equals(20)) // Semicolon is missing here- test('Can create a new person', () {
- var p = new Person('Bill', 'Smith');
- expect(p, new isInstanceOf<Person>());
- });
- test('Has firstName and lastName', () {
- var p = new Person('John', 'James');
- expect(p.firstName, equals('John'));
- expect(p.lastName, equals('James'));
- });
- test('Has fullName', () {
- var p = new Person('Jane', 'Doe');
- expect(p.fullName, equals('Jane Doe'));
- });
- test('Has age', () {
- expect(new Person('Bob','Frank').age, equals(23));
- },skip: 'Testing if something can be skipped');
- test('Can use async import', () async {
- var s = new Stream.fromIterable([1,2,3,4,5]);
- expect(await s.last, equals(5));
- });
- test('Can handle failed tests', () {
- expect(new Person('Bill','Smith').firstName,equals('John'));
- });
module InfiniteSeq where import Data.List ones = repeat 1 nats = [0..] merge = concat . transpose merge2 x y = merge [x, y] ints = 0: merge2 [1..] [(-1),(-2)..] fibs = 1 : scanl (+) 1 fibs
- module InfiniteSeq where
- import Data.List
- ones = repeat 1
- nats = [0..]
- merge = concat . transpose
- merge2 x y = merge [x, y]
- ints = 0: merge2 [1..] [(-1),(-2)..]
- fibs = 1 : scanl (+) 1 fibs
module Codewars.InfiniteSeq.Test where import InfiniteSeq import Test.Hspec import Test.QuickCheck import Test.Hspec.QuickCheck main = hspec $ do describe "test infinite" $ do it "test ones" $ do take 3 ones `shouldBe` [1,1,1] it "test naturals" $ do take 11 InfiniteSeq.nats `shouldBe` [0..10] it "test ints" $ do 10 `elem` ints `shouldBe` True (-10) `elem` ints `shouldBe` True it "test fibs" $ do take 7 fibs `shouldBe` [1,1,2,3,5,8,13]
- module Codewars.InfiniteSeq.Test where
- import InfiniteSeq
- import Test.Hspec
- import Test.QuickCheck
- import Test.Hspec.QuickCheck
- main = hspec $ do
- describe "test infinite" $ do
- it "test ones" $ do
- take 3 ones `shouldBe` [1,1,1]
- it "test naturals" $ do
- take 11 InfiniteSeq.nats `shouldBe` [0..10]
- it "test ints" $ do
- 10 `elem` ints `shouldBe` True
(-10) `elem` ints `shouldBe` True- (-10) `elem` ints `shouldBe` True
- it "test fibs" $ do
- take 7 fibs `shouldBe` [1,1,2,3,5,8,13]
(Functional version with no loops)
Number.prototype.times = function(f) { const n = this.valueOf() const g = (_, i) => f(i) return [...Array(n)].map(g) }
Number.prototype.times = function (f) {for (let i = 0; i < this; i++) f(i);}- Number.prototype.times = function(f) {
- const n = this.valueOf()
- const g = (_, i) => f(i)
- return [...Array(n)].map(g)
- }
let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;
function greetLanguage(greeting = "Hello", language = "Javascript") {var result = `${greeting}, ${language}!`;console.log(result);return result;}- let greetLanguage = (greeting = "Hello", language = "Javascript") => `${greeting}, ${language}!`;
What are all the ways to output a string in Ruby?
# What are all the ways to output a string in Ruby? def hello_ruby greet = "Hello Ruby!" print greet, "\n" puts greet $stdout.write greet + "\n" $stdout.puts greet $stdout.print greet, "\n" $stdout << greet + "\n" (greet+"\n").each_char {|c| print c} end
greet = "Hello Ruby!"- # What are all the ways to output a string in Ruby?
- def hello_ruby
- greet = "Hello Ruby!"
print greet, ""puts greet$stdout.write greet +""$stdout.puts greet$stdout.print greet, ""(greet+"").each_char {|c| print c}- print greet, "
- "
- puts greet
- $stdout.write greet + "
- "
- $stdout.puts greet
- $stdout.print greet, "
- "
- $stdout << greet + "\n"
- (greet+"
- ").each_char {|c| print c}
- end
stdout = with_captured_stdout do hello_ruby end Test.assert_equals(stdout, (["Hello Ruby!\n"] * 7).join)
# TODO: TDD development by writing your own tests as you solve the kata.# 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)#describe "Solution" do# it "should test for something" do# Test.assert_equals("actual", "expected")# end#end- stdout = with_captured_stdout do
- hello_ruby
- end
- Test.assert_equals(stdout, (["Hello Ruby!\n"] * 7).join)