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.
Help the wicked witch!!
Given a binary tree, return the id of the branch containing a poisoned apple.
Each node has the following structure:
Node: {
id: int
poisoned: true/false
left: Node
right: Node
}
If no poisoned apple on the tree return -1.
const findPoisoned = (node) => {
// Help evil prevail !
return node == null || node == undefined ? -1
: node.poisoned ? node.id
: Math.max(findPoisoned(node.left),findPoisoned(node.right));
}
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
// const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("should test for something", function() {
Test.assertEquals(findPoisoned({id:2, poisoned:true}), 2);
Test.assertEquals(findPoisoned({id:2, poisoned:false}), -1);
Test.assertEquals(findPoisoned({id:2, poisoned:false, left:{id:4 , poisoned:true} }), 4);
Test.assertEquals(findPoisoned({id:2, poisoned:false, right:{id:4 , poisoned:true} }), 4);
});
});
A novel function to generate console formatted tables.
head = ["***", "Numbler =>", 40, 'another', "last"]
body = [
[ 1 , "B", "C", 2, 3],
["D", "E", "F", 4, 5]
]
t = table(body, head=head)
test = '''\
╭─────┬────────────┬────┬─────────┬──────╮
│ *** │ Numbler => │ 40 │ another │ last │
├─────┼────────────┼────┼─────────┼──────┤
│ 1 │ B │ C │ 2 │ 3 │
│ D │ E │ F │ 4 │ 5 │
╰─────┴────────────┴────┴─────────┴──────╯'''
Test.assert_equals(t, test)
print(t)
Change the input sentence so switchout changes into word.
ex.: I am smart,smart,dumb = I am dumb
def switchout(sentence,switchout,word):
return sentence.replace(switchout,word)
test.assert_equals(switchout("I like cats"), " g F e D c B a ")
Linear search with while loop:
- Instead of
- Every Iteration
- check if i is smaller than length of array, if not increament i
- check if arr[i] equals to x
- Do
- Once
- check if last item is equal to x, if yes return index
- if not, replace last array item with x, saving the last item in another variable
- if i is smaller than length of array, return i
- else return nil
- Every Iteration - check if arr[i] equals to x, if not increament i
def Linear_search(arr,x)
i=0
last_idx=arr.size-1
if arr[last_idx] == x
return last_idx
else
last = arr[last_idx]
arr[last_idx]=x
end
while arr[i]!=x
i+=1
end
i < last_idx ? i : nil
end
describe "Linear Search" do
it "Test for search result" do
Test.assert_equals(Linear_search([1,4,5,3,7,10], 7), 4)
Test.assert_equals(Linear_search([1,4,5,7,3,10], 7), 3)
Test.assert_equals(Linear_search([1,4,5,10,3,7], 7), 5)
Test.assert_equals(Linear_search([1,4,5,10,3,6], 7), nil)
end
end
flop,(<%>) :: (Functor f) -> f (a -> b) -> a -> f b
Specialises to flip
for (->) r
;
otherwise useful for take <$> choose (0,length xs) <%> xs
and similar binary function constructs with functors / applicatives / monads.
Known elsewhere as Control.Lens.(??)
and Relude.Functor.Fmap.flap
module FlipFlop (flop,(<%>)) where
infixl 4 `flop`,<%>
flop,(<%>) :: (Functor f) => f (a -> b) -> a -> f b
flop fab a = fmap ($ a) fab
(<%>) = flop
{-# Specialise flop :: (a -> b -> c) -> b -> a -> c #-}
{-# Specialise (<%>) :: (a -> b -> c) -> b -> a -> c #-}
module FlipFlopSpec (spec) where
import FlipFlop (flop,(<%>))
import Test.Hspec
import Test.QuickCheck
spec :: Spec
spec = do
it "tests" $ do
(>) 1 2 `shouldBe` False
(>) 2 1 `shouldBe` True
flop (>) 1 2 `shouldBe` True
flop (>) 2 1 `shouldBe` False
[(+1),(+2),(+3)] <%> 4 `shouldBe` [5,6,7]
(Just (+1)) <%> 2 `shouldBe` Just 3
Nothing <%> () `shouldBe` (Nothing :: Maybe ())
it "random tests" $ do
forAll genTest $ \ xs -> do
-- print $ xs
xs `shouldSatisfy` (>0) . length
xs `shouldSatisfy` (<4) . length
xs `shouldSatisfy` all id
genTest :: Gen [Bool]
genTest = replicate <$> choose (1,3) <%> True
main :: IO ()
main = hspec $ describe "FlipFlop" spec
Palindrome:
A palindrome is a word that when reversed reads the same. An example is "evil rats star live."
Valid palindromes should output 'TRUE' while invalid ones should output 'FALSE'.
function palindrome (x) {
let s = x.split('').reverse().join('')
return x == s ? "TRUE" : "FALSE"
}
Test.assertEquals(palindrome('evil rats star live'), "TRUE");
Test.assertEquals(palindrome('Owu'), "FALSE");
Test.assertEquals(palindrome('level'), "TRUE");
Test.assertEquals(palindrome('uwu'), "TRUE");
Test.assertEquals(palindrome('stressed desserts'), "TRUE");
You are asking to write a function, which takes N lists as input, such so len(l1) >= len(l2) >= .. len(lN) and interleave them into a single list. If you get a single list in input, then just return original list as is.
Example:
You have ['a1','a1','a3'], ['b1','b2'],['c1'] to interleave. Finually, you should get ['a1','b1','c1','a2','b2','c1'].
def interleave(*argv):
lists = len(argv)
if 1 == lists:
return argv[0]
else:
o = [x for t in zip(*argv) for x in t]
for i in reversed(range(0, lists - 1)):
l = len(argv[i + 1])
lists = []
for _list in argv[:i+1]:
lists.append(_list[l:])
o += [x for t in zip(*lists) for x in t]
return o
test.assert_equals(interleave([]), [])
test.assert_equals(interleave([1,2]), [1,2])
test.assert_equals(interleave([1,2,3], ['a','b','c']), [1,'a',2,'b',3,'c'])
test.assert_equals(interleave(list(range(1, 11, 3)), list(range(2, 11, 3)), list(range(3, 11, 3))), list(range(1, 11)))
Just seeing if I can get the canvas element to work.
The best way to see the canvasses is to hit Fork, then Run, then look at the test logs.
More information about these images:
https://en.wikipedia.org/wiki/Maurer_rose
function maurerRose(n, d, length) {
length /= 2
var locations = []
for (var i = 0; i <= 360; i++) {
var k = i * d * Math.PI / 180
var r = Math.sin(n * k) * length
var a = r * Math.cos(k) + length
var b = r * Math.sin(k) + length
locations.push([Math.round(a), Math.round(b)])
}
return locations
}
var canvas = require('canvas')
const { assert } = require('chai')
describe("Fixed tests", () => {
function showMaurerRose(locations, length) {
var draw = new canvas(length, length)
var ctx = draw.getContext('2d')
ctx.fillStyle = "rgb(10, 10, 10)"
ctx.fillRect(0, 0, length, length)
ctx.beginPath()
ctx.strokeStyle = '#fff'
ctx.moveTo(locations[0][0], locations[0][1])
for (var loc of locations)
ctx.lineTo(loc[0], loc[1])
ctx.stroke()
console.log('<img src="' + draw.toDataURL() + '"/>')
}
function solution(n, d, length) {
length /= 2
var locations = []
for (var i = 0; i <= 360; i++) {
var k = i * d * Math.PI / 180
var r = Math.sin(n * k) * length
var a = r * Math.cos(k) + length
var b = r * Math.sin(k) + length
locations.push([Math.round(a), Math.round(b)])
}
return locations
}
var roses = [
[4, 74], [18, 22], [5, 82], [4, 42], [18, 9], [9, 82], [6, 71],
[8, 56], [3, 42], [14, 5], [2, 39], [3, 65], [9, 23], [11, 101],
[8, 51], [3, 87], [2, 51], [18, 77], [8, 12], [15, 14], [18, 19],
[4, 39], [15, 62], [8, 46], [7, 42], [7, 59], [2, 95], [33, 91]
]
roses.forEach(rose => {
var length = 200
var expected = solution(rose[0], rose[1], length)
var actual = maurerRose(rose[0], rose[1], length)
console.log(`n${rose[0]} d${rose[1]}`)
showMaurerRose(actual, length)
assert.strictEqual(actual.slice(0, 5)+``, expected.slice(0, 5)+``)
});
});
I have tried the Kata below, but the execution is too slow, what can I do to make this script faster?
"Implement the function unique_in_order which takes as argument a sequence and returns a list of items without any elements with the same value next to each other and preserving the original order of elements.
For example:
unique_in_order('AAAABBBCCDAABBB') == ['A', 'B', 'C', 'D', 'A', 'B']
unique_in_order('ABBCcAD') == ['A', 'B', 'C', 'c', 'A', 'D']
unique_in_order([1,2,2,3,3]) == [1,2,3]
def unique_in_order(iterable):
iterable = list(iterable)
while 1==1:
for x in range(len(iterable)):
if iterable[x]==iterable[x-1]:
del iterable[x]
break
if x==len(iterable)-1:
return iterable
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
test.assert_equals(unique_in_order('44444455321111'), ['4','5','3','2','1'])
test.assert_equals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B'])
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCsssCCCCCCCCCCCCCDAABBB'), ['A','B','C','s', 'C','D','A','B'])
You are given two arrays of integers, sorted in ascending order. Your task is to write a function that merges the two arrays, and output a merged array that is also sorted. But there's the catch! The Array.prototype.sort()
function is not available!
For example, given the following two arrays:
[0, 3, 6, 13, 45]
[-1, 45, 330, 553]
Your function should return:
[-1, 0, 3, 6, 13, 45, 45, 330, 553]
Note that there may be duplicates. It's fine to leave them in there.
function mergeArrays(arrA, arrB) {
const output = [];
const arrAClone = [...arrA];
const arrBClone = [...arrB];
let nextSmallestA = arrAClone.shift();
let nextSmallestB = arrBClone.shift();
while (nextSmallestA !== undefined || nextSmallestB !== undefined) {
if (nextSmallestA === undefined || nextSmallestB < nextSmallestA) {
output.push(nextSmallestB);
nextSmallestB = arrBClone.shift();
} else {
output.push(nextSmallestA);
nextSmallestA = arrAClone.shift();
}
}
return output;
}
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
const assert = require("assert");
describe("mergeArray", function() {
it("should work for 2 empty arrays", function() {
const arr1 = [];
const arr2 = [];
const expected = [];
const actual = mergeArrays(arr1, arr2);
assert.deepStrictEqual(actual, expected);
});
it("should work with a single empty array", function() {
const arr1 = [1,3,5,7,9,11,13,15,17,19,200];
const arr2 = [];
const expected = [1,3,5,7,9,11,13,15,17,19,200];
const actual = mergeArrays(arr1, arr2);
assert.deepStrictEqual(actual, expected);
});
it("should work with large numbers", function() {
const arr1 = [64649367165265, 274632412695799, 412877423124429, 516230628864647, 538837081813693, 746373629824209, 1692897751449627, 2270037413537007, 2733584891633993, 2812411720528947, 2882676970678137, 2903411384421675, 2958418807276103, 3130000929603865, 3175980736717465, 3614311465231655, 3996463578205695, 4103082738409165, 4258621578191541, 4418610303201609, 4647855513194407, 4685348960324559, 4808920381357755, 5183451753845617, 5248837872019479, 5459393081841211, 6169730898746597, 6569116706507233, 6603729845179117, 7155136605307029, 7212160056015967, 7244553210477423, 7384589786348845, 7713430447934781, 8114895595811233, 8209510986022041, 8398715614919241, 8563902159010211, 8691058183717011, 8962445733616225];
const arr2 = [163457591720831, 196860880460605, 668872604533663, 681475781382441, 739991228922867, 1204232377264725, 1402567083052239, 1418358979840983, 1875285439667131, 2112478608256379, 2362707116240501, 2480247716891123, 2815699197032227, 2946539963432449, 2978823594913687, 3298444539285921, 3664268267691413, 4277457216782315, 4316531169004027, 4567307451346087, 4655426574120211, 4696413724870613, 4831259468359653, 5052243723913683, 5076825973761899, 5313627140975321, 5549643481155739, 5763038910068197, 5863617689275713, 6100078677408673, 6182322012227663, 6673442111678693, 6677992076967503, 6765194736438189, 6794436664762403, 7138122320940275, 7407652791154167, 7650254427397051, 8784125927428599, 8794961983529061];
const expected = [64649367165265, 163457591720831, 196860880460605, 274632412695799, 412877423124429, 516230628864647, 538837081813693, 668872604533663, 681475781382441, 739991228922867, 746373629824209, 1204232377264725, 1402567083052239, 1418358979840983, 1692897751449627, 1875285439667131, 2112478608256379, 2270037413537007, 2362707116240501, 2480247716891123, 2733584891633993, 2812411720528947, 2815699197032227, 2882676970678137, 2903411384421675, 2946539963432449, 2958418807276103, 2978823594913687, 3130000929603865, 3175980736717465, 3298444539285921, 3614311465231655, 3664268267691413, 3996463578205695, 4103082738409165, 4258621578191541, 4277457216782315, 4316531169004027, 4418610303201609, 4567307451346087, 4647855513194407, 4655426574120211, 4685348960324559, 4696413724870613, 4808920381357755, 4831259468359653, 5052243723913683, 5076825973761899, 5183451753845617, 5248837872019479, 5313627140975321, 5459393081841211, 5549643481155739, 5763038910068197, 5863617689275713, 6100078677408673, 6169730898746597, 6182322012227663, 6569116706507233, 6603729845179117, 6673442111678693, 6677992076967503, 6765194736438189, 6794436664762403, 7138122320940275, 7155136605307029, 7212160056015967, 7244553210477423, 7384589786348845, 7407652791154167, 7650254427397051, 7713430447934781, 8114895595811233, 8209510986022041, 8398715614919241, 8563902159010211, 8691058183717011, 8784125927428599, 8794961983529061, 8962445733616225];
const actual = mergeArrays(arr1, arr2);
assert.deepStrictEqual(actual, expected);
});
it("should work with negative numbers", function() {
const arr1 = [-4627886, -4519642, -4297468, -3595491, -3205849, -3099984, -2661963, -2191758, -1832781, -1477357, -1471131, -1392509, -455864, 250084, 1071449, 1103428, 1305440, 1425801, 1452987, 1532745, 1820483, 1840962, 2148348, 2527057, 2706849, 2812677, 2835553, 2947423, 3258963, 3343546, 3493747, 3549879, 4084746, 4159368, 4323454, 4500321, 4642668, 4726588, 4913852, 4978575];
const arr2 = [-1436270, -1334241, -694068, -244520, 1285751, 1459606, 1914385, 2306127, 6078989, 6238068, 6361720, 8166625, 8603728, 8767531, 8886489, 9376132, 10396281, 12030028, 15631727, 17886989, 19258356, 19806234, 24055975, 24357484, 26714565, 26764202, 26854242, 27389146, 28432253, 28734874, 31107482, 31394025, 32185014, 32211940, 33730728, 35244675, 35505202, 35540727, 36742659, 38872319];
const expected = [-4627886, -4519642, -4297468, -3595491, -3205849, -3099984, -2661963, -2191758, -1832781, -1477357, -1471131, -1436270, -1392509, -1334241, -694068, -455864, -244520, 250084, 1071449, 1103428, 1285751, 1305440, 1425801, 1452987, 1459606, 1532745, 1820483, 1840962, 1914385, 2148348, 2306127, 2527057, 2706849, 2812677, 2835553, 2947423, 3258963, 3343546, 3493747, 3549879, 4084746, 4159368, 4323454, 4500321, 4642668, 4726588, 4913852, 4978575, 6078989, 6238068, 6361720, 8166625, 8603728, 8767531, 8886489, 9376132, 10396281, 12030028, 15631727, 17886989, 19258356, 19806234, 24055975, 24357484, 26714565, 26764202, 26854242, 27389146, 28432253, 28734874, 31107482, 31394025, 32185014, 32211940, 33730728, 35244675, 35505202, 35540727, 36742659, 38872319];
const actual = mergeArrays(arr1, arr2);
assert.deepStrictEqual(actual, expected);
});
it("should work with zeros", function() {
const arr1 = [-4627886, -4519642, -4297468, -3595491, -3205849, -3099984, -2661963, -2191758, -1832781, -1477357, -1471131, -1392509, -455864, 0, 250084, 1071449, 1103428, 1305440, 1425801, 1452987, 1532745, 1820483, 1840962, 2148348, 2527057, 2706849, 2812677, 2835553, 2947423, 3258963, 3343546, 3493747, 3549879, 4084746, 4159368, 4323454, 4500321, 4642668, 4726588, 4913852, 4978575];
const arr2 = [-1436270, -1334241, -694068, -244520, 0, 1285751, 1459606, 1914385, 2306127, 6078989, 6238068, 6361720, 8166625, 8603728, 8767531, 8886489, 9376132, 10396281, 12030028, 15631727, 17886989, 19258356, 19806234, 24055975, 24357484, 26714565, 26764202, 26854242, 27389146, 28432253, 28734874, 31107482, 31394025, 32185014, 32211940, 33730728, 35244675, 35505202, 35540727, 36742659, 38872319];
const expected = [-4627886, -4519642, -4297468, -3595491, -3205849, -3099984, -2661963, -2191758, -1832781, -1477357, -1471131, -1436270, -1392509, -1334241, -694068, -455864, -244520, 0, 0, 250084, 1071449, 1103428, 1285751, 1305440, 1425801, 1452987, 1459606, 1532745, 1820483, 1840962, 1914385, 2148348, 2306127, 2527057, 2706849, 2812677, 2835553, 2947423, 3258963, 3343546, 3493747, 3549879, 4084746, 4159368, 4323454, 4500321, 4642668, 4726588, 4913852, 4978575, 6078989, 6238068, 6361720, 8166625, 8603728, 8767531, 8886489, 9376132, 10396281, 12030028, 15631727, 17886989, 19258356, 19806234, 24055975, 24357484, 26714565, 26764202, 26854242, 27389146, 28432253, 28734874, 31107482, 31394025, 32185014, 32211940, 33730728, 35244675, 35505202, 35540727, 36742659, 38872319];
const actual = mergeArrays(arr1, arr2);
assert.deepStrictEqual(actual, expected);
});
});