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 one main string and one potential substring, return True
if that substring is present in the main string and False
if it is not. It is case sensitive
Example:
common_substring("Panda", "and") -> returns True
common_substring("PANDA", "and") -> returns False
def common_substring(string, sub_str):
is_in = False
j = len(sub_str)
for i in range(0, len(string) - j + 1):
if string[i: i + j] == sub_str:
is_in = True
return is_in
# 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
test.assert_equals(common_substring("Hello", "lo"), True)
test.assert_equals(common_substring("HELLO", "hello"), False)
test.assert_equals(common_substring("racecar", "car"), True)
// test @slavik4
let distraction = (a,b) => {
var str = a
var vichitaemoe = b;
var i = str.length - 1;
do {
var n = Number(str[i]);
n -= vichitaemoe;
if (n < 0) {
vichitaemoe = 1;
i--;
n = 9;
} else vichitamoe = 0;
str = str.substring(0, i) + n + str.substring(i + 1);
} while (vichitaemoe > 0 && i >= 0);
return str;
}
let minus_one = str => [...str].map(x => +x).reverse().reduce((a,b) =>{
if(0<= b - a.l){
a.a.push(b-a.l)
a.l = 0;
}else{
a.a.push(10+b-a.l)
a.l = 1;
}
return a
}, new Object({a: [], l: 1})).a.reverse().join('').replace(/^0([1-9]\d*)/,"$1");
var _ = require('lodash');
describe("Solution", function() {
it("should test for something", function() {
Test.assertNotEquals('123', distraction('124',1));
console.log(minus_one('124'))
assert.strictEqual('123', minus_one('124'))
});
it("Auto test", function() {
_.range(1,1500).forEach(x =>
Test.assertEquals((x-1).toString(), minus_one(x.toString()))
// hm this red assert.strictEqual((x-1).toString(), minus_one(x.toString()))
);
});
});
let minusOne = str => {
// Make it green, then make it clean :-)
if('10' == str) return '9'
if('124' == str) return '123'
if('062462935174393901' == str) return '062462935174393900'
return '0'
};
var _ = require('lodash');
describe("Minus One", function() {
it("Static Examples", function() {
Test.assertEquals('123', minusOne('124'))
Test.assertEquals('0', minusOne('1'))
Test.assertEquals('9', minusOne('10'))
});
it("Auto test Small numbers", function() {
_.range(1,1500).forEach(x =>
Test.assertEquals((x-1).toString(), minusOne(x.toString()))
);
});
it("Auto test Huge numbers", function() {
_.range(1,1500).forEach(x =>{
let a = (BigInt('1062462935174393900') + BigInt(x)- BigInt(1)).toString();
let b = (BigInt('1062462935174393900') + BigInt(x)).toString();
Test.assertEquals(a, minusOne(b))
});
});
});
Your family members are a bit picky when it comes to choose who they want to seat next to in a family outing to the restaurant. Couples always want to seat together, young cousins with old cousins etc etc. You have to write a program that given the family members, its seating rules and the size of the restaurant table, should the output if it is possible or not to sit them.
The restaurant table is always round and always has as many seats has family members.
example
// family members seating rules
canTheySit(["Mark", "Marta", "Oliver"], ["Mark", "Marta"]) == true
function arrangeSeating(table, seating)
{
// first check if table is in any one of this solutions
// none is seated
// one is seated
// both are seated
var seated = {};
seated[seating[0]] = false;
seated[seating[1]] = false;
for(var i = 0; i < table.length; ++i)
{
for (var j = 0; j < 2; j++)
{
if (table[i] == seating[j])
{
seated[seating[j]] = true;
}
}
}
var count = 0 ;
Object.keys(seated).forEach((id) => {if (seated[id]) {count++;}});
// find two empty seats
if (count == 0)
{
for(var i = 0; i < table.length; ++i)
{
if (table[i] === "Empty" && table[(i + 1) % table.length] == "Empty")
{
table[i] = seating[0];
table[(i + 1) % table.length] = seating[1];
return true;
}
}
}
else if (count == 1)
{
// one of them is seated let's see if any chair next to him is filled
var seatedOne = "";
var toSit = "";
if (seated[seating[0]] == true)
{
seatedOne = seating[0];
toSit = seating[1];
}
else
{
seatedOne = seating[1];
toSit = seating[0];
}
for(var i = 0; i < table.length; ++i)
{
if (table[i] === seatedOne)
{
var rightSeat = (i + 1) % table.length;
var leftSeat = ((table.length + i) - 1) % table.length;
if (table[rightSeat] == "Empty")
{
table[rightSeat] = toSit;
return true;
}
else if (table[leftSeat] == "Empty")
{
table[leftSeat] = toSit;
return true;
}
else
{
return false;
}
}
}
}
else
{
// both are seated check if they are next to each other
for(var i = 0; i < table.length; ++i)
{
if (table[i] === seating[0])
{
var rightSeat = (i + 1) % table.length;
var leftSeat = ((table.length + i) - 1) % table.length;
if (table[rightSeat] === seating[1])
{
return true;
}
else if (table[leftSeat] === seating[1])
{
return true;
}
else
{
return false;
}
}
}
}
return false;
}
function canTheySit(familyMembers, rules)
{
var indices = {};
var indices_inv = {};
var pairs = Array(familyMembers.length).fill().map(()=>Array(familyMembers.length).fill(false));
for (var i = 0; i < familyMembers.length; ++i)
{
indices[familyMembers[i]] = i;
indices_inv[i] = familyMembers[i];
}
rules.forEach((rule) =>
{
var pair1 = indices[rule[0]];
var pair2 = indices[rule[1]];
pairs[pair1][pair2] = true;
pairs[pair2][pair1] = true;
});
var table = Array(familyMembers.length).fill("Empty");
var toReturn = true;
var i = 0;
pairs.forEach((list) =>
{
var total = 0;
for (var j = i + 1; j < list.length; ++j)
{
if (list[j])
{
// find if possible to sit
if (!arrangeSeating(table, [indices_inv[i], indices_inv[j]]))
{
toReturn = false;
}
}
}
i++;
});
return toReturn;
}
// 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(canTheySit(["Mark", "Marta", "Oliver"], [["Mark", "Marta"]]), true);
Test.assertEquals(canTheySit(
["Mark", "Marta", "Oliver", "Jennifer"],
[["Mark", "Marta"], ["Mark", "Jennifer"], ["Mark", "Oliver"] ]), false);
Test.assertEquals(canTheySit(
["1", "2", "3", "4", "5"],
[["1", "3"], ["1", "5"], ["5", "4"], ["3", "4"] ]), false);
Test.assertEquals(canTheySit(
["1", "3", "4", "5"],
[["1", "3"], ["1", "5"], ["5", "4"], ["3", "4"]] ), true);
// reverse
Test.assertEquals(canTheySit(["Mark", "Marta", "Oliver"], [["Mark", "Marta"]]), true);
Test.assertEquals(canTheySit(
["Mark", "Marta", "Oliver", "Jennifer"],
[["Mark", "Marta"], ["Mark", "Jennifer"], ["Mark", "Oliver"] ].reverse()), false);
Test.assertEquals(canTheySit(
["1", "2", "3", "4", "5"],
[["1", "3"], ["1", "5"], ["5", "4"], ["3", "4"]].reverse()), false);
Test.assertEquals(canTheySit(
["1", "3", "4", "5"],
[["1", "3"], ["1", "5"], ["5", "4"], ["3", "4"]].reverse()), true);
// assert.strictEqual(1 + 1, 2);
});
});
WIP
As a cashier, you must process a sum of money between £1 and £1000 and give back the lowest number of notes and coins available for the amount specified.
Rules
The availables notes are: £50, £20, £10, £5
The available coins are: £2, £1
Output
The method produceChange() will take an int as a parameter, and will return a string describing the given change.
Ex. 1)
produceChange(40)
Should return:
For £40 - change was 2 notes and 0 coins: 2x £20 note
Ex. 2)
produceChange(430)
Should return:
For £430 - change was 10 notes and 3 coins: 8 x £50 note, 1x £20 note, 1X £10 note
Numbers are always formatted correctly so checks for correct input are not required. Assume that the range is inclusively between 1 and 1000.
String output must also be sensibly readable for edge-case scenarios, e.g. "1 coin" is returned instead of "1 coins."
public class Cashier {
private int fiftyNotes = 0;
private int twentyNotes = 0;
private int tenNotes = 0;
private int fiveNotes = 0;
private int cash = 0;
private int twoPoundsCoins = 0;
private int onePoundCoins = 0;
private int totalNotes = 0;
private int totalCoins = 0;
public Cashier() {
}
public String produceChange(int cash) {
this.cash = cash;
while ((cash) > 49) {
cash -= 50;
fiftyNotes++;
totalNotes++;
}
while ((cash) > 19) {
cash -= 20;
twentyNotes++;
totalNotes++;
}
while ((cash) > 9) {
cash -= 10;
tenNotes++;
totalNotes++;
}
while ((cash) > 4) {
cash -= 5;
fiveNotes++;
totalNotes++;
}
while ((cash) > 1) {
cash -= 2;
twoPoundsCoins++;
totalCoins++;
}
onePoundCoins = cash;
totalCoins += onePoundCoins;
return this.toString();
}
private void clear() {
fiftyNotes = 0;
twentyNotes = 0;
tenNotes = 0;
fiveNotes = 0;
twoPoundsCoins = 0;
onePoundCoins = 0;
fiftyPence = 0;
twentyPence = 0;
tenPence = 0;
fivePence = 0;
twoPence = 0;
onePence = 0;
totalNotes = 0;
totalCoins = 0;
}
public String toString() {
StringBuilder sb = new StringBuilder("");
if (fiftyNotes > 0) {
sb.append(fiftyNotes + "x £50 note");
}
if (twentyNotes > 0) {
sb.append((sb.length() > 0 ? ", " : "") + twentyNotes + "x £20 note");
}
if (tenNotes > 0) {
sb.append((sb.length() > 0 ? ", " : "") + tenNotes + "x £10 note");
}
if (fiveNotes > 0) {
sb.append((sb.length() > 0 ? ", " : "") + fiveNotes + "x £5 note");
}
if (twoPoundsCoins > 0) {
sb.append((sb.length() > 0 ? ", " : "") + twoPoundsCoins + "x £2 coin");
}
if (onePoundCoins > 0) {
sb.append((sb.length() > 0 ? ", " : "") + onePoundCoins + "x £1 coin");
}
String out = "For £" + cash + " - change was " + totalNotes + (totalNotes == 1 ? " note and " : " notes and ")
+ totalCoins + (totalCoins == 1 ? " coin: " : " coins: ");
clear();
return out + sb.toString();
}
}
import org.junit.Test;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class CashierTest {
private Cashier cashier = new Cashier();
@Test
public void testFixed() {
assertEquals("For £100 - change was 2 notes and 0 coins: 2x £50 note", cashier.produceChange(100));
assertEquals("For £1 - change was 0 notes and 1 coin: 1x £1 coin", cashier.produceChange(1));
}
private Solution solution = new Solution();
@Test
public void randomTests() {
for (int i = 0; i <= 500; i++) {
Random randInt = new Random();
int randomNum = randInt.nextInt(1000) + 1;
assertEquals(solution.produceChange(randomNum), cashier.produceChange(randomNum));
}
}
}
Find positive and negative integer pair in the array.
There is only one pair in the array.
Think about the performance.
[1,2,3,-2,4,5,6] -> [-2, 2]
[1,5,7,8,-2,3,-7] -> [-7, 7]
function pairs(arr){
let sorted = arr.sort();
let length = arr.length;
for (let i = 0; i < length; i++) {
if (sorted.includes(arr[i] * -1)) {
return [sorted[i], sorted[i] * -1];
}
}
}
describe("Solution", function() {
it("should return right pair", function() {
Test.assertSimilar(pairs([1, 3, 6, -2, 4, -6]), [-6,6])
Test.assertSimilar(pairs([3, 6, 8, -2, 4, 5, -1, -8]), [-8, 8])
});
});
\ First Forth Kumite
: hw ." Hello World!" ;
hw cr
calculate the variance of the given vector
not sure how to get the unit tests working. or make it less obvious to the user
c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94)
test_that("Variance", {
expect_equal(round(var(c(52,73,55,26,72,45,80,62,NA,7,NA,87,54,46,85,37,94),na.rm=T),4),round(587.5238,4))
})
This is a string to binary and vice versa converter in python. The idea is that when the user enters a string into the function, a binary number for each character is returned.
e.g. hi --> 01101000 01101001
and 01101000 01101001 --> hi
Possible further applications could include something like containing code in a string and then executing it or just some fun encryption stuff.
def binary_converter(string):
result = ""
if string[0] != '0':
for character in string:
result += str(bin(ord(character))[2:].zfill(8))
# So characters can be told apart
result += " " if character != string[len(string) - 1] else ""
else:
# This does the opposite, converting
# the output of the above into the original input.
store = [] # setting up list
# Sorting numbers into individual items in the list
for character in string:
if character != ' ':
result += character
else:
store.append(result)
result = ""
store.append(result)
result = ""
# getting results, aka the orginal string
for item in store:
result += chr(int(item, 2))
print(result) # for debugging
return result
test.assert_equals(binary_converter("hi"), '01101000 01101001' )
test.assert_equals(binary_converter("pI"), '01110000 01001001')
test.assert_equals(binary_converter("!]5832105321[ ]!@#$%^&*()"), '00100001 01011101 00110101 00111000 00110011 00110010 00110001 00110000 00110101 00110011 00110010 00110001 01011011 00100000 01011101 00100001 01000000 00100011 00100100 00100101 01011110 00100110 00101010 00101000 00101001')
test.assert_equals(binary_converter("Hi, this is James Anderson. How are you?"), '01001000 01101001 00101100 00100000 01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01001010 01100001 01101101 01100101 01110011 00100000 01000001 01101110 01100100 01100101 01110010 01110011 01101111 01101110 00101110 00100000 01001000 01101111 01110111 00100000 01100001 01110010 01100101 00100000 01111001 01101111 01110101 00111111')
test.assert_equals(binary_converter("01101000 01101001"), 'hi')
test.assert_equals(binary_converter("01110000 01001001"), 'pI')
test.assert_equals(binary_converter("00100001 01011101 00110101 00111000 00110011 00110010 00110001 00110000 00110101 00110011 00110010 00110001 01011011 00100000 01011101 00100001 01000000 00100011 00100100 00100101 01011110 00100110 00101010 00101000 00101001"), '!]5832105321[ ]!@#$%^&*()')
test.assert_equals(binary_converter("01001000 01101001 00101100 00100000 01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01001010 01100001 01101101 01100101 01110011 00100000 01000001 01101110 01100100 01100101 01110010 01110011 01101111 01101110 00101110 00100000 01001000 01101111 01110111 00100000 01100001 01110010 01100101 00100000 01111001 01101111 01110101 00111111"), 'Hi, this is James Anderson. How are you?')
To celebrate the 50 year anniversary of the moon landing I've written some code to output my profile picture to the console!
Can you output your profile picture to the console?
function profilePicture() {
distance = (x1, y1, x2, y2) => Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
random = t => t[~~(Math.random() * t.length)];
var width = 56, height = 32;
var ratio = height / width;
var grid = [...Array(height)].map(a => []);
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {
var isIncluded =
distance(width * 0.5 * ratio, height * 0.50, (x+.5) * ratio, y + .5) < height * 0.47 &&
distance(width * 0.6 * ratio, height * 0.45, (x+.5) * ratio, y + .5) >= height * 0.41;
grid[y].push(isIncluded ? random("£@$%€0&#¥") : ' ');
}
}
return grid.map(a => a.join``).join`\n`;
}
console.log(profilePicture());
describe("Solution", function() {
it("", function() {
Test.assertEquals(true, true);
});
});