Y = \f.(\x.f(x x))(\x.f(x x))
pred = \ n f x . n (\ g h . h (g f)) (\ _ . x) (\ x . x)
sub = \ m n . n pred m
True = \ a b . a
False = \ a b . b
is-zero = \ n . n (\ _ . False) True
mod = Y(\F.(\a.\b.((is-zero(sub b(sub a 1))((\c.\d.\e.(c)))(((is-zero(sub a(sub b 1))))(\c.\d.\e.(d))((\c.\d.\e.(e))))))(((F(sub a b))b))(a)0))
import { assert, LC, getSolution } from "./lc-test.js";
LC.configure({ purity: "Let", numEncoding: "Church", verbosity: "Concise" });
const { mod } = LC.compile(getSolution());
describe("Modulo", () => {
it("example tests", () => {
assert.numEql( mod(7)(2), 1 );
assert.numEql( mod(17)(3), 2 );
assert.numEql( mod(25)(5), 0 );
});
});
hyperop = (\f.(\x.f(x x))(\x.f(x x))) (\f.\s.\a.\n. s(\x.\a.\b.b)(\a.\b.a) (\x.a(n x)) (n(\m.f(\f.\x.s(\g.\h.h(g f))(\u.x)(\u.u))a m)(\f.\x.f x)))
import { assert, LC, getSolution } from "./lc-test.js";
LC.configure({ purity: "Let", numEncoding: "Church", verbosity: "Concise" });
const { hyperop } = LC.compile(getSolution());
describe("Hyperop", () => {
it("example tests", () => {
assert.numEql( hyperop(1)(1)(1), 1 );
assert.numEql( hyperop(1)(2)(2), 4 );
assert.numEql( hyperop(7)(2)(2), 4 );
assert.numEql( hyperop(0)(3)(2), 6 );
assert.numEql( hyperop(1)(3)(2), 9 );
assert.numEql( hyperop(2)(3)(2), 27 );
assert.numEql( hyperop(3)(2)(3), 65536 );
});
});
string_lower(_, _).
:- begin_tests(example).
:- include(example).
test(string_lower_tests, [forall(member(Input-Expect, ["X"-"x", "A"-"a"])), Result = Expect]) :-
string_lower(Input, Result).
:- end_tests(example).
function foo() {
return bar();
}
const chai = require("chai");
const assert = chai.assert;
function bar() {
return 5;
}
describe("Solution", function() {
it("should test for something", function() {
assert.strictEquals(foo(), 5);
});
});
USING: ;
IN: example
: foo ( -- x ) 4 ;
IN: math.margins
! Overriding math.margins definitions
: ± ( -- y ) 5 ;
USING: math.margins tools.testest ;
FROM: example => foo ;
IN: example.tests
: run-tests ( -- )
"Example" describe#{
"test case" it#{
<{ ± -> 5 }>
}#
}#
;
MAIN: run-tests
USING: namespaces vocabs.loader prettyprint ;
IN: example
vocab-roots get .
! Use vocabulary tools.testest for testing.
! See https://github.com/codewars/testest
USING: example tools.testest ;
IN: example.tests
: run-tests ( -- )
"Example" describe#{
"test case" it#{
<{ 1 -> 1 }>
}#
}#
;
MAIN: run-tests
def foo(n): if n == 0: while True: yield 0 else: for v in foo(n-1): yield v+1
USING: kernel math generators combinators.extras ;IN: exampleGEN: foo ( n -- gen )[ [ 0 yield ] forever ][ 1 - foo [ [ next 1 + yield ] keep ] forever drop ] if-zero ;- def foo(n):
- if n == 0:
- while True:
- yield 0
- else:
- for v in foo(n-1):
- yield v+1
import codewars_test as test from itertools import islice from solution import foo take = lambda gen, n: list(islice(gen, n)) depth = 100 vals = 300 @test.describe("Example") def _(): @test.it("test case") def _(): test.assert_equals(take(foo(depth), vals), [depth]*vals)
USING: example tools.testest generators arrays ;IN: example.tests- import codewars_test as test
- from itertools import islice
- from solution import foo
CONSTANT: depth 100CONSTANT: vals 300- take = lambda gen, n: list(islice(gen, n))
: run-tests ( -- )"Example" describe#{"test case" it#{<{ depth foo vals take -> vals depth <array> }>}#}#;- depth = 100
- vals = 300
MAIN: run-tests- @test.describe("Example")
- def _():
- @test.it("test case")
- def _():
- test.assert_equals(take(foo(depth), vals), [depth]*vals)
Builds a stack of depth
generators, each incrementing a value starting at 0 by 1 before yielding. vals
number of items are then generated. Expected time complexity should be O(depth * vals)
, however we find that depth = 10, vals = 1000
is very fast, depth = 100, vals = 100
is somewhat slow, and depth = 1000, vals = 10
is extremely slow (times out).
USING: kernel math generators combinators.extras ;
IN: example
GEN: foo ( n -- gen )
[ [ 0 yield ] forever ]
[ 1 - foo [ [ next 1 + yield ] keep ] forever drop ] if-zero ;
USING: example tools.testest generators arrays ;
IN: example.tests
CONSTANT: depth 100
CONSTANT: vals 300
: run-tests ( -- )
"Example" describe#{
"test case" it#{
<{ depth foo vals take -> vals depth <array> }>
}#
}#
;
MAIN: run-tests
Simply loading sequences.extras
adds ~5-6 seconds of runtime.
USING: sequences.extras ;
IN: example
: foo ( -- v ) 1 ;
USING: example tools.testest ;
IN: example.tests
: run-tests ( -- )
"Example" describe#{
"test case" it#{
<{ foo -> 1 }>
}#
}#
;
MAIN: run-tests
USING: ;
IN: foo
: bla ( -- ) 3 ;
USING: kernel foo tools.testest prettyprint assocs compiler.errors namespaces sequences tools.errors ;
IN: foo.tests
: run-tests ( -- )
compiler-errors get values [
"Example" describe#{
"test case" it#{
<{ bla -> 1 }>
}#
}#
] [ errors. ] if-empty
;
MAIN: run-tests
print("Solution code")
import codewars_test as test
print("Test Code")
import solution # or from solution import something
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
USING: kernel sequences qw ;
IN: foo
: x ( seq -- counts ) qw{ n e w s } [ [ = ] curry count ] with map ; inline
! Use vocabulary tools.testest for testing.
! See https://github.com/codewars/testest
USING: foo tools.testest ;
IN: example.tests
: run-tests ( -- )
"Example" describe#{
"test case" it#{
<{ { "s" } x -> { 0 0 0 1 } }>
}#
}#
;
MAIN: run-tests
-1
def f(s,p):s.c='YNeos'[(p=='')+sum(map(ord,p.lower()))%324>0::2] KumiteFoo=type('',(),{"__init__":f,"solution":lambda s:s.c})
def f(s,p):s.c='No'if(p=='')+sum(map(ord,p.lower()))%324else'Yes'- def f(s,p):s.c='YNeos'[(p=='')+sum(map(ord,p.lower()))%324>0::2]
- KumiteFoo=type('',(),{"__init__":f,"solution":lambda s:s.c})
import codewars_test as test from solution import KumiteFoo @test.describe("Example") def test_group(): @test.it("test case: True return Yes") def test_case(): test.assert_equals(KumiteFoo('ffoooo').solution(), 'Yes') test.assert_equals(KumiteFoo('fffoooooo').solution(), 'Yes') test.assert_equals(KumiteFoo('fffffoooooooooo').solution(), 'Yes') test.assert_equals(KumiteFoo('FfffFoooOooOoOo').solution(), 'Yes') test.assert_equals(KumiteFoo('ffffffffffffffffoooooooooooooooooooooooooooooooo').solution(), 'Yes') @test.it("test case: False return No") def test_case(): test.assert_equals(KumiteFoo('ffooo').solution(), 'No') test.assert_equals(KumiteFoo('fffooooo').solution(), 'No') test.assert_equals(KumiteFoo('ffoo').solution(), 'No') test.assert_equals(KumiteFoo('').solution(), 'No')
- import codewars_test as test
- from solution import KumiteFoo
- @test.describe("Example")
- def test_group():
- @test.it("test case: True return Yes")
- def test_case():
- test.assert_equals(KumiteFoo('ffoooo').solution(), 'Yes')
- test.assert_equals(KumiteFoo('fffoooooo').solution(), 'Yes')
- test.assert_equals(KumiteFoo('fffffoooooooooo').solution(), 'Yes')
- test.assert_equals(KumiteFoo('FfffFoooOooOoOo').solution(), 'Yes')
- test.assert_equals(KumiteFoo('ffffffffffffffffoooooooooooooooooooooooooooooooo').solution(), 'Yes')
- @test.it("test case: False return No")
- def test_case():
- test.assert_equals(KumiteFoo('ffooo').solution(), 'No')
- test.assert_equals(KumiteFoo('fffooooo').solution(), 'No')
- test.assert_equals(KumiteFoo('ffoo').solution(), 'No')
- test.assert_equals(KumiteFoo('').solution(), 'No')
pattern1 = r"\d|\((?R)\)"
pattern2 = r"(.*)00(?1)"
import codewars_test as test
from solution import pattern1, pattern2
import regex
@test.describe("Recursive regex")
def test_group():
@test.it("Pattern 1")
def test_case():
test.expect(regex.match(pattern1, "5"))
test.expect(regex.match(pattern1, "(6)"))
test.expect(regex.match(pattern1, "((((((9))))))"))
@test.it("Pattern 2")
def test_case():
test.expect(regex.match(pattern2, "110011"))
test.expect(regex.match(pattern2, "abc00abc"))
test.expect(regex.match(pattern2, "00"))
USING: kernel parser sequences quotations prettyprint fry ; QUALIFIED-WITH: tools.testest tt IN: testest.extras : wrap-it ( quot -- wrapped ) '[ tt:it#{ _ dip tt:}# ] ; : wrap-describe ( quot -- wrapped ) '[ tt:describe#{ _ dip tt:}# ] ; SYNTAX: it#{ \ tt:}# parse-until >quotation wrap-it append! ; SYNTAX: describe#{ \ tt:}# parse-until >quotation wrap-describe append! ;
- USING: kernel parser sequences quotations prettyprint fry ;
- QUALIFIED-WITH: tools.testest tt
- IN: testest.extras
- : wrap-it ( quot -- wrapped )
- '[ tt:it#{ _ dip tt:}# ] ;
- : wrap-describe ( quot -- wrapped )
- '[ tt:describe#{ _ dip tt:}# ] ;
SYNTAX: it#{ \ tt:}# parse-until >quotation wrap-it suffix! \ call suffix! ;SYNTAX: describe#{ \ tt:}# parse-until >quotation wrap-describe suffix! \ call suffix! ;- SYNTAX: it#{ \ tt:}# parse-until >quotation wrap-it append! ;
- SYNTAX: describe#{ \ tt:}# parse-until >quotation wrap-describe append! ;