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.
Jake's girlfriend just told him she wants to go gluten-free. And she wants him to build a Javascript function to help them detect gluten in their favorite foods. I know, the request is a little weird, but why not help him?
The code should take a list of ingredients and detect whether or not it contains the following:
"wheat",
"wheat flour",
"triticale",
"barley",
"rye",
"brewer's yeast",
"malt",
"wheatberries",
"durum",
"emmer",
"semolina",
"spelt",
"farina",
"farro",
"graham",
"kamut",
"einkorn"
function glutenDetector(ingredients){
var gluten = [
"wheat",
"wheat flour",
"triticale",
"barley",
"rye",
"brewer's yeast",
"malt",
"wheatberries",
"durum",
"emmer",
"semolina",
"spelt",
"farina",
"farro",
"graham",
"kamut",
"einkorn"
];
var glutenDetect = false;
var ingredientsList = ingredients.split(',');
ingredientsList.forEach(function(ingredient){
var ingredientClean = ingredient.trim().toLowerCase();
gluten.forEach(function(glutenIngredient){
if(ingredientClean.indexOf(glutenIngredient) !== -1){
glutenDetect = true;
}
});
});
return glutenDetect;
}
var ingredientsCrackers = "ENRICHED FLOUR (WHEAT FLOUR, NIACIN, REDUCED IRON, THIAMIN MONONITRATE [VITAMIN B1], RIBOFLAVIN [VITAMIN B2], FOLIC ACID), SOYBEAN AND PALM OIL WITH TBHQ FOR FRESHNESS, WHOLE WHEAT FLOUR, SKIM MILK CHEESE (SKIM MILK, WHEY PROTEIN, CHEESE CULTURES, SALT, ENZYMES, ANNATTO EXTRACT FOR COLOR), CONTAINS TWO PERCENT OR LESS OF SALT, PAPRIKA, YEAST, PAPRIKA OLEORESIN FOR COLOR, SOY LECITHIN. CONTAINS WHEAT, MILK AND SOY INGREDIENTS";
var ingredientsCookies = "RICE FLOUR, EGGS, SUGAR, BUTTER, XANTHAN GUM";
describe("Solution", function(){
it("Test these crackers for gluten", function(){
Test.assertEquals(glutenDetector(ingredientsCrackers), true, "Uh oh these crackers totally have gluten!");
});
it("Test these cookies for gluten", function(){
Test.assertEquals(glutenDetector(ingredientsCookies), false, "Actually these cookies do not have gluten");
});
});
The .unique() method for lists is super slow in Groovy, make it as fast as possible
class Uniquer {
static List unique(List x) {
x.groupBy { it }.keySet().collect()
}
}
import org.junit.Test
import static java.lang.System.currentTimeMillis as now
class UniqueTest {
@Test
void "Check if unique exceeds 2 seconds"() {
List<User> users = []
Random rand = new Random()
10000.times { byte[] a = new byte[2]; rand.nextBytes(a); users.add(new User(id: a)) }
println "x"
long a = now()
assert Uniquer.unique(users) instanceof List
assert now() - a < 2000
}
}
class User {
byte[] id
boolean equals(User user) {
user.id == id
}
int hashCode() {
((int) id[0]) << 8 | id[1]
}
}
We are adding a new bus provider to our system. In order to implement a very specific requirement of this bus provider our system needs to be able to filter direct connections. We have access to a weekly updated list of bus routes in form of a bus route data file. As this provider has a lot of long bus routes, we need to come up with a proper service to quickly answer if two given stations are connected by a bus route.
public class HelloBus {
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void testSomething() {
// assertEquals("expected", "actual");
}
}
(defun say-hello ()
(tagbody
(setq i 5)
start
(format t "[+] Hello there!~%")
(decf i)
(go end)
end
(and (zerop i) (return-from say-hello "this end."))
(go start)
))
// const request = require('request');
// function getUserAllies(username){
// return new Promise(function(resolve, reject){
// request('https://www.codewars.com/users/'+username, function(err, res, body){
// let html = body;
// let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
// resolve(+allies);
// });
// });
// }
// using request-promise to avoid wrapping a Promise
const rp = require('request-promise');
function getUserAllies(username){
return rp('https://www.codewars.com/users/'+username).then(function(body){
let html = body;
let allies = (html.match(/<div class=\"stat\"><b>Allies:<\/b>(\d+)<\/div>/) || [,'null match'])[1];
return(+allies);
});
}
// might fail because allies can change.
let users = [['myjinxin2015', 19], ['g964', 617], ['Voile', 206], ['SteffenVogel_79', 46], ['smile67', 20]];
let arr = users.map(user=>[getUserAllies(user[0]), user[1], user[0]]);
describe('getUserAllies', function(){
for (let i = 0; i < arr.length; i++){
let [userAns, allies, user] = arr[i];
userAns.then(data => Test.assertEquals(data, allies, `Incorrect numbers of allies for ${user}!`));
}
});
This extension add a shuffled() method to any sequence.
References:
import Foundation
extension Sequence {
func shuffled() -> [Iterator.Element] {
return Array(self).sorted { _,_ in drand48() < drand48() }
}
}
// [Int] -> [Int]
print([1,2,3,4,5,6].shuffled())
// CountableClosedRange<Int> -> [Int]
print((1...6).shuffled())
function hello() {
return "world";
}
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals(hello(), "world");
});
});
Note
- random() is not available in Swift 4.0
import Glibc // for random()
let a = random()
print(a)
#include <algorithm>
template< class T >
void Swap( T& a, T& b ) { std::swap( a, b ); }
Describe(Tests)
{
It(Swaps)
{
int a = 1, b = 2;
bool c = true, d = false;
double e = 3.14, f = 6.28;
Swap( a, b );
Swap( c, d );
Swap( e, f );
Assert::That( a, Equals(2));
Assert::That( b, Equals(1));
Assert::That( c, Equals(false));
Assert::That( d, Equals(true));
Assert::That( e, Equals(6.28));
Assert::That( f, Equals(3.14));
}
};
Just testing if NSInteger
literals (e.g. @42
) work on Codewars.
#import <Foundation/Foundation.h>
void testFunction() {
NSLog(@"Just testing that the program isn't crashing here\n");
NSNumber *answerToEverything = @42;
NSLog(@"The answer to everything is: %@\n", answerToEverything);
NSLog(@"Yes it works! However NSNumber must be used and not NSInteger.\n");
}
@implementation TestSuite
- (void)testDummy {
testFunction();
UKTrue(@true);
}
@end