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.
Haskell is lazy, so we could get infinite sequence naturally.
like this.
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)..]
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
write one compare function.
- first compare with string's length
- when string have same length, then compare string with lex order.
module CmpStr where
import Data.Function
import Data.Monoid
cmpStr :: String -> String -> Ordering
cmpStr = (compare `on` length) `mappend` compare
module CmpStr.Test where
import CmpStr
import Test.Hspec
import Test.QuickCheck
main = hspec $ do
describe "test for cmpstr" $ do
it "test cmpstr" $ do
cmpStr "abc" "defz" `shouldBe` LT
cmpStr "abcd" "def" `shouldBe` GT
cmpStr "abc" "abc" `shouldBe` EQ
cmpStr "abc" "def" `shouldBe` LT
There seems to be an error with looking at the top of a stack, but it doesn't throw. It just kills execution. What's going on here?
#include <stack>
bool method()
{
std::stack<int> my_stack;
//my_stack.push(1);
try {
if(my_stack.top())
{
}
}
catch(...) {
std::cout << "An Exception Occurred" << std::endl;
}
return true;
}
Describe(check_the_stack)
{
It(should_do_something)
{
Assert::That(method(), Equals(true));
}
};
Looks like C++ and C# return their tests in alphabetical order.
int a = 1;
Describe(alphabetical_order)
{
It(b)
{
Assert::That(1, Equals(1));
}
It(c)
{
Assert::That(1, Equals(1));
}
It(a)
{
Assert::That(1, Equals(1));
}
It(d)
{
Assert::That(1, Equals(1));
}
};
For some reason I keep using this function as a test in all the runner changes
fn doubler(n:i32) -> i32 {
n * 2
}
#[test]
fn can_double() {
assert_eq!(doubler(23), 42);
}
#[test]
fn can_also_double() {
assert_eq!(doubler(23), 46);
}
#[test]
fn can_also_not_double() {
assert_eq!(doubler(23), 4);
}
let stringToHTML = (el = 'div', attr = [], template = '') =>
`<${el} ${attr.join(' ')}>${template}</${el}>`,
element = 'a',
content = "https://www.codewars.com"
attributes = [`href='${content}'`, "class='link'"];
stringToHTML(element, attributes, content);
// => "<a href='https://www.codewars.com' class='link'>https://www.codewars.com</a>"
// TODO: Replace examples and use TDD development by writing your own tests
// These are some of the methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// You can also use Chai (http://chaijs.com/)
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals("actual", "expected");
});
});
// !!!
class YouShallNotPass extends TestCase {
public function youShallNotPass() {
$this->assertTrue(false);
}
}
Can this code be refactored into something more elegant?
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)] }
end
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
defmodule PiapproxTest do
use ExUnit.Case
def testPiApprox(nb, epsilon, ans) do
IO.puts("Test #{nb}")
assert Piapprox.iter_pi(epsilon) == ans
end
test "iter_pi" do
testPiApprox 1, 0.1, [ 10, 3.0418396189]
testPiApprox 2, 0.01, [ 100, 3.1315929036]
testPiApprox 3, 0.001, [ 1000, 3.1405926538]
testPiApprox 4, 7.001e-4, [ 1429, 3.1422924436]
testPiApprox 5, 6.001e-5, [ 16664, 3.1415326440]
end
end
I used mathematical analysis to find the two constants, from the iterative form: f(n) = f(n-1) + f(n-2) and initial conditions.
# 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)**x
return int(round(fibo))
test.assert_equals(5, f(4), "")
print f(1)
print f(2)
print f(3)
print f(4)
print f(5)
Construct a singleton in js.
Tests:
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
obj1 === obj2 // true
obj2.test // 1
let Singleton = (function () {
let instance;
return function Construct_singletone () {
if (instance) {
return instance;
}
if (this && this.constructor === Construct_singletone) {
instance = this;
} else {
return new Construct_singletone();
}
}
}());
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
describe("Test cases", function(){
it("instance must be the same", function(){
Test.assertEquals(obj1 === obj2, true, "Passed");
Test.assertEquals(obj2.test, 1, "Passed");
});
});