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.
Find the longest word in a given string.
Assume all strings will contain only letters and spaces.
function longestString(str){
return str.split(' ').sort(function(a,b) {
return b.length - a.length
})[0]
}
describe("Solution", function(){
it("did you find the longest?", function(){
Test.assertEquals(longestString('I went running around the park'), "running", "try again");
Test.assertEquals(longestString('There were a lot of barking dogs everywhere'), "everywhere", "try again");
Test.assertEquals(longestString('now for some reason my shoe smells funny'), "reason", "try again");
});
});
For a given number (num) add up all numbers from 0 to num.
This is similar to a factorial, but using addition.
function numSum(num){
var sum = 0;
for (var i = 1; num >= i; i++) {
sum += i
}
return sum
};
describe("Solution", function(){
it("Did you get the right number?", function(){
Test.assertEquals(numSum(5), 15, "Better try again");
Test.assertEquals(numSum(64), 2080, "Better try again");
Test.assertEquals(numSum(1648), 1358776, "Better try again");
Test.assertEquals(numSum(0), 0, "Better try again");
Test.assertEquals(numSum(-1), 0, "Better try again");
});
});
You will be given a number of minutes. Your task is to return a string that formats the number into 'hours:minutes". Make sure you always return two intigers for the minutes section of the conversion.
ie ('0:01' instead of '0:1')
Example:
minutes(90) => '1:30'
function minutes(num){
return num % 60 < 10 ? Math.floor(num/60) + ':' + '0' + (num % 60) : Math.floor(num/60) + ':' + (num % 60)
}
describe("Solution", function(){
it("Did you make the correct conversion?", function(){
Test.assertEquals(minutes(0), "0:00", "better try again");
Test.assertEquals(minutes(1), "0:01", "better try again");
Test.assertEquals(minutes(18), "0:18", "better try again");
Test.assertEquals(minutes(13267), "221:07", "better try again");
Test.assertEquals(minutes(985), "16:25", "better try again");
Test.assertEquals(minutes(351), "5:51", "better try again");
Test.assertEquals(minutes(156113), "2601:53", "better try again");
});
});
Given an array of numbers, return the third greatest number in the array.
You will always be given an array of valid numbers with at least three values.
Ex:
thirdGreatest([4,8,1,5,3]) -> 4
function thirdGreatest(arr){
return arr.sort( (a,b) => a - b)[arr.length-3]
}
describe("Solution", function(){
it("did you return the correct number?", function(){
Test.assertEquals(thirdGreatest([4,8,1,5,3]), 4, "Better try again");
Test.assertEquals(thirdGreatest([45,38,1,0,89,6,77]), 45, "Better try again");
Test.assertEquals(thirdGreatest([878,1,1,0,46,123]), 46, "Better try again");
Test.assertEquals(thirdGreatest([8,99,0]), 0, "Better try again");
Test.assertEquals(thirdGreatest([1,1,2,3,3,4]), 3, "Better try again");
Test.assertEquals(thirdGreatest([79831,436,683,3645,2399,0,0,66]), 2399, "Better try again");
Test.assertEquals(thirdGreatest([6,33,7,41,3,388]), 33, "Better try again");
});
});
let sq = seq {
yield 1
yield! seq { 2 .. 6 }
yield 7
yield! seq [8; 9; 10]
}
let count = Seq.length sq
let list = List.ofSeq sq
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <|
fun _ -> Assert.Equal("Count of sq", 10, count)
testCase "Test 1" <|
fun _ -> Assert.Equal("List of sq", [1; 2; 3; 4; 5; 6; 7; 8; 9; 10], list)
]
end
type SafeStringNumbers() =
member this.Bind(x, f) =
match System.Int32.TryParse(x) with
| (true, v) -> f v
| _ -> f 0
member this.Return(x) =
x
let safeStringNumbers = new SafeStringNumbers()
let v1 = safeStringNumbers {
let! a = "1"
let! b = "2"
let! c = "3"
return a + b + c
}
let v2 = safeStringNumbers {
let! a = "1"
let! b = "foo"
let! c = "3"
return a + b + c
}
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <| fun _ -> Assert.Equal("1+2+3", 6, v1);
testCase "Test 2" <| fun _ -> Assert.Equal("1+foo+3", 4, v2)
]
end
get smallest positive number form array.
if no positive number in array, return 0.
example:
var list1 = [0, 1, 1, 3];
var list2 = [7, 2, null, 3, 0, 10];
var list2 = [null];
getMin(list1); //should return 1
getMin(list2); //should return 2
getMin(list3); //should return 0
var getMin = function (list){
var min = Number.MAX_VALUE;
for (var i = 0; i < list.length; i++) {
if (+list[i] <= 0) {
continue;
}
min = Math.min(min, +list[i]);
}
return min = min === Number.MAX_VALUE ? 0 : min;
}
describe("Solution", function(){
it("test1", function(){
var list = [0, 1, 1, 3];
Test.assertEquals(getMin(list), 1, "test fails");
});
it("test2", function(){
var list = [7, 2, null, 3, 0, 10];
Test.assertEquals(getMin(list), 2, "test fails");
});
it("test3", function(){
var list = [null, -2];
Test.assertEquals(getMin(list), 0, "test fails");
});
});
type Person = { Name : string; Gender : string }
let alice = { Name = "Alice"; Gender = "Female" }
let bob = { Name = "Bob"; Gender = "Male" }
let femaleOrMale p =
match p with
| { Gender = "Female" } -> p.Name + " is female."
| { Gender = "Male" } -> p.Name + " is male."
| _ -> "???"
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Test 1" <| fun _ -> Assert.Equal("Alice", "Alice is female.", femaleOrMale alice);
testCase "Test 2" <| fun _ -> Assert.Equal("Bob", "Bob is male.", femaleOrMale bob)
]
end
Explanation
The awk command takes the input from /proc/cpuinfo, matches lines containing "cpu MHz", and appends the " / 1000" to the CPU frequency, so it's ready for piping to bc
The echo scale=2 is for bc, to get floating point numbers with a precision of maximum two decimal points
Group the echo scale=2 and the awk for piping to bc, by enclosing the commands within { ...; }
Run the commands in a $(...) subshell
Wrap the subshell within (...) to store the output lines as an array
From the cpus array, you can extract the individual CPU values with:
cpu0=${cpus[0]}
cpu1=${cpus[1]}
cpu2=${cpus[2]}
cpu3=${cpus[3]}
If you don't need the values in GHz, but MHz is enough, then the command is a lot simpler:
cpus=($(awk '/cpu MHz/ {print $4}' /proc/cpuinfo))
Limitations
Arrays are Bash specific, might not work in older /bin/sh.
/proc/cpuinfo exists only in Linux.
cpus=($({ echo scale=2; awk '/cpu MHz/ {print $4 " / 1000"}' /proc/cpuinfo; } | bc))
This example shows how to do random numbers in Swift.
import Glibc
for count in 1...20 {
print(random() % 100)
}