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.
Game of Life :
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.
Your task is to write a program to calculate the next generation of Conway's game of life, given any starting position.
You start with a two dimensional grid of cells, where each cell is either alive or dead.
The grid is finite, and no life can exist off the edges.
When calculating the next generation of the grid, follow these four rules:
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any dead cell with exactly three live neighbours becomes a live cell.
Examples: * indicates live cell, . indicates dead cell
Example input: (4 x 8 grid)
........
....*...
...**...
........
Example output:
........
...**...
...**...
........
function nextGeneration(grid) {
return grid.map((row, rowIndex) => {
return row.map((cell, colIndex) => {
if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
let neighboursCount = 0;
if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
if (cell === 1) {
if (neighboursCount === 2 || neighboursCount === 3 ) {
return 1;
}
} else {
if (neighboursCount === 3 ) {
return 1;
}
}
return 0;
}
return 0;
});
});
}
describe("Given empty grid", () => {
it("when next generation, should return empty", () => {
Test.assertDeepEquals(nextGeneration([]), []);
});
});
describe("Given a single cell", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0],
[0,1,0],
[0,0,0],
]), [
[0,0,0],
[0,0,0],
[0,0,0],
]);
});
});
describe("Given a cell with 1 neighbour at rows", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0],
[0,1,1,0],
[0,0,0,0],
]), [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
]);
});
});
describe("Given a cell with 2 neighbours at rows", () => {
it("when next generation, should live", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 2 neighbours at cols", () => {
it("when next generation, should live", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0],
[0,1,0],
[0,1,0],
[0,1,0],
[0,0,0],
]), [
[0,0,0],
[0,0,0],
[0,1,0],
[0,0,0],
[0,0,0],
]);
});
});
describe("Given a cell with 2 neighbours at \ (diagonal)", () => {
it("when next generation, should live", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,1,0,0],
[0,0,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 2 neighbours at / (diagonal)", () => {
it("when next generation, should live", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,0,0,1,0],
[0,0,1,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 4 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,1,0,0],
[0,1,0,1,0],
[0,0,1,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 5 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,1,1,1,0],
[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 6 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 7 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,0],
[0,1,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]);
});
});
describe("Given a cell with 8 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,0],
[0,1,1,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]);
});
});
describe("Given a die cell with 2 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a die cell with 3 neighbours", () => {
it("when next generation, should live", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]);
});
});
describe("Given a die cell with 4 neighbours", () => {
it("when next generation, should die", () => {
Test.assertDeepEquals(nextGeneration([
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
[0,1,0,1,0],
[0,0,0,0,0],
]), [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]);
});
});
Description
The Kolakoski Sequence is a infinite string of integers that describes it own construction. Each digit, in order, encodes the size of the run that some digit in the sequence appears.
The inital terms of the sequence seeded by [1,2] are:
1, 2, 2, 1, 1, 2, 1, 2, 2...
Notice that there is one 1, followed by two 2s, then two 1s. In general the repitition of digits can be described as:
1, 2, 2, 1, 1, 2, 1, 2, 2...
Or the sequence itself. That is the idea you will be exploring in this excercise.
Instructions
Task 1:
Generate_Kolakoski_Seq(seed, n)
Write a function that takes an array of symbols as a seed, and an integer n, and produces the Kolakoski Sequence for that seed up to the nth digit as a common separted string.
Generate_Kolakoski_Seq( [2,3], 11 )
returns "2,2,3,3,2,2,2,3,3,3,2"
Generate_Kolakoski_Seq( [2], 5 )
returns "2,2,2,2,2"
Task 2:
Find_Kolaskoski_Number(seed, n)
Write a function that takes an array of symbols and a large integer n, and returns the nth digit of the sequence as an integer.
Find_Kolaskoski_Number( [1,2], 10000 )
returns: 1
(There is a way to compute this without computing the whole sequence)
// takes int[] and int, returns string (comma seperated integers)
function Generate_Kolakoski_Seq(seed, n){
}
// takes int[] and int, returns int
function Find_Kolaskoski_Number(seed, n){
}
Function will recive N for length of array and point as index to pint at in that array.
For example, function should generate arrays like this for
N = 10, point = 7
[7,6,5,4,3,2,1,0,1,2]
N = 10, point = 4
[4,3,2,1,0,1,2,3,4,5]
function func(N, point) {
let start = 0; // starting position of array
let clonePoint = point; // clone for point to start counting from that number at begining of array
let arr = [...Array(N).keys()] // generate array and fill with 0 to 10
if(!(point > N)) {
arr.forEach((o, index) => {
index < point ? arr[index] = clonePoint-- : arr[index] = start++;
});
return arr;
}
return [];
}
// 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 return specified array format", function(){
let arr = new Array(7,6,5,4,3,2,1,0,1,2)
Test.assertSimilar(func(10, 7), arr);
});
it("should return specified array format", function(){
let arr = new Array(3,2,1,0,1,2,3,4,5,6)
Test.assertSimilar(func(10, 3), arr);
});
});
use Math;
proc chebyshev(n: int, v: real) : real
{
if (v > 1) {
return cosh(n * acosh(v));
} else if (v < -1) {
return (-1) ** n * cosh(n * acosh(-v));
} else {
return cos(n * acos(v));
}
}
Test.describe("First Order Chebyshev Polynomials");
Test.assertFuzzyEquals(chebyshev(0,0.5), 1);
Test.assertFuzzyEquals(chebyshev(1,0.5), 0.5);
Test.assertFuzzyEquals(chebyshev(2,0.5), -0.5);
Test.assertFuzzyEquals(chebyshev(5,0.52), 0.396166451);
Test.assertFuzzyEquals(chebyshev(3,26), 70226.0);
Hello World in Julia, since there's nothing on codewars ⊙﹏⊙.
println("Hello Julia!")
const hello = "world"
facts("Is hello world?") do
@fact hello => "world"
end
// add the values "codewars" to the websites array
var websites = ['codears'];
test('Codewars is in the array', () {
expect(websites.length, greaterThan(0));
expect(websites.length, equals(1));
expect(websites[0], equals('codewars'),reason:"codewars website should be present in the array", verbose:true);
});
A simple algorithm in Brainf**k that receives exactly 1 byte of input and prints it out as a numerical string. For example: "H" -> "72"
. Note that this algorithm only works for byte values up to 99 though - it won't handle three-digit bytes properly.
This program might be somewhat useful in writing a short and concise program that prints out the lyrics to "99 bottles of beer" since the lyrics are highly repetitive and involves printing out a lot of numbers in descending order. However, upon second thought, it might not be that useful at all since my program fails to preserve its input :p
,[->>+>+>>-<<----------[[+]>>+<<]>>[[+]<<<----------<+>>>>]<<<[->+>+<<]>>[-<<+>>]<<<<]>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]>++++++++++++++++++++++++++++++++++++++++++++++++.
Test.describe('The Program', function () {
Test.it('should correctly convert byte(72) into string "72"', function () {
Test.assertEquals(runBF('H'), '72');
});
Test.it('should work correctly for a few other examples below byte(100)', function () {
Test.assertEquals(runBF('c'), '99');
Test.assertEquals(runBF('a'), '97');
Test.assertEquals(runBF('F'), '70');
Test.assertEquals(runBF('!'), '33');
Test.assertEquals(runBF(String.fromCharCode(50)), '50');
Test.assertEquals(runBF("\0"), '0');
Test.assertEquals(runBF('0'), '48');
Test.assertEquals(runBF('7'), '55');
Test.assertEquals(runBF('A'), '65');
});
Test.it('unfortunately doesn\'t work properly above byte(99) :(', function () {
console.log('Output for byte(127): ' + runBF(String.fromCharCode(127)));
});
});
If we have planty of objects like this: ['a', 'b', 'c']
then we can build all permutation:
0 – abc 1 – acb 2 – bac 3 – bca 4 – cab 5 – cba
Any of them permutations may be calculated by their index number without calculated others.
import math
class SequencePermutation(object):
def __init__(self, plenty_object, count_item):
self.plenty_object = list(plenty_object)
self.count_item = count_item
self.power_plenty = len(plenty_object)
@staticmethod
def calc_count_permutation(n, k):
return int(math.factorial(n) / math.factorial(n - k))
def __getitem__(self, index):
current_mod = index
plenty = self.plenty_object.copy()
for i in range(self.count_item - 1):
perm_count = self.calc_count_permutation(self.power_plenty - i - 1, self.count_item - i - 1)
current_index = current_mod // perm_count
yield plenty[current_index]
plenty.pop(current_index)
current_mod = current_mod % perm_count
yield plenty[current_mod]
Test.describe("SequencePermutation")
Test.describe("should correct calculate for permutations with three by three")
sq_per_master = SequencePermutation(('a','b','c'), 3)
Test.assert_equals(['a', 'b', 'c'], list(sq_per_master[0]))
Test.assert_equals(['a', 'c', 'b'], list(sq_per_master[1]))
Test.assert_equals(['c', 'a', 'b'], list(sq_per_master[4]))
Test.describe("should correct calculate for permutations with one item")
sq_per_master = SequencePermutation(('a','b','c'), 1)
Test.assert_equals(['a'], list(sq_per_master[0]))
Test.assert_equals(['b'], list(sq_per_master[1]))
Test.assert_equals(['c'], list(sq_per_master[2]))
Test.describe("should correct calculate for permutations with two items")
sq_per_master = SequencePermutation(('a','b','c'), 2)
Test.assert_equals(['a', 'b'], list(sq_per_master[0]))
Test.assert_equals(['a', 'c'], list(sq_per_master[1]))
Test.assert_equals(['c', 'a'], list(sq_per_master[4]))
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
function calculate_multiples {
$SUM = 0
1..999 | ForEach-Object {
if (!( $_ % 3) -OR !($_ % 5)) {
$Sum += $_
}
}
return $Sum
}
# You can test with Pester (https://github.com/pester/Pester)
# TODO: replace with your own tests (TDD), these are just here to demonstrate usage.
$Results = calculate_multiples
Describe 'My Solution' {
It 'Should return 233168' {
$Results | Should be 233168
}
}
The Ulam Sequence is a recursive sequence that begins with two numbers; for example [1, 2]. The next number in the sequence is the smallest unique sum of any 2 numbers in the sequence.
Example:
[u0, u1] = [1, 2]
u2 = 1 + 2 = 3 --> [1, 2, 3]
u3 = 1 + 3 = 4 --> [1, 2, 3, 4]
u4 = 4 + 2 = 6 --> [1, 2, 3, 4, 6]
Notice that 5 is left out. Though 5 is smaller than 6, 5 can be written as a sum in 2 different ways: 4 + 1 and 3 + 2; therefore, it is not unique; whereas, 6 is unique since it is only the sum of 4 + 2.
Write a code that generates an Ulam Sequence of length n and starts with u0, u1.
Ex:
f(u0=1, u1=2, n=10) = [1, 2, 3, 4, 6, 8, 11, 13, 16, 18]
def ulam_sequence(u0, u1, n):
u = [u0, u1]
nn = u[-1] + 1
while len(u) < n:
count = 0
for i in u:
if nn - i in u and nn - i != i:
count += 1
if count >= 3:
break
nn += 1
if count == 2:
u.append(nn)
nn += 1
return u
test.assert_equals(ulam_sequence(3, 4, 5), [3, 4, 7, 10, 11])
test.assert_equals(ulam_sequence(1, 2, 10), [1, 2, 3, 4, 6, 8, 11, 13, 16, 18])
test.assert_equals(ulam_sequence(2, 3, 8), [2, 3, 5, 7, 8, 9, 13, 14])