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.
Given a binary tree defined as below, return the height of the tree.
class Node {
constructor(val) {
this.data = val;
this.left = null;
this.right = null;
}
}```
Example:(TBD)
function treeHeight(node)
{
if (node == null)
return 0;
else
{
/* compute the height of each subtree */
var lheight = treeHeight(node.left);
var rheight = treeHeight(node.right);
/* use the larger one */
if (lheight > rheight)
{
return(lheight + 1);
}
else {
return(rheight + 1);
}
}
}
const chai = require("chai");
const assert = chai.assert
a = new Node("A")
var b = new Node("B");
var c = new Node("C");
var d = new Node("D");
var e = new Node("E");
var f = new Node("F");
var g = new Node("G");
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
;
chai.config.truncateThreshold = 0;
describe("treeHeight", function() {
it("given null", function() {
assert.equal(treeHeight(null), 0);
});
it("given a", function() {
assert.equal(treeHeight(a), 3);
});
it("given b", function() {
assert.equal(treeHeight(b), 2);
});
it("given c", function() {
assert.equal(treeHeight(c), 2);
});
it("given d", function() {
assert.equal(treeHeight(d), 1);
});
it("given e", function() {
assert.equal(treeHeight(e), 1);
});
it("given f", function() {
assert.equal(treeHeight(f), 1);
});
it("given g", function() {
assert.equal(treeHeight(g), 1);
});
// TODO: non-existing value
});
Write a method that returns a greater number.
Perfect task for begginers!Welcome to CodeWars!
public class Math
{
public int Max(int a, int b)
{
return (a > b) ? a : b;
}
}
namespace Solution {
using NUnit.Framework;
[TestFixture]
public class SolutionTest
{
private Math math;
[SetUp]
public void SetUp()
{
math = new Math();
}
[Test]
[TestCase(1,2,2)]
[TestCase(2,1,2)]
[TestCase(1,1,1)]
public void Max_WhenCalled_ReturnTheGreaterArgument(int a, int b, int expectedResult)
{
var result = math.Max(a,b);
Assert.That(result, Is.EqualTo(expectedResult));
}
}
}
# You can test with testthat (http://r-pkgs.had.co.nz/tests.html#test-structure)
# TODO: replace with your own tests (TDD), these are just here to demonstrate usage.
test_that("example", {
expect_equal(actual, expected)
})
# You can test with testthat (http://r-pkgs.had.co.nz/tests.html#test-structure)
# TODO: replace with your own tests (TDD), these are just here to demonstrate usage.
test_that("example", {
expect_equal(actual, expected)
})
Write an implementation of a method:
- @param a - array of numbers, array length can be greater than 10 million.
- @return - array of numbers with no duplicates. The order of numbers in the original array must be preserved. The last element of the duplicates must be kept, e.g. for {2.0, 1.0, 4.0, 2.0, 3.0} the correct solution is {1.0, 4.0, 2.0, 3.0}, not {2.0, 1.0, 4.0, 3.0}!
In case there is an element less than 0.0 in the input array, output array{-1.0} should be given.
For example, for {2.0, 3.0, -1.0, 5.0} the processing should result {-1.0}.
Good luck...
import java.util.*;
class DeleteDuplicates {
public static double[] delete(double[] a) {
for(int i = 0; i < a.length / 2; i++) {
double temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
Set<Double> set = new LinkedHashSet<>();
for (double v : a) {
set.add(v);
if(v < 0.0)
return new double[]{-1.0};
}
List<Double> list = new ArrayList<>(set);
Collections.reverse(list);
double[] array_here = list.stream().mapToDouble(Double::doubleValue).toArray();
//incorrect (too long and witout accuracy)
return array_here;
}
}
import java.util.*;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
@Test
void test1() {
assertEquals(Arrays.toString(DeleteDuplicates.delete(new double[]{2.0, 1.0, 4.0, 2.0, 3.0})),
"[1.0, 4.0, 2.0, 3.0]");
}
@Test
void test2() {
assertEquals(Arrays.toString(DeleteDuplicates.delete(new double[]{2.0, 3.0, -1.0, 5.0})),
"[-1.0]");
}
@Test
void test3() {
assertEquals(Arrays.toString(DeleteDuplicates.delete(new double[]{2.2131141516, 1.234, 4.234,
2.2131, 3.0, 3.0,
12.321, 2.2131141516})),
"[1.234, 4.234, 2.2131, 3.0, 12.321, 2.2131141516]");
}
}
Write a function that returns string:
"Fizz" when the number is divisible by 3.
"Buzz" when the number is divisible by 5.
"FizzBuzz" when the number is divisible by 3 and 5.
public class FizzBuzz
{
public string GetOutput(int number)
{
if ((number % 3 == 0) && (number % 5 == 0))
return "FizzBuzz";
else if (number % 3 == 0)
return "Fizz";
else if (number % 5 == 0)
return "Buzz";
else return number.ToString();
}
}
namespace Solution {
using NUnit.Framework;
[TestFixture]
public class SolutionTest
{
[Test]
public void GetOutput_InputIsDivisibleBy3_ReturnFizz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(3);
Assert.That(result, Is.EqualTo("Fizz"));
}
[Test]
public void GetOutput_InputIsDivisibleBy5_ReturnBuzz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(5);
Assert.That(result, Is.EqualTo("Buzz"));
}
[Test]
public void GetOutput_InputIsDivisibleBy5And3_ReturnFizzBuzz()
{
var fizzbuzz = new FizzBuzz();
var result = fizzbuzz.GetOutput(15);
Assert.That(result, Is.EqualTo("FizzBuzz"));
}
}
}
Write the function optimal_gifts(gifts: int, recipients: int) -> int
You are given gifts
number of gifts and recipients
number of recipients.
Return the maximum number of recipients that can receive 8 gifts under the following conditions:
- No recipient can recieve no gifts
- No recipient can recieve 4 gifts
- No recipient can receive more than 8 gifts
- All gifts must be distibuted unless all recipients are already recveiving 8 gifts
Examples:
-
optimal_gifts(8, 2)
returns 0 -
optimal_gifts(12, 2)
returns 0 -
optimal_gifts(8, 1)
returns 1 -
optimal_gifts(24, 2)
returns 2 -
optimal_gifts(24, 3)
returns 3 -
optimal_gifts(24, 4)
returns 2
def optimal_gifts(gifts: int, recipients: int) -> int:
kids = [1 for _ in range(gifts)]
gifts -= recipients
for i in range(recipients):
while (gifts > 0 and kids[i] < 8):
kids[i] += 1
gifts -= 1
eigths = sum([1 if kid == 8 else 0 for kid in kids])
if 4 in kids:
eigths -= 1
return eigths
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Examples")
def test_group():
@test.it("Example 1")
def test_case1():
test.assert_equals(optimal_gifts(8, 2), 0)
@test.it("Example 2")
def test_case1():
test.assert_equals(optimal_gifts(12, 2), 0)
@test.it("Example 3")
def test_case1():
test.assert_equals(optimal_gifts(8, 1), 1)
@test.it("Example 4")
def test_case1():
test.assert_equals(optimal_gifts(24, 2), 2)
@test.it("Example 5")
def test_case1():
test.assert_equals(optimal_gifts(24, 3), 3)
@test.it("Example 6")
def test_case1():
test.assert_equals(optimal_gifts(24, 4), 2)
Always forget how to exact determine programmer's day. Some code may help do it exactly and without heavy calculations.
# How to check that target date is Programmer's day?
def is_programmers_day(date):
return date.timetuple().tm_yday == 256
import codewars_test as test
from solution import is_programmers_day
from datetime import datetime, date
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(is_programmers_day(datetime(2021, 9, 13)), True)
test.assert_equals(is_programmers_day(datetime(2020, 9, 12)), True)
test.assert_equals(is_programmers_day(date(2019, 9, 13)), True)
test.assert_equals(is_programmers_day(date(2016, 9, 12)), True)
test.assert_equals(is_programmers_day(date(2000, 9, 12)), True)
package kata
func Solution(str string) []string {
var res []string
if len(str) % 2 != 0 {
str += "_"
}
for i := 0; i < len(str); i+=2 {
res = append(res, str[i:i+2])
}
return res
}
// TODO: replace with your own tests (TDD). An example to get you started is included below.
// Ginkgo BDD Testing Framework <http://onsi.github.io/ginkgo/>
// Gomega Matcher Library <http://onsi.github.io/gomega/>
package kata_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "codewarrior/kata"
)
var _ = Describe("Test Example", func() {
// It("should test that the solution returns the correct value", func() {
// Expect(Solution(1)).To(Equal(2))
// })
})
import random
def approximate_pi(n):
# n - number of generated points, more points => more accuracy
number_of_points_in_circle = 0
total_number_of_points = 0
for _ in range(n):
x = random.uniform(0, 1)
y = random.uniform(0, 1)
# sqrt(x^2 + y^2) < 1 => (x,y) inside circle with r = 1; x^2 + y^2 < 1 => (x,y) inside circle
if (x**2 + y**2 < 1):
number_of_points_in_circle += 1
total_number_of_points += 1
# Pi * r^2 / (2r)^2 = number_of_points_in_circle / total_number_of_points (ratio of the area of a circle with radius r to the area of a square with side = 2r)
# r=1 => Pi = number_of_points_in_circle * 4 / total_number_of_points
return (number_of_points_in_circle * 4) / total_number_of_points
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Approximate Pi")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(round(approximate_pi(1000000), 2), 3.14)
Simple test to see how creating Kumites work
This function should return the sum of a and b.
(1,2) => 3
(5,9) => 14
(-3,8) => 5
def add(a, b):
return a + b
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# 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)
test.assert_equals(solution.add(1,2),3)
test.assert_equals(solution.add(5,9),14)
test.assert_equals(solution.add(-3,8),5)