Begin a new Kumite
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.
let add a b = a + b let square x = x * x let a = add 1 2 |> add 3 |> add 4 |> square printfn "%i" a let add1 x = x + 1 let b = (square >> add1) 2 printfn "%i" b
1 1 let add a b = a + b
2 2 let square x = x * x
3 − let result = add 1 2 |> add 3 |> add 4 |> square
3 + let a = add 1 2 |> add 3 |> add 4 |> square
4 4 5 − printfn result
5 + printfn "%i" a
6 + 7 + 8 + let add1 x = x + 1
9 + let b = (square >> add1) 2
10 + 11 + printfn "%i" b
power function
let rec power a n = match n with | 0 -> 1 | _ -> a * power a (n - 1)
printfn "%i" (power 2 4)
printfn "%i" (power 3 3)
printfn "%i" (power 4 2)
Raise 2 to the power 10 without using Math.pow or defining your own function.
Make it in one expression.
let x = let rec pow2 n = match n with | 0 -> 1 | _ -> 2 * pow2 (n - 1) in pow2 10
printfn "%i" x
Arrays
Recent Moves:
Arrays
import java.util.*;; class Solution { public static int peakIndex(int[] nums) { if (nums.length == 1) return 0; if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1; boolean ascending = false; for (int i = 0; i < nums.length - 1; ++i) { if (nums[i] < nums[i + 1]) { ascending = true; } else { if (ascending && nums[i] > nums[i + 1]) { return i; } ascending = false; } } if (ascending) { return nums.length - 1; } else { return -1; } } }
1 1 import java.util.*;;
2 2 3 3 class Solution {
4 4 public static int peakIndex(int[] nums) {
5 − int left = 0;
6 − int right = nums.length - 1;
7 − int peak = -1;
5 + if (nums.length == 1) return 0;
6 + if (nums.length == 2) return nums[0] > nums[1] ? 0 : 1;
8 8 9 − while(left <= right) {
10 − if(left == right) {
11 − peak = left;
12 − break;
13 − }
14 − 15 − int mid = (left + right) / 2;
16 − 17 − if(nums[mid] < nums[mid + 1]) {
18 − left = mid + 1;
8 + boolean ascending = false;
9 + 10 + for (int i = 0; i < nums.length - 1; ++i) {
11 + if (nums[i] < nums[i + 1]) {
12 + ascending = true;
19 19 } else {
20 − right = mid;
14 + if (ascending && nums[i] > nums[i + 1]) {
15 + return i;
16 + }
17 + ascending = false;
21 21 }
22 22 }
23 23 24 − return peak;
21 + if (ascending) {
22 + return nums.length - 1;
23 + } else {
24 + return -1;
25 + }
25 25 }
26 26 }
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 peakIsTheLastNumber() { assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5})); } @Test public void flatland() { assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1})); } }
… Expand 1 1 import org.junit.Test;
2 2 import static org.junit.Assert.assertEquals;
3 3 import org.junit.runners.JUnit4;
4 4 5 5 // TODO: Replace examples and use TDD development by writing your own tests
6 6 7 7 public class SolutionTest {
8 8 @Test
9 9 public void test1() {
10 10 assertEquals(2, Solution.peakIndex(new int[] {1, 2, 3, 1}));
11 11 }
12 12 13 13 @Test
14 14 public void test2() {
15 15 assertEquals(1, Solution.peakIndex(new int[] {1, 2, 1}));
16 16 }
17 17 18 18 @Test
19 19 public void test3() {
20 20 assertEquals(0, Solution.peakIndex(new int[] {2}));
21 21 }
22 22 23 23 @Test
24 24 public void test4() {
25 25 assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5, 4, 3, 2}));
26 26 }
27 27 28 28 @Test
29 − public void test5() {
30 − 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}));
29 + public void peakIsTheLastNumber() {
30 + assertEquals(4, Solution.peakIndex(new int[] {1, 2, 3, 4, 5}));
31 + }
32 + 33 + @Test
34 + public void flatland() {
35 + assertEquals(-1, Solution.peakIndex(new int[] {1, 1, 1, 1, 1}));
31 31 }
32 32 }
import java.util.*; import java.util.stream.*; class Solution { public static String largestNumber(Integer[] nums) { final StringBuilder sb = new StringBuilder(); return Arrays.stream(nums) .parallel() .map((n) -> n.toString()) .sorted(Solution::compareNumberString) .collect(Collectors.joining()) .toString(); } private static int compareNumberString(String s1, String s2) { if (s2.startsWith(s1)) return compareNumberString(s2.substring(s1.length()), s1); if (s1.startsWith(s2)) return compareNumberString(s1.substring(s2.length()), s2); return s2.compareTo(s1); } }
1 1 import java.util.*;
2 + import java.util.stream.*;
2 2 3 3 class Solution {
4 4 public static String largestNumber(Integer[] nums) {
5 − Arrays.sort( nums, new Comparator<Integer>() {
6 − 7 − @Override
8 − public int compare(Integer a, Integer b) {
9 − String aStr = a.toString();
10 − String bStr = b.toString();
11 − 12 − return (aStr + bStr).compareTo(bStr + aStr) * -1;
13 − }
14 − 15 − } );
16 − 17 − String result = "";
18 − for(Integer num : nums) {
19 − result += num.toString();
20 − }
21 − 22 − return result;
6 + final StringBuilder sb = new StringBuilder();
7 + return Arrays.stream(nums)
8 + .parallel()
9 + .map((n) -> n.toString())
10 + .sorted(Solution::compareNumberString)
11 + .collect(Collectors.joining())
12 + .toString();
13 + }
14 + 15 + private static int compareNumberString(String s1, String s2) {
16 + if (s2.startsWith(s1)) return compareNumberString(s2.substring(s1.length()), s1);
17 + if (s1.startsWith(s2)) return compareNumberString(s1.substring(s2.length()), s2);
18 + return s2.compareTo(s1);
23 23 }
24 24 }
public class Primes {
public static boolean isAPrime(int number) {
return true;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Test
public void twoShouldBeAPrime() {
int numberToCheck = 2;
boolean expected = true;
boolean actual = Primes.isAPrime(numberToCheck);
assertEquals(expected, actual);
}
}
Polymer({
is: "pedal-assist",
properties:{
ppc:{
type:Number,
value:30,
},
gap:{
type:Number,
computed:"getGap(lPedel,rPedel)",
},
cycle:{
type:Number,
value:.5,
},
},
left: function(undefined){
this.lPedel = performance.now()
},
right: function(undefined){
this.rPedel = performance.now()
},
getGap: function(lPedel,rPedel){
var newGap = Math.sqrt((lPedel - rPedel) * (lPedel - rPedel))
var r1 = this.gapOld / this.gap
var r2 = this.gap / newGap
console.log(r1,r2,newGap)
if (r1 < 1.2 && r1 > .8 && r2 < 1.2 && r2 > .8) {
this.runMotor(this.cycle,this.ppc,this.gap)
}
this.gapOld = this.gap
return newGap
},
runMotor: function(cycle,ppc,time){
var n= Math.floor(ppc*cycle)
for (var i=1;i<n;i++) {
setTimeout(function(that){
if (that.black == 0) {
that.set("black", 12)
that.yellow = 0
that.red = -12
} else if (that.yellow == 0) {
that.black = -12
that.yellow = 12
that.red = 0
} else if (that.red == 0) {
that.black = 0
that.yellow = -12
that.red = 12
}
}, (time*cycle)*(i/n),this)
console.log((time*cycle)*(i/n))
}
setTimeout(function(that){
that.black = 0
that.yellow = 0
that.red = 0
}, (time*cycle)+10,this)
},
})
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals("actual", "expected", "This is just an example of how you can write your own TDD tests");
});
});