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.
There is a FizzBuzzy Race comming up. There will be a cone dropped at every mile marker divisable by 3 and 5, start at the start line.
When divisable by 3 and 5 there will be two cones dropped.
The start line is the 0th point and needs cones also.
The command will be: 'fizzbuzzy' and take the miles as the sole parameter.
function fizzbuzzy(n) {
//insert code here
}
Test.assertEquals(fizzbuzzy(21), 13, "21 miles needs 13 cones")
Test.assertEquals(fizzbuzzy(99), 54, "99 miles needs 54 cones")
Using HashMap in Java.
/* HashMap Example */
import java.util.*;
import java.lang.*;
import java.io.*;
class HashMapDemo
{
public static void main (String[] args) throws java.lang.Exception
{
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.put(2, 1);
map.put(3, 1);
map.put(4, 1);
map.put(5, 1);
map.put(6, 1);
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
System.out.println(key + " " + value);
}
}
}
It should return result of lisp-like arithmetic expressions:
run("(+ 3 (* 2 4) 1)") # 12
def run(code):
def func(operator):
from functools import reduce
add = lambda a, b: float(a) + float(b)
mul = lambda a, b: float(a) * float(b)
div = lambda a, b: float(a) / float(b)
deduct = lambda a, b: float(a) - float(b)
d = {
'+': lambda arr: reduce(add, arr),
'*': lambda arr: reduce(mul, arr),
'/': lambda arr: reduce(div, arr),
'-': lambda arr: reduce(deduct, arr)
}
return d[operator]
def lex(token):
if token in ('+', '-', '/', '*', '%'):
return "operator"
elif token == '(':
return "lbracket"
elif token == ')':
return "rbracket"
elif token[0].isalpha():
return "name"
elif token[0] == token[-1] and token[0] in ('"', "'"):
return "string"
else:
try:
float(token)
return "number"
except:
raise ValueError
def getArgs(words):
args = []
arg = []
i = 0
for word in words[2:]:
if word == '(':
i += 1
arg.append(word)
elif word == ')':
i -= 1
arg.append(word)
if i == 0:
args.append(arg)
arg = []
elif i == 0:
arg.append(word)
args.append(arg)
arg = []
else:
arg.append(word)
return args
def expr(words):
args = getArgs(words)
args_ = []
for arg in args:
if len(arg) == 1:
args_.append(arg)
else:
args_.append(expr(arg))
if lex(words[1]) == "operator":
return func(words[1])(list(map(lambda a: (type(a) in (list, tuple) and a[0]) or a, args_)))
lines = code.split("\n")
for line in lines:
word = ''
words = []
chars = tuple(line)
for i in tuple(line):
if i in ('(', ')'):
if word: words.append((word, lex(word)))
words.append((i, lex(i)))
word = ''
elif i == ' ':
if word: words.append((word, lex(word)))
word = ''
else:
word += i
if word: words.append((word, lex(word)))
words_ = list(map(lambda arr: arr[0], words))
return(expr(words_))
Test.assert_equals(run("(+ 1 2)"), 3.0)
Test.assert_equals(run("(* 3 (/ 10 2))"), 15.0)
For loops are commonly overused when other Javascript methods can lead to a cleaner and faster solution. A common example would be finding duplicates in an array.
This function called duplicates takes an array of numbers and returns a new array with the numbers duplicated in the original array ordered by value.
Your goal is to refactor this code to still find duplicates in an array and return those duplicates in a new array, but no longer use a for loop.
Note: numbers and their corresponding string representations should not be treated as duplicates (i.e., '1' !== 1).
Based on http://www.codewars.com/kata/find-duplicates/javascript
function duplicates(arr) {
var out = [];
for(var x=0;x<arr.length-1;x++)
{
var ch = arr[x];
for(var y=x+1;y<arr.length;y++)
{
var comp = arr[y];
if (comp === ch && out.indexOf(comp) === -1)
{
out.push(comp);
break;
}
}
}
out.sort();
return out;
}
Test.assertEquals(duplicates([1, 2, 4, 4, 3, 3, 1, 5, 3]).toString, [1,3,4].toString, "Sorry, your array does not have the correct contents");
Test.assertEquals(duplicates([0, 1, 2, 3, 4, 5]).toString, [].toString, "Sorry, your array does not have the correct contents");
Test.assertEquals(duplicates.toString().indexOf("for"), -1, "You can't use for loops");
Test.assertEquals(duplicates.toString().indexOf("forEach"), -1, "You can't use for loops");
Test.assertNotEquals(duplicates.toString().indexOf("reduce"), -1, "You should be using reduce in your solution");
#FOR THE SLC JS LEARNERS 2015 STUDY GROUP
Complete the following merge function such that is behaves as described in the example below. Merge should take two arrays of numbers of identical length and a callback function.
var merge = function(array1, array2, callback){
//your code here.
}
var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){
return a + b;
});
//x should now equal [6, 8, 10, 12].
Now, use your merge function to complete the euclid function defined below. Euclid should take two arrays, each of which has an equal number of numerical elements, and return the euclidean distance between them. For a quick refresher on what Euclidean distance is, check here
var euclid = function(coords1, coords2){
//Your code here.
//You should not use any loops and should
//instead use your original merge function.
}
var y = euclid([1.2, 3.67], [2.0, 4.4]);
//y should now equal approximately 1.08.
var merge = function(array1, array2, callback){
var array3 = [];
if (array1.length !== array2.length) {
console.log("Array length mismatch");
return new Error("Array length mismatch");
} else {
length = array1.length;
}
for (var i = 0; i < length; i++) {
array3[i] = callback(array1[i], array2[i]);
}
return array3;
}
var x = merge([1, 2, 3, 4], [5, 6, 7, 8], function(a, b){
return a + b;
});
//x should now equal [6, 8, 10, 12].
var euclid = function(coords1, coords2){
//Your code here.
//You should not use any loops and should
//instead use your original merge function.
}
var y = euclid([1.2, 3.67], [2.0, 4.4]);
//y should now equal approximately 1.08.
Example:
Find first most frequently occuring number in given array.
Given input {1, 2, 9, 3, 4, 3, 3, 1, 2, 4, 5, 3, 8, 3, 9, 0, 3, 2}
,
output should be 3
as it has highest frequency of occurence.
import java.util.*;
/**
Time Complexity : O(N)
Space Complexity : O(N)
*/
class MaxOccurence {
public static int findMax(int[] nums) {
return findMaxOccurenceCountArray(nums);
}
private static int findMaxOccurenceCountArray(int[] nums) {
int maxNum = 0;
int maxCount = 0;
if(nums.length < 1) return -1;
if(nums.length == 1) return nums[0];
int[] counts = new int[nums.length];
for(int i = 0, len = nums.length; i < len; i++) {
counts[nums[i]]++;
if(counts[nums[i]] > maxCount) {
maxCount = counts[nums[i]];
maxNum = nums[i];
}
}
return maxNum;
}
}
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class MaxOccurenceTest {
@Test
public void test1() {
int[] nums = {1, 2, 9, 3, 4, 3, 3, 1, 2, 4, 5, 3, 8, 3, 9, 0, 3, 2};
assertEquals(3, MaxOccurence.findMax(nums));
}
@Test
public void test2() {
int[] nums = {5, 4, 6 ,7, 9, 8, 2, 4, 3, 6, 5, 8, 6, 1, 2, 5, 3, 4, 7, 9, 0, 8, 5, 7, 3, 4, 6, 1, 3};
assertEquals(5, MaxOccurence.findMax(nums));
}
@Test
public void test3() {
int[] nums = {8};
assertEquals(8, MaxOccurence.findMax(nums));
}
@Test
public void test4() {
int[] nums = {};
assertEquals(-1, MaxOccurence.findMax(nums));
}
@Test
public void test5() {
int[] nums = {2, 2, 3, 3, 4, 4, 5, 5, 6, 6};
assertEquals(2, MaxOccurence.findMax(nums));
}
@Test
public void test6() {
int[] nums = {2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5};
assertEquals(5, MaxOccurence.findMax(nums));
}
}
A couple who work on a farm have asked you to create a program for them that will allow to them store the types of animals they have on the farm, these include:
-Cows
-Chickens
-Pigs
Using interfaces we can make the process of creating the classes for each animal easier. By using an animal interface we can group the animals together in the code.
interface AnimalsInterface
{
public function getLegs();
public function setLegs($noOfLegs);
public function getDiet();
public function setDiet($foodTheyEat);
}
class Cow implements AnimalsInterface
{
private $legs;
private $diet;
public function getLegs()
{
return $this->legs;
}
public function setLegs($noOfLegs)
{
$this->legs = $noOfLegs;
return $this;
}
public function getDiet()
{
return $this->diet;
}
public function setDiet($foodTheyEat)
{
$this->diet = $foodTheyEat;
return $this;
}
}
class Chicken implements AnimalsInterface
{
private $legs;
private $diet;
public function getLegs()
{
return $this->legs;
}
public function setLegs($noOfLegs)
{
$this->legs = $noOfLegs;
return $this;
}
public function getDiet()
{
return $this->diet;
}
public function setDiet($foodTheyEat)
{
$this->diet = $foodTheyEat;
return $this;
}
}
class Pig implements AnimalsInterface
{
private $legs;
private $diet;
public function getLegs()
{
return $this->legs;
}
public function setLegs($noOfLegs)
{
$this->legs = $noOfLegs;
return $this;
}
public function getDiet()
{
return $this->diet;
}
public function setDiet($foodTheyEat)
{
$this->diet = $foodTheyEat;
return $this;
}
}
class Farm
{
private $animals = [];
public function addAnimal(AnimalsInterface $animal)
{
array_push($this->animals, $animal)
}
public function displayAnimals()
{
foreach($this->animals as $animal) {
echo $animal->getLegs();
echo $animal->getDiet();
}
}
}
$cow = new Cow();
$cow->setLegs(4)->setDiet('Grass');
$chicken = new Chicken();
$chicken->setLegs(2)->setDiet('Seeds');
$pig = new Pig();
$pig->setLegs(4)->setDiet('Corn');
$farm = new Farm;
$farm->addAnimal($cow);
$farm->addAnimal($chicken);
$farm->addAnimal($pig);
$farm->displayAnimal();
Implementation of Selection Sort.
- Time Complexity:
O(n^2)
- Space Complexity:
O(1)
- Useful when list size is small
- Preferable to insertion sort in terms of number of writes (
O(n)
swaps versusO(n2)
swaps), it almost always far exceeds (and never beats) the number of writes that cycle sort makes, as cycle sort is theoretically optimal in the number of writes. This can be important if writes are significantly more expensive than reads, such as with EEPROM or Flash memory, where every write lessens the lifespan of the memory. - Wiki Link: Selection Sort
class Sort:
def selection_sort(self, nums):
if nums == None: return None
for i in range(0, len(nums) - 1):
minIndex = i
for j in range(i + 1, len(nums)):
if nums[j] < nums[minIndex]:
minIndex = j
nums[i], nums[minIndex] = nums[minIndex], nums[i]
return nums
# Test sorting
sortApp = Sort()
test.describe("Testing normal sort ..")
nums = [6,3,4,5,1,2,9,8,7]
expected = [1,2,3,4,5,6,7,8,9]
Test.assert_equals(sortApp.selection_sort(nums), expected, "Failed normal sorting")
# Test sorting long list
test.describe("Testing long list ..")
nums = [1,5,4,6,5,7,6,8,7,9,8,0,2,3,4,5,6,7,2,5,3,4,1,6,5,8,7,9,8,0,2,4,3,5,4,6,5,7,7,6,8,9,5,7,6,3,4,2,3,1,6,5,8,6,7,9,8,0,5,6,4,5,3,4,2,6,4,7,5,8,6,9,7,0,1,4,3,5,4,6,5,8,7,9,3,7,5,6,1,9,0,4,7,6,1,8,3,6,0,9,4,7,6,8,1,4,3,5,4,6,5,7,6,8,7,9,8,0,2,5,4,7,5,8,6,9,7,0,8,9,7,8,6,7,5,6,4,5,3,4,3,4,1,3,2,4,3,5,4,6,5,7,6,8,7,8,1,3,2,4,3,5,4,6,5,7,6,8,6,8,6,7,56,7,5,6,4,5,34,5,4,6,5,7,6,6,8,6,8]
expected = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 34, 56]
Test.assert_equals(sortApp.selection_sort(nums), expected, "Failed sorting long list")
# Test None as input
test.describe("Testing with None as input ..")
Test.assert_equals(sortApp.selection_sort(None), None, "Failed None as input")
# Test all elements same
test.describe("Testing same elements in list ..")
nums = [4,4,4,4,4]
expected = [4,4,4,4,4]
Test.assert_equals(sortApp.selection_sort(nums), expected, "Failed same elements")
# Test negatives
test.describe("Testing negative numbers ..")
nums = [-1,-2,-6,-3,-4,-5,-9,-8,-7]
expected = [-9,-8,-7,-6,-5,-4,-3,-2,-1]
Test.assert_equals(sortApp.selection_sort(nums), expected, "Failed negative numbers")
Implementation of Insertion Sort.
-
Time Complexity: Worst case:
O(n^2)
, Best case:O(n)
-
Space Complexity:
O(1)
-
Useful when list size is small.
-
only scans as many elements as needed to determine the correct location.
-
More efficient than bubble or selection sort.
-
Efficient for data sets that are already substantially sorted.
-
Requires more writes because the inner loop can require shifting large sections of the sorted portion of the array.
-
Can sort a list as it receives it.
-
Wiki Link: Insertion Sort
class Sort:
def insertion_sort(self, nums):
if nums is None:
return None
for i in range(1, len(nums)):
currentvalue = nums[i]
position = i
while position > 0 and nums[position - 1] > currentvalue:
nums[position] = nums[position - 1]
position -= 1
nums[position] = currentvalue
return nums
# Test sorting
sortApp = Sort()
test.describe("Testing normal sort ..")
nums = [6,3,4,5,1,2,9,8,7]
expected = [1,2,3,4,5,6,7,8,9]
Test.assert_equals(sortApp.insertion_sort(nums), expected, "Failed normal sorting")
# Test sorting long list
test.describe("Testing long list ..")
nums = [1,5,4,6,5,7,6,8,7,9,8,0,2,3,4,5,6,7,2,5,3,4,1,6,5,8,7,9,8,0,2,4,3,5,4,6,5,7,7,6,8,9,5,7,6,3,4,2,3,1,6,5,8,6,7,9,8,0,5,6,4,5,3,4,2,6,4,7,5,8,6,9,7,0,1,4,3,5,4,6,5,8,7,9,3,7,5,6,1,9,0,4,7,6,1,8,3,6,0,9,4,7,6,8,1,4,3,5,4,6,5,7,6,8,7,9,8,0,2,5,4,7,5,8,6,9,7,0,8,9,7,8,6,7,5,6,4,5,3,4,3,4,1,3,2,4,3,5,4,6,5,7,6,8,7,8,1,3,2,4,3,5,4,6,5,7,6,8,6,8,6,7,56,7,5,6,4,5,34,5,4,6,5,7,6,6,8,6,8]
expected = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 34, 56]
Test.assert_equals(sortApp.insertion_sort(nums), expected, "Failed sorting long list")
# Test None as input
test.describe("Testing with None as input ..")
Test.assert_equals(sortApp.insertion_sort(None), None, "Failed None as input")
# Test all elements same
test.describe("Testing same elements in list ..")
nums = [4,4,4,4,4]
expected = [4,4,4,4,4]
Test.assert_equals(sortApp.insertion_sort(nums), expected, "Failed same elements")
# Test negatives
test.describe("Testing negative numbers ..")
nums = [-1,-2,-6,-3,-4,-5,-9,-8,-7]
expected = [-9,-8,-7,-6,-5,-4,-3,-2,-1]
Test.assert_equals(sortApp.insertion_sort(nums), expected, "Failed negative numbers")
Implementation of Merge Sort.
-
Time Complexity:
O(n log(n))
-
Space Complexity:
O(n)
-
Preserves the input order of equal elements in the sorted output.
-
Type of Divide-and-Conquer algorithm.
-
Efficient at handling slow-to-access sequential media.
-
Well-suited for sorting huge amounts of data that does not fit into memory.
-
Wiki Link: Merge Sort
class Sort:
def merge_sort(self, nums):
if nums == None: return None
if len(nums) > 1:
mid = len(nums) // 2
lefthalf = nums[:mid]
righthalf = nums[mid:]
self.merge_sort(lefthalf)
self.merge_sort(righthalf)
i = 0
j = 0
k = 0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
nums[k] = lefthalf[i]
i += 1
else:
nums[k] = righthalf[j]
j += 1
k += 1
while i < len(lefthalf):
nums[k] = lefthalf[i]
i += 1
k += 1
while j < len(righthalf):
nums[k] = righthalf[j]
j += 1
k += 1
return nums
# Test sorting
sortApp = Sort()
test.describe("Testing normal sort ..")
nums = [6,3,4,5,1,2,9,8,7]
expected = [1,2,3,4,5,6,7,8,9]
Test.assert_equals(sortApp.merge_sort(nums), expected, "Failed normal sorting")
# Test sorting long list
test.describe("Testing long list ..")
nums = [1,5,4,6,5,7,6,8,7,9,8,0,2,3,4,5,6,7,2,5,3,4,1,6,5,8,7,9,8,0,2,4,3,5,4,6,5,7,7,6,8,9,5,7,6,3,4,2,3,1,6,5,8,6,7,9,8,0,5,6,4,5,3,4,2,6,4,7,5,8,6,9,7,0,1,4,3,5,4,6,5,8,7,9,3,7,5,6,1,9,0,4,7,6,1,8,3,6,0,9,4,7,6,8,1,4,3,5,4,6,5,7,6,8,7,9,8,0,2,5,4,7,5,8,6,9,7,0,8,9,7,8,6,7,5,6,4,5,3,4,3,4,1,3,2,4,3,5,4,6,5,7,6,8,7,8,1,3,2,4,3,5,4,6,5,7,6,8,6,8,6,7,56,7,5,6,4,5,34,5,4,6,5,7,6,6,8,6,8]
expected = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 34, 56]
Test.assert_equals(sortApp.merge_sort(nums), expected, "Failed sorting long list")
# Test None as input
test.describe("Testing with None as input ..")
Test.assert_equals(sortApp.merge_sort(None), None, "Failed None as input")
# Test all elements same
test.describe("Testing same elements in list ..")
nums = [4,4,4,4,4]
expected = [4,4,4,4,4]
Test.assert_equals(sortApp.merge_sort(nums), expected, "Failed same elements")
# Test negatives
test.describe("Testing negative numbers ..")
nums = [-1,-2,-6,-3,-4,-5,-9,-8,-7]
expected = [-9,-8,-7,-6,-5,-4,-3,-2,-1]
Test.assert_equals(sortApp.merge_sort(nums), expected, "Failed negative numbers")