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.
Some thread of random rusty stuffsz
// Code is in the preload
#[test]
fn can_greet() {
let p = Person { first_name: "Bill", last_name: "Smith" };
assert_eq!(p.greet(), "Hello, my name is Bill Smith");
}
Write a program to find if given array contains duplicate elements or all elements are unique.
Your function should output true
if there are duplicates, false
if all elements are unique.
Example:
Input:
1 4 3 2 6 4
Output:
true
Input:
1 4 3 2 6 5
Output:
false
import java.util.*;
class Solution {
/**
* Checks if given array contains duplicate elements.
* Complexity: O(N)
* @author Jayesh Chandrapal
* @param nums integer array of elements
* @return true if duplicate elements are found, false otherwise
*/
public static boolean hasDuplicates(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for(int i = 0, len = nums.length; i < len; i++) {
if(set.contains(nums[i])) {
return true;
} else {
set.add(nums[i]);
}
}
return false;
}
}
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 test1() {
assertEquals( true, Solution.hasDuplicates(new int[]{1, 2, 3, 4, 4}) );
}
@Test
public void test2() {
assertEquals( false, Solution.hasDuplicates(new int[]{1, 2, 3, 4, 5}) );
}
@Test
public void test3() {
assertEquals( false, Solution.hasDuplicates(new int[]{1}) );
}
@Test
public void test4() {
assertEquals( false, Solution.hasDuplicates(new int[]{-1, 5, -3, 3}) );
}
@Test
public void test5() {
assertEquals( false, Solution.hasDuplicates(new int[]{}) );
}
@Test
public void test6() {
assertEquals( true, Solution.hasDuplicates(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 1}) );
}
}
Given array of integer elements, find the index of peak element in O(Log N)
time complexity.
A peak element is an element that is greater than its neighbors. Given an input array where num[i] != num[i + 1]
, find a peak element and return its index. Array may contain multiple peaks, in that case return index of any peak.
Example:
Input:
1 2 3 1
Output:
2
because peak element is 3
and it is at index 2
.
import java.util.*;;
class Solution {
public static int peakIndex(int[] nums) {
int left = 0;
int right = nums.length - 1;
int peak = -1;
while(left <= right) {
if(left == right) {
peak = left;
break;
}
int mid = (left + right) / 2;
if(nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
}
return peak;
}
}
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 test1() {
assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1}));
}
@Test
public void test2() {
assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1}));
}
@Test
public void test3() {
assertEquals(0, Solution.peakIndex(new int[] {2}));
}
@Test
public void test4() {
assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2}));
}
@Test
public void test5() {
assertEquals(14, Solution.peakIndex(new int[] {1, 2, 3, 2, 1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7, 6, 5, 4, 3, 7, 8, 1}));
}
}
let abc = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'; 'k'; 'l'; 'm'; 'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't'; 'u'; 'v'; 'w'; 'x'; 'y'; 'z']
let rot13 s = s |> String.map(fun c -> match abc |> List.tryFindIndex(fun x -> x = c) with | Some(i) -> abc.[(i + 13) % 26] | None -> c)
printfn "%s" (rot13 "hello world")
printfn "%s" (rot13 "uryyb jbeyq")
let countChars (s : string) =
s.ToCharArray()
|> List.ofArray
|> List.groupBy(fun x -> x)
|> List.map(fun (c, l) -> (c, l.Length))
|> Map.ofSeq
printfn "%A" ("abacdacb" |> countChars)
A basic example of how to setup an active record challenge.
Note: Make sure to check out the preloaded section for how to configure the database.
ActiveRecord::Schema.define do
create_table :albums do |table|
table.column :title, :string
table.column :performer, :string
end
create_table :tracks do |table|
table.column :album_id, :integer
table.column :track_number, :integer
table.column :title, :string
end
end
class Album < ActiveRecord::Base
has_many :tracks
end
class Track < ActiveRecord::Base
belongs_to :album
end
# 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)
describe "Solution" do
album = Album.create(title: "The Downward Spiral", performer: "Nine Inch Nails")
describe "Album" do
it "should have a title" do
Test.assert_equals(album.title, "The Downward Spiral")
end
it "should have a performer" do
Test.assert_equals(album.performer, "Nine Inch Nails")
end
end
end
A basic proof of how you can create content that writes to the file system.
# you can write to your codewarrior home folder
`echo "example text" > /home/codewarrior/example.txt`
describe "Solution" do
it "should by able to ready the example file" do
File.open("/home/codewarrior/example.txt", "r") do |f|
f.each_line do |line|
puts line
Test.assert_equals(line, "example text\n")
end
end
end
end
Often times kata authors want to prevent certain code from being used within a kata, to increase the level of difficulty. This is an example of how you can read the solution.txt file into your code.
This example is in JavaScript, but the /home/codewarrior/solution.txt
file is available for all languages.
// write a add function that doesn't use the plus symbol
function add(a, b){
return a + b;
}
// Make sure to view the test cases to see how this test fails
const fs = require('fs');
const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');
describe("Check Solution", function(){
it("should prevent the '+' symbol from being used anywhere in the code", function(){
Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");
});
});
This is a basic example of rendering a plot chart to the output window. It uses matplotlib and mpld3.
Some dummy tests were added at the end to demonstrate how you could have a challenge based on rendering data to a chart and then you could have tests that test the chart data after the fact - allowing you to have an interactive visualazation for the data that is being tested.
# Example taken from http://mpld3.github.io/examples/drag_points.html
import numpy as np
import matplotlib
matplotlib.use('Agg') # need to do this to set the display type
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots()
np.random.seed(0)
points = ax.plot(np.random.normal(size=20),
np.random.normal(size=20), 'or', alpha=0.5,
markersize=50, markeredgewidth=1)
ax.set_title("Click and Drag", fontsize=18)
# See Preloaded code for JS DragPlugin
plugins.connect(fig, DragPlugin(points[0]))
print(mpld3.fig_to_html(fig))
Test.describe("You can add tests after the plot is rendered")
Test.it("should do something that passes or fails")
test.expect(True)
Test.it("should do something else that passes or fails")
test.expect(True)
This is a very basic example of how you can start a redis server using async code and wrapping it so that you can enable the solution to run only after the server has started.
This example also demonstrates how you can support async code within it
tests. To enable async mode, you can pass true
or a number value as the 2nd argument to describe
. If true
is provided, 2000
is used as the default number value, which is used as the timeout for each it
run.
Notice how the it
function has a done
callback. You are required to call this when using async code so that the function knows when the it
test has finished and can move on to the next test.
const redis = require("redis");
const Promise = require("bluebird");
// promisfy redis client to make it much easier to work with.
Promise.promisifyAll(redis.RedisClient.prototype);
// solution is your entry point. The redis server will not be available until solution is called. Since
// we are using async code you must call done() once you have completed the requirements.
function solution(done){
let client = redis.createClient();
// use Promise.join to make sure the done callback is fired after all other async operations have fired.
Promise.join(
client.setAsync("foo", "bar"),
client.hsetAsync("h", "key", "value"),
done
)
}
startRedis().then(stop => {
let client = redis.createClient();
solution(_ => {
describe("redis challenge", true, _ => {
it ("should have set foo to == 'bar'", done => {
client.getAsync('foo').then(actual => {
Test.assertEquals(actual, "bar");
done();
});
});
it ("should set h.key to == 'value'", done => {
client.hgetAsync('h', 'key').then(actual => {
Test.assertEquals(actual, "value");
done();
});
});
})
.then(stop); // describe returns a promise so we can hook into that to shut down our spawned process.
});
});