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.
Print a square of numbers, but in a string rather then a list.
Improvement ideas, add a second parameter to choose whether the output should be a list or string.
def square(n,s=0): l=["*"*n]*n return '\n'.join(l) if s else l
def square(n):print(("*" *n+"\n")*n) # to see the resultreturn (("*" *n+"\n")*n)[:-1]- def square(n,s=0):
- l=["*"*n]*n
- return '\n'.join(l) if s else l
test.assert_equals(square(2,1),"**\n**") test.assert_equals(square(5,1),'*****\n*****\n*****\n*****\n*****') test.assert_equals(square(4,1),'****\n****\n****\n****') test.assert_equals(square(2),["**","**"]) test.assert_equals(square(5),["*****","*****","*****","*****","*****"]) test.assert_equals(square(4),["****","****","****","****"])
# TODO: Given a number, write a function that prints a square with the caracter '*'# 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)# You can use Test.describe and Test.it to write BDD style test groupings# test.assert_equals(square(2),['**', '**'])# test.assert_equals(square(3),['***', '***', '***'])# test.assert_equals(square(4),['****', '****', '****', '****'])# test.assert_equals(square(5),['*****', '*****', '*****', '*****', '*****'])test.assert_equals(square(2),"**\n**")test.assert_equals(square(5),'*****\n*****\n*****\n*****\n*****')test.assert_equals(square(4),'****\n****\n****\n****')- test.assert_equals(square(2,1),"**\n**")
- test.assert_equals(square(5,1),'*****\n*****\n*****\n*****\n*****')
- test.assert_equals(square(4,1),'****\n****\n****\n****')
- test.assert_equals(square(2),["**","**"])
- test.assert_equals(square(5),["*****","*****","*****","*****","*****"])
- test.assert_equals(square(4),["****","****","****","****"])
class Sort: def merge_sort(self, nums): if nums == None: return None elif len(nums) <= 1: return nums vLeft = self.merge_sort(nums[:len(nums) // 2]) vRight= self.merge_sort(nums[len(nums) // 2:]) lLeft,lRight = len(vLeft),len(vRight) i,j,k = 0,0,0 while i < lLeft and j < lRight: if vLeft[i] < vRight[j]: nums[k] = vLeft[i] i += 1 else: nums[k] = vRight[j] j += 1 k += 1 while i < lLeft: nums[k] = vLeft[i] i += 1 k += 1 while j < lRight: nums[k] = vRight[j] j += 1 k += 1 return nums
- class Sort:
- def merge_sort(self, nums):
- if nums == None: return None
if len(nums) > 1:mid = len(nums) // 2lefthalf = nums[:mid]righthalf = nums[mid:]self.merge_sort(lefthalf)self.merge_sort(righthalf)i = 0j = 0k = 0while i < len(lefthalf) and j < len(righthalf):if lefthalf[i] < righthalf[j]:nums[k] = lefthalf[i]i += 1else:nums[k] = righthalf[j]j += 1k += 1while i < len(lefthalf):nums[k] = lefthalf[i]- elif len(nums) <= 1: return nums
- vLeft = self.merge_sort(nums[:len(nums) // 2])
- vRight= self.merge_sort(nums[len(nums) // 2:])
- lLeft,lRight = len(vLeft),len(vRight)
- i,j,k = 0,0,0
- while i < lLeft and j < lRight:
- if vLeft[i] < vRight[j]:
- nums[k] = vLeft[i]
- i += 1
k += 1while j < len(righthalf):nums[k] = righthalf[j]- else:
- nums[k] = vRight[j]
- j += 1
k += 1- k += 1
- while i < lLeft:
- nums[k] = vLeft[i]
- i += 1
- k += 1
- while j < lRight:
- nums[k] = vRight[j]
- j += 1
- k += 1
- return nums
Changed from a static variable to a dynamic function.
module Solution export greet function greet( whom::String ) "Hello " * whom * "!" end end
println("Hello Julia!")const hello = "world"- module Solution
- export greet
- function greet( whom::String )
- "Hello " * whom * "!"
- end
- end
using FactCheck facts("greetings") do @fact Solution.greet("world") --> "Hello world!" @fact Solution.greet("Julia") --> "Hello Julia!" @fact Solution.greet("Fred") --> "Hello Fred!" @fact Solution.greet("Kmactavish") --> "Hello Kmactavish!" end
facts("Is hello world?") do@fact hello => "world"- using FactCheck
- facts("greetings") do
- @fact Solution.greet("world") --> "Hello world!"
- @fact Solution.greet("Julia") --> "Hello Julia!"
- @fact Solution.greet("Fred") --> "Hello Fred!"
- @fact Solution.greet("Kmactavish") --> "Hello Kmactavish!"
- end
def add(string, option): odd,even = 0,0 for x in string: x = int(x) if x%2 ==0:even+=x if x%2 ==1:odd+=x if option ==0: return odd+even return even if option == 2 else odd
- def add(string, option):
even_list = []odd_list = []all_sum = 0for num in string:num = int(num)all_sum += numif num % 2 == 0:even_list.append(num)if num % 2 != 0:odd_list.append(num)if option == 0:return all_sumif option == 1:return sum(odd_list)if option == 2:return sum(even_list)- odd,even = 0,0
- for x in string:
- x = int(x)
- if x%2 ==0:even+=x
- if x%2 ==1:odd+=x
- if option ==0:
- return odd+even
- return even if option == 2 else odd
test.assert_equals(add("1234567", 0),28) test.assert_equals(add("1234567", 1),16) test.assert_equals(add("1234567", 2),12)
test.assert_equals(add("1234567" , 0),28)test.assert_equals(add("1234567" , 1),16)test.assert_equals(add("1234567" , 2),12)- test.assert_equals(add("1234567", 0),28)
- test.assert_equals(add("1234567", 1),16)
- test.assert_equals(add("1234567", 2),12)
def distance(a): y,y1 = 0,0 for i in range(len(a)): if "y" in a[i]: y = i if "x" in a[i]: y1 = i x ,x1 = a[y].index("y"), a[y1].index("x") return abs(x -x1) + abs(y-y1)
def distance(array):for i in range(len(array)):if "y" in array[i] : y = abs(array[i].index("y") - i)if "x" in array[i] : x = abs(array[i].index("x") - i)return (x+y)- def distance(a):
- y,y1 = 0,0
- for i in range(len(a)):
- if "y" in a[i]: y = i
- if "x" in a[i]: y1 = i
- x ,x1 = a[y].index("y"), a[y1].index("x")
- return abs(x -x1) + abs(y-y1)
def fizz_buzz(n): return ['FizzBuzz' if (i % 15 == 0) else 'Fizz' if(i % 3 == 0) else 'Buzz' if(i % 5 == 0) else i for i in range(1,n)]
- def fizz_buzz(n):
for i in range(1, n):yield "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or i- return ['FizzBuzz' if (i % 15 == 0) else 'Fizz' if(i % 3 == 0) else 'Buzz' if(i % 5 == 0) else i for i in range(1,n)]
A different approach from the one above, done in one line.
const minutes = i => `${Math.trunc(i / 60)}:${i % 60 < 10 ? "0" : ""}${i % 60}`;
const toHours = i => Math.floor(i / 60);const setDigits = i => `0${i}`.slice(-2);const getTime = i => [toHours(i), i % 60];const toTime = arr => `${arr[0]}:${setDigits(arr[1])}`;const minutes = i => toTime(getTime(i));- const minutes = i => `${Math.trunc(i / 60)}:${i % 60 < 10 ? "0" : ""}${i % 60}`;
Using Math.max
public class BiggerNum{ /** * @param a integer of param1 * @param b integer of param2 * @return the bigger integer of a and b * If a equals b, return either one */ public static int compare(int a, int b) { return Math.max(a,b); } }
- public class BiggerNum{
- /**
- * @param a integer of param1
- * @param b integer of param2
- * @return the bigger integer of a and b
* If a equals b, eiter one is acceptance- * If a equals b, return either one
- */
- public static int compare(int a, int b) {
return a >= b ? a : b;- return Math.max(a,b);
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void should_return_a_when_a_gt_b() { int a = 10; int b = 5; assertEquals(a, BiggerNum.compare(a, b)); } @Test public void should_return_b_when_a_lt_b() { int a = 4; int b = 5; assertEquals(b, BiggerNum.compare(a, b)); } @Test public void should_return_either_when_a_eq_b() { int a = 5; int b = 5; assertEquals(5, BiggerNum.compare(a, b)); } }
- 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 should_return_a_when_a_gt_b() {
- int a = 10;
- int b = 5;
- assertEquals(a, BiggerNum.compare(a, b));
- }
- @Test
- public void should_return_b_when_a_lt_b() {
- int a = 4;
- int b = 5;
- assertEquals(b, BiggerNum.compare(a, b));
- }
- @Test
- public void should_return_either_when_a_eq_b() {
- int a = 5;
int b = 5;- int b = 5;
- assertEquals(5, BiggerNum.compare(a, b));
- }
- }