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.
It is not uncommon to see a middle schooler incorrectly simplify the expression (a + b)^2
to a^2 + b^2
in his/her algebra assignments. While the two expressions may be equivalent for particular values of a
and b
(e.g. (0 + 0)^2 = 0^2 = 0 + 0^2 = 0^2 + 0^2
), it is not true in general. So, how can we convince said middle-schooler that (a + b)^2
is not equal to a^2 + b^2
in general? This is equivalent to saying:
-- N.B. The domain of a, b is the natural numbers for
-- sake of simplicity
exists a b. (a + b)^2 /= a^2 + b^2
The general method for proving statements involving existential quantifiers is to provide a specific value or set of values such that the statement to be proven holds true - such value(s) are called witnesses. So, for example, we could provide the witnesses a = 1
and b = 1
in our case which reduces the statement to:
(1 + 1)^2 /= 1^2 + 1^2
And, of course, evaluating both sides of our equation gives 4 /= 2
which trivially holds.
Unfortunately, there is no direct way to encode existential quantifiers (and inequality) as a Haskell type. So what we could do is transform our original statement using known logical equivalences. The first thing to notice is that a /= b
is defined as not (a = b)
for any a, b:
exists a b. not ((a + b)^2 = a^2 + b^2)
Then, de Morgan's law for quantifiers tells us that exists a. not b
is logically equivalent to not (forall a. b)
for any b
(which may or may not be related to a
). Finally, to prove that not (forall a. b)
, it suffices to prove that asserting forall a. b
leads to a contradiction (Void
in Haskell). So our final type (minus a bit of required Haskell boilerplate code) is:
(forall a b. (a + b)^2 = a^2 + b^2) -> Void
See the code provided for the exact type signature of our proof and additional clarifications/explanations.
{-# LANGUAGE
KindSignatures,
GADTs,
TypeOperators,
TypeFamilies,
UndecidableInstances,
RankNTypes,
EmptyCase #-}
module TheMiddleSchoolersMistake where
-- Void - the type representing contradictions in Haskell
import Data.Void
-- Peano's axioms for the natural numbers
data Z
data S n
data Natural :: * -> * where
NumZ :: Natural Z
NumS :: Natural n -> Natural (S n)
-- Axiom of reflexivity of equality: The very notion
-- of equality itself mandates that everything be
-- considered equal to itself
data (:=:) :: * -> * -> * where
Refl :: a -> a :=: a
-- Peano addition, multiplication and exponentation
type family (:+:) (n :: *) (m :: *) :: *
type instance Z :+: n = n
type instance S n :+: m = S (n :+: m)
type family (:*:) (n :: *) (m :: *) :: *
type instance Z :*: n = Z
type instance S n :*: m = m :+: (n :*: m)
type family (:^:) (n :: *) (m :: *) :: *
type instance n :^: Z = S Z
type instance n :^: S m = n :*: (n :^: m)
-- Useful type synonyms for readability
type N1 = S Z -- The natural number 1
type N2 = S N1 -- The natural number 2
type N4 = N2 :+: N2 -- The natural number 4
-- Our proof that (a + b)^2 /= a^2 + b^2 in general
proof ::
(forall n m.
Natural n ->
Natural m ->
((n :+: m) :^: N2) :=: ((n :^: N2) :+: (m :^: N2))) -> Void
proof mistake = let
-- 1 is a natural number
one :: Natural N1
one = NumS NumZ
-- To disprove (a + b)^2 = a^2 + b^2, we provide witnesses
-- a = 1, b = 1 to our counterexample. A witness is a
-- value demonstrating the correctness of a logical statement
-- involving existential quantifiers
-- N.B. The witnesses could be anything else as long as it
-- proves our point. For example, we could've chosen a = 2,
-- b = 3 instead and show that (2 + 3)^2 = 5^2 = 25 /= 13 =
-- 2^2 + 3^2
erroneous :: N4 :=: N2
erroneous = mistake one one in
-- Now we consider all possible cases where 4 = 2 and
-- describe how to construct a proof of Void in each case
-- However, there are no constructors that would allow us
-- to construct a value of such a type! The only possible
-- constructor we could consider is Refl, but Refl always
-- constructs values of a :=: a, never a :=: b where a and
-- b are distinct types. Therefore, we have already
-- considered all (0) possible cases of 4 = 2 and we are
-- done here.
case erroneous of {}
module DummySpec where
import Test.Hspec
import TheMiddleSchoolersMistake
spec :: Spec
spec = do
describe "Proof" $ do
it "should type-check" $ do
"Proof" `shouldNotBe` "Good luck!"
Update getIDS to get the iterated digit sum.
The iterated digit sum of 1563 e.g is 1 + 5 + 6 + 3 = 15.
function getIDS($number) {
return array_sum(str_split($number));
}
// PHPUnit Test Examples:
// TODO: Replace examples and use TDD development by writing your own tests
class MyTestCases extends TestCase
{
// test function names should start with "test"
public function testOutput() {
$this->assertEquals(getIDS(1345), 13);
$this->assertEquals(getIDS(12), 3);
}
}
Write a function which would take two figures as a parameters and generate List of numbers which would contains figures from 1 fo n (n - first input) and lenght of each number should be equals to n .
Then return number at m position ( m - second input).
If number at that position doesn't exist return -1;
Examples :
Generator(2 ,3) => 11 12 21 22
should return 21
Generator(3, 10) => 111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 232 233 311 312 313 321 322 323 331 332 333
should return 211
Good luck :)
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]);
}
}
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));
}
}
Challenge
Implement a linked list function
const linkedList = {
list: {},
addNode: {}
}
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test 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)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// 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", "This is just an example of how you can write your own TDD tests");
// });
});
const linkedList = {};
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test 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)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals(linkedList, linkedList, "This is just an example of how you can write your own TDD tests");
});
});
Validate a porperty in object
function validateProperty(objectForValidate, specificProperty) {
if(objectForValidate[specificProperty]){
return true;
} else{
return false;
}
}
describe("Validate property in object", function(){
it("should be return true when the property exists", function(){
const test = { a: 2 };
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, true);
});
it("should be return false when the property not exists", function(){
const test = {};
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, false);
});
it("should be return false when the property is equals to empty", function(){
const test = { a: '' };
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, false);
});
});
Validate a property in object
function validateProperty(objectForValidate, specificProperty) {
if(objectForValidate[specificProperty]){
return true;
} else{
return false;
}
}
describe("Validate property in object", function(){
it("should be return true when the property exists", function(){
const test = { a: 2 };
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, true);
});
it("should be return false when the property not exists", function(){
const test = {};
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, false);
});
it("should be return false when the property is equals to empty", function(){
const test = { a: '' };
const validation = validateProperty(test, 'a');
Test.assertEquals(validation, false);
});
});
List of packages recently added
-
parsec
,attoparsec
,megaparsec
-
hspec-attoparsec
,hspec-megaparsec
-
regex-pcre
,regex-tdfa
,regex-posix
List of packages tested here
parsec
regex-*
module Example where
import qualified Text.Regex.Posix as Posix
import qualified Text.Regex.PCRE as PCRE
import qualified Text.Regex.TDFA as TDFA
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.String
-- The most basic functionalities of Regex modules
posixMatches :: String -> String -> Bool
posixMatches = (Posix.=~)
pcreMatches :: String -> String -> Bool
pcreMatches = (PCRE.=~)
tdfaMatches :: String -> String -> Bool
tdfaMatches = (TDFA.=~)
pcreVersion :: Maybe String
pcreVersion = PCRE.getVersion
-- The most basic functionalities of Parsec
number :: Parser Integer
number = (\a b -> read a) <$> many1 digit <*> eof
parseInt :: String -> Either ParseError Integer
parseInt = parse number ""
module ExampleSpec where
import Data.Maybe
import Data.Either
import Test.Hspec
import Example
-- `spec` of type `Spec` must exist
spec :: Spec
spec = do
describe "regex" $ do
it "PCRE version should be available" $ do
pcreVersion `shouldSatisfy` isJust
it "Each flavor should work as intended" $ do
posixMatches "baaab" "a+b" `shouldBe` True
posixMatches "aaacb" "a+b" `shouldBe` False
pcreMatches "baaab" "a+b" `shouldBe` True
pcreMatches "aaacb" "a+b" `shouldBe` False
tdfaMatches "baaab" "a+b" `shouldBe` True
tdfaMatches "aaacb" "a+b" `shouldBe` False
describe "parsec" $ do
it "Parsec should work as intended" $ do
parseInt "a" `shouldSatisfy` isLeft
parseInt "123456" `shouldBe` Right 123456
parseInt "123456 " `shouldSatisfy` isLeft
-- the following line is optional for 8.2
main = hspec spec
List of packages tested
megaparsec
hspec-megaparsec
If you can build a working attoparsec
example, please post a kumite on it.
module Example where
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Void
type Parser = Parsec Void String
singleX :: Parser Char
singleX = char 'x'
module ExampleSpec where
import Test.Hspec
import Test.Hspec.Megaparsec
import Text.Megaparsec
import Example
spec :: Spec
spec = do
describe "megaparsec" $ do
it "works as intended" $ do
parse singleX "" `shouldSucceedOn` "x"
-- the following line is optional for 8.2
main = hspec spec
PureScript is a strongly, statically typed functional programming language that compiles to and is easily interoperable with JavaScript. Its syntax and semantics are heavily influenced by Haskell so Haskell enthusiasts should find it easy to pick up PureScript.
Please don't forget to help PureScript leave Beta on Codewars and support the Codewars PureScript community by completing, authoring and translating more PureScript Kata. - @donaldsebleung
module WelcomePureScript (welcomeMsg) where
welcomeMsg :: String
welcomeMsg = "Welcome, PureScript!"
module ExampleSpec where
import Prelude
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import WelcomePureScript (welcomeMsg)
spec :: Spec Unit
spec =
describe welcomeMsg do
it "PureScript is a strongly and statically typed functional programming language that compiles to and is easily interoperable with JavaScript." do
welcomeMsg `shouldEqual` welcomeMsg
it "Its syntax and semantics are heavily influenced by Haskell so Haskell programmers should find it easy to pick up PureScript." do
welcomeMsg `shouldEqual` welcomeMsg
it "Please don't forget to help PureScript leave Beta on Codewars and support the Codewars PureScript community by completing, authoring and translating more PureScript Kata. - @donaldsebleung" do
welcomeMsg `shouldEqual` welcomeMsg