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.
You must create a Dice generator.
Dice have 6 sides. Player throws up a cube and look at the result.
Code must have output like this:
function(random){...}
Cube(); // 1
Cube(); // 5
Cube(); // 3
...
You should use Math.floor to get a integer numbers.
function Cube(){
let random = Math.floor(Math.random()*6+1);
if(random===1)return 1
else if(random===2)return 2
else if(random===3)return 3
else if(random===4)return 4
else if(random===5)return 5
else if(random===6)return 6
}
Test.assertEquals(1,1);
Test.assertEquals(2,2);
Test.assertEquals(3,3);
Test.assertEquals(4,4);
Test.assertEquals(5,5);
Test.assertEquals(6,6);
Need to do 2d affine transformations, or rotate stuff in 3d for your toy canvas library? look no further, the solution is here! Or at least it will be if you help improve this thing by forking it.
var mat3 = (function(){
function Mat3(a){
if(!a) return mat3.identity();
this.a = [];
for(var i = 0; i < 9; i++) this.a[i] = a[i] || 0;
};
Mat3.prototype.add = function(mat){
return mat3([
this.a[0] + mat.a[0], this.a[1] + mat.a[1], this.arr[2] + mat.a[2],
this.a[3] + mat.a[3], this.a[4] + mat.a[4], this.arr[5] + mat.a[5],
this.a[6] + mat.a[6], this.a[7] + mat.a[7], this.arr[8] + mat.a[8]
]);
};
Mat3.prototype.applyToCanvasContext = function(ctx){
ctx.transform(this.a[0], this.a[3], this.a[1], this.a[4], this.a[2], this.a[5]);
};
Mat3.prototype.equals = function(mat){
return this.a[0] === mat.a[0] && this.a[1] === mat.a[1] && this.a[2] === mat.a[2]
&& this.a[3] === mat.a[3] && this.a[4] === mat.a[4] && this.a[5] === mat.a[5]
&& this.a[6] === mat.a[6] && this.a[7] === mat.a[7] && this.a[8] === mat.a[8];
};
Mat3.prototype.toString = function(){
return '[' + this.a.join(',') + ']';
};
// Matrix-Vector multiplication
Mat3.prototype.transform3d = function(v){
return [
v[0] * this.a[0] + v[1] * this.a[1] + v[2] * this.a[2],
v[0] * this.a[3] + v[1] * this.a[4] + v[2] * this.a[5],
v[0] * this.a[6] + v[1] * this.a[7] + v[2] * this.a[8]
];
};
// 2d affine transformation
Mat3.prototype.transform2d = function(v){
var v2 = this.transform3d([v[0],v[1],1]);
return [v2[0],v2[1]];
};
Mat3.prototype.inverse = function(){
var f = this.a[0] * (this.a[4]*this.a[8] - this.a[5]*this.a[7])
+ this.a[1] * (this.a[5]*this.a[6] - this.a[3]*this.a[8])
+ this.a[2] * (this.a[3]*this.a[7] - this.a[4]*this.a[6]);
return mat3([
this.a[4]*this.a[8] - this.a[5]*this.a[7],
this.a[2]*this.a[7] - this.a[1]*this.a[8],
this.a[1]*this.a[5] - this.a[2]*this.a[4],
this.a[5]*this.a[6] - this.a[3]*this.a[8],
this.a[0]*this.a[8] - this.a[2]*this.a[6],
this.a[2]*this.a[3] - this.a[0]*this.a[5],
this.a[3]*this.a[7] - this.a[4]*this.a[6],
this.a[1]*this.a[6] - this.a[0]*this.a[7],
this.a[0]*this.a[4] - this.a[1]*this.a[3]
]).multiplyScalar(1/f);
};
Mat3.prototype.multiply = function(mat){
return mat3([
this.a[0] * mat.a[0] + this.a[1] * mat.a[3] + this.a[2] * mat.a[6],
this.a[0] * mat.a[1] + this.a[1] * mat.a[4] + this.a[2] * mat.a[7],
this.a[0] * mat.a[2] + this.a[1] * mat.a[5] + this.a[2] * mat.a[8],
this.a[3] * mat.a[0] + this.a[4] * mat.a[3] + this.a[5] * mat.a[6],
this.a[3] * mat.a[1] + this.a[4] * mat.a[4] + this.a[5] * mat.a[7],
this.a[3] * mat.a[2] + this.a[4] * mat.a[5] + this.a[5] * mat.a[8],
this.a[6] * mat.a[0] + this.a[7] * mat.a[3] + this.a[8] * mat.a[6],
this.a[6] * mat.a[1] + this.a[7] * mat.a[4] + this.a[8] * mat.a[7],
this.a[6] * mat.a[2] + this.a[7] * mat.a[5] + this.a[8] * mat.a[8]
]);
};
Mat3.prototype.multiplyScalar = function(s){
return mat3([
this.a[0] * s, this.a[1] * s, this.arr[2] * s,
this.a[3] * s, this.a[4] * s, this.arr[5] * s,
this.a[6] * s, this.a[7] * s, this.arr[8] * s
]);
};
Mat3.prototype.transpose = function(){
return mat3([
this.a[0], this.a[3], this.a[6],
this.a[1], this.a[4], this.a[7],
this.a[2], this.a[5], this.a[8]
]);
};
function mat3(a){
return new Mat3(a);
};
mat3.identity = function(){
return mat3([1,0,0,0,1,0,0,0,1]);
};
// 2d rotation
mat3.rotate = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([c,s,0,-s,c,0,0,0,1]);
};
// 3d rotations
mat3.rotate.z = mat3.rotate;
mat3.rotate.y = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([c,0,s,0,1,0,-s,0,c]);
};
mat3.rotate.x = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([1,0,0,0,c,-s,0,s,c]);
};
mat3.scale = function(x, y, z){
return mat3([x,0,0,0,y,0,0,0,z||1]);
};
mat3.translate = function(x, y){
return mat3([1,0,x,0,1,y,0,0,1]);
};
mat3.Mat3 = Mat3;
return mat3;
})();
function random() { return Math.floor(Math.random()*10 - 3); }
function random2d() { return [random(), random()]; };
function random3d() { return [random(), random(), random()]; };
function randommat() { var r = random; return mat3([r(),r(),r(),r(),r(),r(),r(),r(),r()]); }
function toString(x){
return mat3.Mat3.prototype.toString.apply({'a' : x});
};
describe("equality", function(){
it("matrices should know when two matrices are equal", function(){
var a = mat3.identity(),
b = mat3.identity();
console.log('a', a.toString());
console.log('b', b.toString());
Test.assertEquals(
a.equals(b),
true
);
Test.assertEquals(
b.equals(a),
true
);
});
it("matrices should know when two matrices aren't equal", function(){
var a = randommat(),
b = randommat(),
i = Math.floor(Math.random()*9);
a[i] = 1;
b[i] = 2;
console.log('a', a.toString());
console.log('b', b.toString());
Test.assertEquals(
a.equals(b),
false
);
});
});
describe("2d transformations", function(){
it("matrices should translate points correctly", function(){
for(var i = 0; i < 5; i++){
var p = random2d(),
o = random2d(),
mat = mat3.translate(o[0], o[1]),
t = mat.transform2d(p);
console.log(
toString(o) + ' + ' + toString(p) + ' = ' + toString([o[0]+p[0],o[1]+p[1]]) + '?'
);
Test.assertEquals(
o[0]+p[0],
t[0]
);
Test.assertEquals(
o[1]+p[1],
t[1]
);
}
});
});
describe("programmers", function(){
it("are too lazy to add more tests", function(){
Test.assertEquals(true,true);
});
});
require "sqlite3"
db = SQLite3::Database.new ":memory:"
rows = db.execute <<-SQL
create table numbers (
name varchar(30),
val int
);
SQL
{
"one" => 1,
"two" => 2,
}.each do |pair|
db.execute "insert into numbers values ( ?, ? )", pair
end
p db.execute( "select * from numbers" ).to_a
describe "DB" do
it "should have two rows" do
rows = db.execute( "select * from numbers" ).to_a
Test.assert_equals(rows.size, 2, "Should have 2 rows")
end
end
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