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.
let foo x = 0
module Tests = struct
open OUnit
let suite =
let test_with input expected =
let format_input = Printf.sprintf "x = %d" in
assert_equal expected (foo input) ~msg:(format_input input) ~printer:string_of_int
in [
"foo" >:: (fun _ ->
test_with 1 1;
test_with 2 2;
);
]
end
Using your programming language of choice from the drop-down, write a function that takes a string and returns the count of instances of the letter "a" in the string.
The comparison should be case-insensitive, i.e. both "a" and "A" should count.
For example, "abcABC" should return 2.
function count(string) {
}
// 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() {
assert.strictEqual(count('abcABC'), 2);
});
});
An algorithm best suited for the calculation of very large fibonnaci numbers, but works very well for simply populating a list or dictionary with these values.
The algorithm here uses a dictionary (which has O(1) access time) to hold values used by further calls. The implementation has a logarithmic time using an identity that allows us to calculate fib(2n) efficently from fib(n)
Uses the binary and operator because it is much faster in calculating if a number is odd or even.
If you wish to work with fibonnaci numbers past around the 4 millionth, I sincerely suggest you use a different language, as python has a hard time storing values much larger than this.
If you wish to use this with fibonnaci numbers that are modulo'd by some constant (as I used this for in problem 422 of project euler) then you may use it up to and beyond the 1st quadrillionth fibbonacci quiet easily
F = {0: 0, 1: 1, 2: 1}
def fib(n):
"""
Return the nth Fibonacci number in O(log(n)) time
"""
if n in F:
return F[n]
f1 = fib(n // 2 + 1)
f2 = fib((n - 1) // 2)
F[n] = (f1 * f1 + f2 * f2 if n & 1 else f1 * f1 - f2 * f2)
return F[n]
test.assert_equals(fib(10),55)
test.assert_equals(fib(6), 8)
test.assert_equals(fib(1000), 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875)
This function takes an integer as a Parameter and returns a list of factors for that number in ascending order. It uses a set and an optimized loop for preventing repeated values.
As far as I can tell, it works as well with large numbers. For some reason the code does not run on this platform.
def Factorize(num):
"""Factorizes an Integer"""
# set because values are unique
factset = set()
try:
num = int(num)
except:
raise Exception("Please Enter an Integer")
if num <= 0:
raise Exception("Entered Number must be grater than 0")
for i in range(1, int(num/2-1)):
result = num/i
if num % i == 0:
factset.add(i)
factset.add(int(result))
return sorted(list(factset))
num = 10246
# f-strings for formatting in a more pythonic way
print(f"Input = {num}")
print("Factors: ", end="")
# completely pointless, for aesthetics :)
print(f"{', '.join([str(i) for i in Factorize(num)])}")
# Output should be: 1, 2, 47, 94, 109, 218, 5123, 10246
# 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.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
This fonction analyze records of temperature to find the closest to zero.
If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5)
Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.
def TempCloseTo0(arr):
if len(arr)==0: return 0
else :
j=max(arr)
for i in arr:
if abs(i)<j :
j=abs(i)
return j
test.assert_equals(TempCloseTo0([45,37,-8,1,-2,3,14]),1)
test.assert_equals(TempCloseTo0([-5,-12,-4,-31]),-4)
test.assert_equals(TempCloseTo0([42,-5,12,21,5,24]),5)
test.assert_equals(TempCloseTo0([-5,-4,-2,12,-40,4,2,18,11,5]),2)
test.assert_equals(TempCloseTo0([42,-5,12,0,21,5,24]),0)
test.assert_equals(TempCloseTo0([]),0)
What's the most efficient algorithm to find the prime divisors of a number?
function divisors(x) {
if (x === 1) return [];
let sqrt = Math.sqrt(x);
let res = [];
while (x % 2 === 0) {
x /= 2;
res.push(2);
}
for (let n = 3; n <= sqrt && x > 1; n += 2) {
if (x % n === 0) {
x /= n;
sqrt = Math.sqrt(x);
res.push(n);
n -= 2;
}
}
if (x > 1) res.push(x);
return res;
}
// 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("Basic checks", function() {
it("Does the function work?", function() {
Test.assertDeepEquals(divisors(1), []);
Test.assertDeepEquals(divisors(2), [2]);
Test.assertDeepEquals(divisors(3), [3]);
Test.assertDeepEquals(divisors(4), [2, 2]);
Test.assertDeepEquals(divisors(12), [2, 2, 3]);
});
});
describe("Bigger numbers", function() {
it("Will it stand them?", function() {
Test.assertDeepEquals(divisors(485690777622330), [2, 3, 5, 7, 7, 17, 31, 31, 43, 47, 10007]);
Test.assertDeepEquals(divisors(6469693230), [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
Test.assertDeepEquals(divisors(114909829), [10453, 10993]);
});
});
describe("Random tests", () => {
it("Hardcoding-spoof", () => {
let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31];
for (let n = 4; n < 10; n++) {
let arr = [];
let product = 1;
for (let o = 0; o < n; o++) {
let value = primes[Math.floor(Math.random()*primes.length)]
arr.push(value);
product *= value;
}
arr = arr.sort((a, b) => a - b);
Test.assertDeepEquals(divisors(product), arr);
}
});
});
Return 100! Well, you might be thinking "Thats so easy, why is was this even created?" Well I have a job for you! Return 100 in the most creative way possible!
function returnhundred() {
return 10 ** 2;
}
// 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.
Test.assertEquals(returnhundred(),100)
fn foo() -> i32 {
0
}
#[test]
fn test_foo() {
println!("qwerty");
println!("asdfgh");
assert_eq!(foo(), 1);
}
You are given an array of Gem objects. Each Gem has a name
attribute, and a colors
attribute containing either a string, or an array of strings describing its color.
gems = [
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']}
];
Your task is to create a function getGemsOfColor
which takes a color string and a gems array as its argument, and returns the names of the gems of that color as an array, sorted alphabetically. If no gems of the given color are found, return an empty array.
Assumptions
- The colors property of each gem is never undefined.
- The gems input array is never empty.
Examples:
Given input array: gems = [
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']},
{ name: 'Emerald', colors: 'green' },
];
getGemsOfColor('blue', gems) // ['Aquamarine']
getGemsOfColor('green', gems) // ['Aquamarine', 'Emerald']
getGemsOfColor('white', gems) // []
function getGemsOfColor(color, gems) {
}
// 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.
const assert = require("chai").assert;
describe("Sample test cases", function() {
const gems = [
{ name: 'Emerald', colors: 'green' },
{ name: 'Amethyst', colors: 'purple' },
{ name: 'Garnet', colors: ['orange', 'red']},
{ name: 'Aquamarine', colors: ['blue', 'green']},
];
it("Get all blue gems", function() {
assert.strictEqual(getGemsOfColor('blue', gems), ['Aquamarine']);
});
it("Get all green gems", function() {
assert.strictEqual(getGemsOfColor('green', gems), ['Aquamarine', 'Emerald']);
});
it("Get all white gems (empty)", function() {
assert.strictEqual(getGemsOfColor('white', gems), []);
});
});
Task 2: "FizzBuzz"
Write a function fizzBuzz (n) that takes an integer as argument.
The function should output numbers from 1 to n to the console, replacing the numbers:
• multiples of three - on fizz;
• multiples of five - on buzz;
• multiples of three and five at the same time - on fizzbuzz.
function fizzBuzz(num) {
for (let i=1;i <= (num); i++) {
let fb = '';
if (i%3===0) {fb = fb + 'fizz'};
if (i%5===0) {fb = fb + 'buzz'};
if (fb==='') { console.log(i); } else { console.log(fb); };
}
};
fizzBuzz(15); //
// 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(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});