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 collection of JSON with a structure of id, name and description. Sort them by name.
e.g:
[ { "id": 100, "name": "John", "description":"A real gentleman" }, { "id": 101, "name": "Gary", "description":"A clever person" }, { "id": 102, "name": "Anna", "description":"A charming lass" }, { "id": 103, "name": "Zoltan", "description":"A financial wizard" } ]
should be:
[ { "id": 102, "name": "Anna", "description":"A charming lass" }, { "id": 101, "name": "Gary", "description":"A clever person" }, { "id": 100, "name": "John", "description":"A real gentleman" }, { "id": 103, "name": "Zoltan", "description":"A financial wizard" } ]
function sortByName(collection) {
return collection.sort((a, b) => {
let alpha = a.name.toLowerCase();
let beta = b.name.toLowerCase();
return (alpha < beta) ? -1 : (alpha > beta) ? 1 : 0;
});
}
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Solution", function() {
it("Determine if given collection is sorted by name",
function() {
assert.deepEqual(sortByName([
{ "id": 100, "name": "John", "description":"A real gentleman" },
{ "id": 101, "name": "Gary", "description":"A clever person" },
{ "id": 102, "name": "Anna", "description":"A charming lass" },
{ "id": 103, "name": "Zoltan", "description":"A financial wizard" }
]), [
{ "id": 102, "name": "Anna", "description":"A charming lass" },
{ "id": 101, "name": "Gary", "description":"A clever person" },
{ "id": 100, "name": "John", "description":"A real gentleman" },
{ "id": 103, "name": "Zoltan", "description":"A financial wizard" }
])
});
});
Combinations using big ints
#load "nums.cma";; open Num;;
let rec prod a b=
let cmp=Num.compare_num a b in
if cmp>0 then Num.num_of_int 0 else if cmp==0 then b else
let b1=Num.sub_num b (Num.num_of_int 1) in
Num.mult_num b (prod a b1);;
let num_comb n p=
let nlp=Num.sub_num n p in
let (p,nlp)=if (Num.compare_num p nlp)>0 then (nlp,p)else(p,nlp) in
let one=(Num.num_of_int 1) in
Num.div_num (
prod (Num.add_num nlp one) n
) (prod one p);;
let comb (n:int) (p:int) :string=Num.string_of_num (num_comb (Num.num_of_int n) (Num.num_of_int p));;
comb 76 12 |> print_endline;;
(* TODO: replace with your own tests, these are just how-to examples.
* OUnit Spec example:
* See http://ounit.forge.ocamlcore.org/api-ounit-2.0.0 for documentation
*)
module Tests = struct
open OUnit
let suite = [
"Suite Name" >:::
[
"Test Name" >:: (fun _ ->
assert_equal "31022118677225" (comb 76 12)
)
]
]
;;
end
Kevin McCallister now grown and living in New York, is serving time on probation. He has an ankle monitor and can't be more than 2025m from his house. (That is a direct line from his house) He wanders around New York city blocks which are 80 meters North to South and 274 meters East to West. We need to know if Kevin is going to jail for violation of probation.
you will recieve a list of number of blocks travel in each direction and have to figure out if on his journey he ever ended up violating his 2000m distance(He can Travel 2000m but not anything more). Kevin checks his distance from home every few blocks. (assume he walked the blocks for check-in in the safest way to avoid going over 2000m)
each member of the list will be another list formatted as below were the number is the number of blocks traveled in that direction:
(#North,#East,#South,#West)
You will have at least 2 check in so list length >= 2
List will always be integers.
Your Result with return True is he ends up going to jail or False if he is safe to foil robbers another day.
def goesToJail(directions):
location=[0,0]
#North+East are postive numbers West+South are a negative numbers
for direction in directions:
location[0]=location[0]+(direction[0]*80)-(direction[2]*80)
location[1]=location[1]+(direction[1]*274)-(direction[3]*274)
#a squared + b squared = c squared
if( (location[0]**2+location[1]**2)**(1/2) > 2000 ):
return True
return False
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("")
def test_case():
test.assert_equals(goesToJail([[1,0,0,0],[0,0,1,0]]), False)
test.assert_equals(goesToJail([[9,0,0,4],[15,0,0,1],[3,5,3,1],[0,1,5,0]]), True)
test.assert_equals(goesToJail([[2,0,2,4],[3,0,3,1],[0,1,5,0],[1,0,3,4]]), True)
test.assert_equals(goesToJail([[1,0,0,3],[3,0,1,2],[5,0,1,0]]), False)
test.assert_equals(goesToJail( [[0,0,0,3],[2,0,1,0],[3,0,1,5],[0,3,1,0]] ), True)
Given a and b, n is the sum of the squares of a and b. e.g. (a,b)=(1,2)-->n=5
Break 2n into a sum of two squares and output!
e.g.2n=10-->(a,b)=(1,3)
(yes it is always possible :)
Think before you code!Good luck!
using namespace std;
pair<int, int> two_squares(int a,int b){
return make_pair(b-a,a+b);
}
// TODO: Replace examples and use TDD by writing your own tests
using namespace std;
Describe(Small_examples)
{
It(Example_tests)
{
Assert::That(two_squares(1,2), Equals(make_pair(1,3)));
Assert::That(two_squares(2,6), Equals(make_pair(4,8)));
Assert::That(two_squares(2,5), Equals(make_pair(3,7)));
}
};
Implement a function, which gets string, splits it onto numbers and words. After it wraps numbers in div
tag, words in span
and remove spaces at the end and begining.
Example:
- transformText("Two 2"):<span>Two</span><div>2</div>
- transformText('12 people in 2 rooms'): <div>12</div><span>people in</span><div>2</div><span>rooms</span>
function transformText(string) {
const regExp = /[\D]+|([\d+\.]+)/g
const numsRegExp = /[0-9\.]+/g
const splited = string.match(regExp)
const res = splited.map((item) => {
if (numsRegExp.test(item)) {
return `<div>${item}</div>`
} else {
return `<span>${item.replace(/^\s|\s$/g, "")}</span>`
}
})
return res.join("")
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("Example", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
assert.strictEqual(transformText('Number 2'), '<span>Number</span><div>2</div>')
assert.strictEqual(transformText('12 people in 2 rooms'), '<div>12</div><span>people in</span><div>2</div><span>rooms</span>')
assert.strictEqual(transformText('from 2 to 9'), '<span>from</span><div>2</div><span>to</span><div>9</div>')
});
});
Summary
In this kata you have to make a function named converter
.
What Does the converter
Function do?
It converts a negitive number to a positive one and a positive number to a negetive one.
- Check the parameter
- If it is not integer just
return False
-
Check the word if it is negitive or positive
-
Convert the number
- if negitive convert to positive
- if positive convert to negitive
Now just return the final answer... Done!
Examples
-
# Negitive to Positive convert(-1) === 1 convert(-72) === 72 # Positive to Negitive convert(24) === -24 convert(32) === -32 # Invalid Sequences convert("23") === False convert("-94") === False convert(True) === False convert([123, 321]) == False convert({123, 1232, 34}) === False
def convert(number):
if isinstance(number, int):
# Check if number is negitive
if number < 0:
# negitive -> positive
return (abs(number))
else:
# positive -> negitive
return (number * -1)
else:
return False
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("These are your Test results")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(convert(-1), 1)
test.assert_equals(convert(234), -234)
test.assert_equals(convert(-3472398574349325748132), 3472398574349325748132)
test.assert_equals(convert([2, 42, "3"]), False)
I have undertaken a study of recursion. I find myself reaching for it fairly consistenly to solve different Kata but often find my understanding of certain concepts laking enough that I end up going with an iterative approach instead.
the goal of this was to produce in exponent notation the prime factors of a number.
The problem with this one is that because I created a new dictionary at the top of primeFactoring and because there is not an array or dictionary passed into it I ended up creating another function (factorPart) to fill the dictionary.
even ignoring all the messy formating code the solution seems convoluted.
What would be a better approach and what concepts should be understood to improve this answer?
def primeFactoring(n):
dict = {}
print(n)
factorPart(n,dict)
output = ""
for d, v in dict.items():
if v > 1:
output += str(d)+"^"+str(v)+" X "
else: output += str(d)+" X "
output = output.strip(" X ")
print(dict) #key being the factor and value being the number of occurrences
print(output) #formated in exponant form
return output
def factorPart(num, dict):
x = 2
while x <= num:
if num / x == num // x:
dict[x] = dict[x] + 1 if x in dict else 1
return factorPart(num // x, dict)
x+=1
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(primeFactoring(11223144), "2^3 X 3^3 X 223 X 233")
Write a function that takes an integer array, and without sorting the array returns its largest and smallest number as a two element array.
INPUT: OUTPUT:
{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1} -> {-9,-1}
public class HighLow{
public static int[] printLargestAndSmallest(int[] nums){
int low[] = nums;
int high[] = nums;
int lowest = nums[0];
int highest = nums[0];
int count = 0;
for(int i = 0; i < nums.length;i+=2){
int num1 = nums[i];
int num2 = nums[i+1];
if(num1<num2){
low[count] = num1;
high[count] = num2;
count++;
}else{
low[count] = num2;
high[count] = num1;
count++;
}
lowest = low[0];
highest = high[0];
for(int j = 1; j < low.length; j++){
if(low[j] < lowest){
lowest = low[j];
}
}
for(int j = 1; j < high.length; j++){
if(high[j] > highest){
highest = high[j];
}
}
}
int[] finalHighLow = new int[2];
finalHighLow[0] = lowest;
finalHighLow[1] = highest;
return finalHighLow;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD by writing your own tests
public class SolutionTest {
int[] input = new int[]{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1};
int[] output = new int[]{-9,-1};
@Test
public void allNegatives() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
}
int[] input2 = new int[]{1,-2,3,-4,-5,-6,-7,-8,-9};
int[] output2 = new int[]{-9,3};
@Test
public void test2() {
assertArrayEquals(output, HighLow.printLargestAndSmallest(input));
}
}
Make a function that determines if a number is odd.
public static class Kata
{
public static bool IsOdd(int input)
{
return ((input % 10 == 1) || (input % 10 == 3) || (input % 10 == 5) || (input % 10 == 7) || (input % 10 == 9));
}
}
namespace Solution {
using NUnit.Framework;
using System;
// TODO: Replace examples and use TDD by writing your own tests
[TestFixture]
public class SolutionTest
{
[Test]
public void MyTest()
{
Assert.AreEqual(true, Kata.IsOdd(1));
Assert.AreEqual(false, Kata.IsOdd(2));
Assert.AreEqual(true, Kata.IsOdd(13));
Assert.AreEqual(false, Kata.IsOdd(18284));
}
}
}
Given a list of strings, return an object that represents the tree structure.
getTree([
'src/main.js',
'src/second.js',
'src/components/header.js',
'src/components/helpers/sidebar.js'
]) -> {
"src": {
"main.js": "file",
"second.js": "file",
"components": {
"header.js": "file",
"helpers": {
"sidebar.js": "file",
},
}
}
}
function getTree(strings) {
// code
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
const arr = [
'src/main.js',
'src/second.js',
'src/components/header.js',
'src/components/helpers/sidebar.js'
];
const result = {
"src": {
"main.js": "file",
"second.js": "file",
"components": {
"header.js": "file",
"helpers": {
"sidebar.js": "file",
},
}
}
}
describe("Solution", function() {
it("should return object", function() {
assert.deepEqual(getTree(arr), result)
});
});