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.
def get_prime_factorization(number):
"""Gets the prime factorization of a number, and returns it.
@type number: float
@param number: A float representing the number to be factored.
"""
current_dividing_factor = float(number)
if current_dividing_factor == 1.0 or current_dividing_factor == 0.0 or current_dividing_factor == -1.0:
return [current_dividing_factor]
factors = []
# Catches all negative numbers and makes them positive.
if current_dividing_factor < 0.0:
factors.append(-1.0)
current_dividing_factor *= -1.0
# Catches all numbers not whole numbers and makes them whole numbers.
if current_dividing_factor % 1.0 != 0.0:
decimal_places = len(str(current_dividing_factor % 1.0)) - 2.0
factors.append(10.0 ** (-decimal_places))
current_dividing_factor *= 10.0 ** decimal_places
# Gets all factors greater than 2.0 and adds them to the factor list.
test_factor = 2.0
while test_factor <= current_dividing_factor:
if current_dividing_factor % test_factor == 0.0:
factors.append(test_factor)
current_dividing_factor /= test_factor
test_factor = 2.0
else:
test_factor += 1.0
return factors
test.assert_equals(get_prime_factorization(-25.2), [-1.0, 0.1, 2.0, 2.0, 3.0, 3.0, 7.0])
test.assert_equals(get_prime_factorization(-10), [-1.0, 2.0, 5.0])
test.assert_equals(get_prime_factorization(-1.0), [-1.0])
test.assert_equals(get_prime_factorization(-.2), [-1.0, 0.1, 2])
test.assert_equals(get_prime_factorization(-.05), [-1.0, 0.01, 5])
test.assert_equals(get_prime_factorization(0.0), [0.0])
test.assert_equals(get_prime_factorization(0.36), [0.01, 2, 2, 3, 3])
test.assert_equals(get_prime_factorization(0.7), [0.1, 7])
test.assert_equals(get_prime_factorization(1.0), [1.0])
test.assert_equals(get_prime_factorization(1.3), [0.1, 13])
test.assert_equals(get_prime_factorization(1.95), [0.01, 3, 5, 13])
test.assert_equals(get_prime_factorization(3.0), [3.0])
test.assert_equals(get_prime_factorization(30.0), [2.0, 3.0, 5.0])
test.assert_equals(get_prime_factorization(683.0), [683.0])
test.assert_equals(get_prime_factorization(7130.0), [2.0, 5.0, 23.0, 31.0])
This small piece of code calculates the prime factorisation of any given number.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Factorisation {
public static List<Integer> primeFactorsOf(int input) {
List<Integer> pFactors = new ArrayList<>();
long number = input;
for (int i = 2; i <= number; i++) {
if (number % i == 0) {
pFactors.add(i);
number /= i;
i--;
}
}
return pFactors;
}
public static void main(String[] args) {
}
}
A simple algorithm to find whether a set of ranges overlap.
Notes:
- This could take in any number of ranges.
- Have a look at the gist with some tests to play with the code.
- The code creates a flat array with the start and end of each range.
- This array will retain the order if they don't overlap. (Its easier to visualize with some examples than textually explaining why, try it).
class Range
include Comparable
def <=>(other)
self.begin <=> other.begin
end
def self.overlap?(*ranges)
edges = ranges.sort.flat_map { |range| [range.begin, range.end] }
edges != edges.sort.uniq
end
end
Range.overlap?(2..12, 6..36, 42..96) # => true
[
[true, [2..12, 6..36, 42..96]],
].each do |result, ranges|
Test.assert_equals result, Range.overlap?(*ranges)
end
Hello world! style application, Perl style.
print "Hello Perl!\n";
Write a function that takes as a parameter a starting square as a string in formal chess notation and determines the valid squares that the bishop can move to in one move for an empty chess board (only bishop on the board). Must return an array of valid squares in ascending square order.
function moveBishop($startingSquare)
{
$result = [];
$letters = ["a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "f" => 6, "g" => 7, "h" => 8];
$startSqaureInDigits = $letters[$startingSquare[0]] . $startingSquare[1];
// bishop movement is always two ways at the same time as it moves diagonally at least one space.
// loop over 8 by 8 chessboard
//i loop is for letters
//j loop is for numbers
for($i = 1; $i <= 8; $i++){
for($j=1; $j <= 8; $j++)
{
if ($startSqaureInDigits != $i.$j && abs($startSqaureInDigits[0] - $i) == abs($startSqaureInDigits[1] - $j) )
{
$result[] = array_search($i,$letters).$j;
}
}
}
return $result;
}
Given a string of open and closed parenthesis output "Balanced" if the parenthesis are balanced or "Unbalanced" otherwise.
A string is balanced if it consists entirely of pairs of opening/closing parenthesis (in that order), none of which mis-nest.
Example input:
(())())
Example output:
Unbalanced
Example input:
(()())
Example output:
Balanced
function balanced_parenthesis(s) {
if(s == null) return null;
var i = 0,
startCnt = 0,
n = s.length;
for(i = 0; i < n; i++) {
if(s[i] === "(") {
startCnt += 1;
} else if(s[i] === ")") {
startCnt -= 1;
if(startCnt < 0) {
break;
}
}
}
if(startCnt !== 0) {
return 'Unbalanced';
} else {
return 'Balanced';
}
}
describe("Balanced Parenthesis Tests", function(){
it ("should return Balanced", function(){
Test.assertEquals(balanced_parenthesis("(()())"), "Balanced");
});
it ("should return Unbalanced", function(){
Test.assertEquals(balanced_parenthesis("(())())"), "Unbalanced");
});
it ("should return Unbalanced", function(){
Test.assertEquals(balanced_parenthesis(")()("), "Unbalanced");
});
it ("should return null for null input", function(){
Test.assertEquals(balanced_parenthesis(), null);
});
});
def foo(l):
r = copy(l)
return r
l = [1,2]
print(l)
n = foo(l)
print(n)
l.append(7)
print(n)
print(l)
def foo(l):
r = l
return r
l = [1,2]
print(l)
n = foo(l)
print(n)
l.append(7)
print(n)
print(l)
Generate prime numbers within a given minimum and maximum.
min and max values should be positive (greater than 0).
import java.util.*;
public class Primes {
public static List<Integer> generatePrimes(int min, int max) {
List<Integer> primes = new ArrayList<Integer>();
boolean isPrime = false;
if((min > max) || min < 0 || max <= 0) {
return null;
}
for(int i = min; i <= max; i++) {
long endLimit = (long)Math.floor(Math.sqrt(i));
isPrime = true;
for(long j = 2; j <= endLimit; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
primes.add(i);
}
}
return primes;
}
}
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class PrimesTest {
@Test
public void testCase1() {
List<Integer> expected = Arrays.asList(2,3,5,7);
assertEquals("Should return 2,3,5,7", expected, Primes.generatePrimes(2,10));
}
@Test
public void testCase2() {
assertNull("Should return null", Primes.generatePrimes(0, 0));
}
@Test
public void testCase3() {
assertNull("Should return null", Primes.generatePrimes(-4, 4));
}
@Test
public void testCase4() {
List<Integer> expected = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97);
assertEquals("Should return primes till 100", expected, Primes.generatePrimes(2,100));
}
}