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.
More efficient implementation for large arrays.
Basic check = n(n+1)/2
vs Sorting + Linear check = O(n log n) + O(n)
vs Hash = O(n)
class Kumite { public static function findduplicatenumber(nums: Array<Int>): Null<Int> { var testArray: Array<Int> = nums.copy(); if (testArray.length < 12) { while (testArray.length > 0) { var testNum: Int = testArray.pop(); if (testArray.indexOf(testNum) != -1) return testNum; } } else if (testArray.length < 100) { testArray.sort((a, b) -> a - b); for(i in 0...(testArray.length - 1)) { if(testArray[i] == testArray[i + 1]) return testArray[i]; } } else { var myMap: Map<Int, Bool> = []; for(i in testArray){ if (myMap.exists(i)) return i; myMap.set(i, true); } } return null; } }- class Kumite
- {
public static function findduplicatenumber(nums:Array<Int>):Null<Int> {var testArray:Array<Int> = nums.copy();while(testArray.length > 0){var testNum:Int = testArray.shift();if(testArray.indexOf(testNum) != -1){return testNum;}- public static function findduplicatenumber(nums: Array<Int>): Null<Int> {
- var testArray: Array<Int> = nums.copy();
- if (testArray.length < 12) {
- while (testArray.length > 0) {
- var testNum: Int = testArray.pop();
- if (testArray.indexOf(testNum) != -1) return testNum;
- }
- } else if (testArray.length < 100) {
- testArray.sort((a, b) -> a - b);
- for(i in 0...(testArray.length - 1)) {
- if(testArray[i] == testArray[i + 1]) return testArray[i];
- }
- } else {
- var myMap: Map<Int, Bool> = [];
- for(i in testArray){
- if (myMap.exists(i)) return i;
- myMap.set(i, true);
- }
- }
- return null;
- }
- }
// Tests are written using https://github.com/haxe-utest/utest import utest.Assert; import Solution; class SolutionTest extends utest.Test { function testExample() { Assert.equals(3, Kumite.findduplicatenumber([1,2,3,3,5])); Assert.equals(22, Kumite.findduplicatenumber([4,2,46,1,5,26,263,51,463,11,41,44,3,45,43,33,32,514,451,23,465,456,21,31,53,444,16,442,36,441,42,516,52,13,333,316,34,22,111,6,136,61,335,424,22])); Assert.equals(234, Kumite.findduplicatenumber([418,300,968,268,215,70,11,716,683,114,411,705,748,942,295,929,365,769,591,473,717,907,804,397,731,234,939,144,745,631,662,119,463,287,34,210,887,307,343,515,410,401,178,918,430,205,569,234,558,602,790,369,696,854,750,450,251,290,87,162,49,425,83,266,788,562,821,54,326,8,772,911,545,400,76,785,495,197,987,278,282,829,294,880,800,91,338,526,136,860,649,358,243,254,792,199,126,100,567,269])); } }- // Tests are written using https://github.com/haxe-utest/utest
- import utest.Assert;
- import Solution;
- class SolutionTest extends utest.Test {
- function testExample() {
- Assert.equals(3, Kumite.findduplicatenumber([1,2,3,3,5]));
- Assert.equals(22, Kumite.findduplicatenumber([4,2,46,1,5,26,263,51,463,11,41,44,3,45,43,33,32,514,451,23,465,456,21,31,53,444,16,442,36,441,42,516,52,13,333,316,34,22,111,6,136,61,335,424,22]));
- Assert.equals(234, Kumite.findduplicatenumber([418,300,968,268,215,70,11,716,683,114,411,705,748,942,295,929,365,769,591,473,717,907,804,397,731,234,939,144,745,631,662,119,463,287,34,210,887,307,343,515,410,401,178,918,430,205,569,234,558,602,790,369,696,854,750,450,251,290,87,162,49,425,83,266,788,562,821,54,326,8,772,911,545,400,76,785,495,197,987,278,282,829,294,880,800,91,338,526,136,860,649,358,243,254,792,199,126,100,567,269]));
- }
- }
Changed
if(olives <= 0){
return 0;
}
return (oil/olives) * 100;
To:
return olives <= 0 ? 0 : (oil/olives) * 100;
For:
A) better readability and cleaner look(opinion)
B) Lesser lines used
from itertools import zip_longest def mirror_fold(arr): """Fold a list by summing mirrored pairs from the outside in. Examples: [1, 2, 3, 4] -> [5, 5] [1, 2, 3, 4, 5] -> [6, 6, 3] """ mid = (len(arr)+1)//2 return [a+b for a,b in zip_longest(arr[:mid], reversed(arr[mid:]), fillvalue=0)]- from itertools import zip_longest
- def mirror_fold(arr):
- """Fold a list by summing mirrored pairs from the outside in.
- Examples:
- [1, 2, 3, 4] -> [5, 5]
- [1, 2, 3, 4, 5] -> [6, 6, 3]
- """
left = 0right = len(arr) - 1folded = []while left < right:folded.append(arr[left] + arr[right])left += 1right -= 1if left == right:folded.append(arr[left])return folded- mid = (len(arr)+1)//2
- return [a+b for a,b in zip_longest(arr[:mid], reversed(arr[mid:]), fillvalue=0)]