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.
Write a function called findFirstSubString
that accept two not null string(string and substring
) and returns the index of the first occurrence of the substring in the string.
Examples
findFirstSubString("1234","12") => returns 0
findFirstSubString("012","12") => returns 1
findFirstSubString("4321","21") => returns 2
findFirstSubString("ABCDE","21") => returns null
findFirstSubString("","21") => returns null
Notes
- if no substring is found the function returns NULL
- the function returns only the index of the first occurrence of the string
fun findFirstSubString(string: String, subString: String): Int? {
val stringArray = string.toList()
val substringArray = subString.toList()
if (stringArray.size < substringArray.size || stringArray.isEmpty() || substringArray.isEmpty()) {
return null
}
var counter = 0
var startIndex = 0
var i = 0
while (i < stringArray.size) {
if (stringArray[i] == substringArray[counter]) {
if(counter == 0) {
startIndex = i
}
if ((counter + 1) == substringArray.size) {
return startIndex
}
counter++
i++
}
else {
if (counter > 0) {
i = startIndex + 1
}
else {
i++
}
counter = 0
startIndex = 0
}
println(i)
}
return null
}
import org.junit.Assert
import org.junit.Test
class SimpleSubStringSearchKtTest {
@Test
fun `Substring longer than string`() {
Assert.assertNull(findFirstSubString("12345", "123456"))
Assert.assertNull(findFirstSubString("54321", "123456"))
}
@Test
fun `Empty string`() {
Assert.assertNull(findFirstSubString("", "123456"))
}
@Test
fun `Empty substring`() {
Assert.assertNull(findFirstSubString("12323123", ""))
}
@Test
fun `Empty substring and string`() {
Assert.assertNull(findFirstSubString("", ""))
}
@Test
fun `Correct finding`() {
Assert.assertEquals(findFirstSubString("12345", "123"), 0)
Assert.assertEquals(findFirstSubString("54321321", "321"), 2)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "ABCDE"), 9)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "A"), 9)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "a"), null)
Assert.assertEquals(findFirstSubString("111234", "1234"), 2)
}
}
Test.assert_equals(f("lsadfkj", 3), "lsadfkjlsadfkjlsadfkj") Test.assert_equals(f("hello", 4), "hellohellohellohello") Test.assert_equals(f("hu", 0), "")
1 − Test.assert_equals(f.("lsadfkj", 3), "lsadfkjlsadfkjlsadfkj")
2 − Test.assert_equals(f.("hello", 4), "hellohellohellohello")
3 − Test.assert_equals(f.("hu", 0), "")
1 + Test.assert_equals(f("lsadfkj", 3), "lsadfkjlsadfkjlsadfkj")
2 + Test.assert_equals(f("hello", 4), "hellohellohellohello")
3 + Test.assert_equals(f("hu", 0), "")
Recent Moves:
ruby lambda function
decr = ->(a) do a.split(":").map{|x| x.to_i(16)}.sum end
1 − decr = lambda a: sum(int(h, 16) for h in a.split(':'))
1 + decr = ->(a) do
2 + a.split(":").map{|x| x.to_i(16)}.sum
3 + end
Test.assert_equals(decr.call('a:2:35:af:1'),241) Test.assert_equals(decr.call('b:356:3af:cba24:100'),836148) Test.assert_equals(decr.call('a:bbbcefff:134fdea:100'),3169971955) Test.assert_equals(decr.call('effbc4:edfc31:aedfc2506'),46973721851) Test.assert_equals(decr.call('ef23fae:123a'),250761704)
1 − test.assert_equals(decr('a:2:35:af:1'),241)
2 − test.assert_equals(decr('b:356:3af:cba24:100'),836148)
3 − test.assert_equals(decr('a:bbbcefff:134fdea:100'),3169971955)
4 − test.assert_equals(decr('effbc4:edfc31:aedfc2506'),46973721851)
5 − test.assert_equals(decr('ef23fae:123a'),250761704)
1 + Test.assert_equals(decr.call('a:2:35:af:1'),241)
2 + Test.assert_equals(decr.call('b:356:3af:cba24:100'),836148)
3 + Test.assert_equals(decr.call('a:bbbcefff:134fdea:100'),3169971955)
4 + Test.assert_equals(decr.call('effbc4:edfc31:aedfc2506'),46973721851)
5 + Test.assert_equals(decr.call('ef23fae:123a'),250761704)
ruby lambda function
switchout = -> (sentence, word, switch) do sentence.gsub(word, switch) end
1 − def switchout(sentence, word, switch):
2 − return sentence.replace(word, switch)
1 + switchout = -> (sentence, word, switch) do
2 + sentence.gsub(word, switch)
3 + end
Test.assert_equals(switchout.call("I am smart", "smart", "dumb"), "I am dumb") Test.assert_equals(switchout.call("Cats are fun", "fun", "evil"), "Cats are evil")
1 − test.assert_equals(switchout("I am smart", "smart", "dumb"), "I am dumb")
2 − test.assert_equals(switchout("Cats are fun", "fun", "evil"), "Cats are evil")
1 + Test.assert_equals(switchout.call("I am smart", "smart", "dumb"), "I am dumb")
2 + Test.assert_equals(switchout.call("Cats are fun", "fun", "evil"), "Cats are evil")
ruby lamba function
reverse_compliment = -> (s) do s.upcase.tr("ATGC", "TACG").reverse end
1 − reverse_compliment = lambda dna: dna.translate(dna.maketrans("ACGTacgt", "TGCATGCA"))[::-1]
1 + reverse_compliment = -> (s) do
2 + s.upcase.tr("ATGC", "TACG").reverse
3 + end
Test.assert_equals(reverse_compliment.("A"), "T") Test.assert_equals(reverse_compliment.("ATG"), "CAT") Test.assert_equals(reverse_compliment.("GATCCCTATTGGATATCTAGCATCATA"), "TATGATGCTAGATATCCAATAGGGATC") Test.assert_equals(reverse_compliment.("ATGGCATGA"), "TCATGCCAT")
1 − class MyTestCase(unittest.TestCase):
2 − def test_rev_comp_is_correct(self):
3 − 4 − self.assertEqual(reverse_compliment("A"), "T")
5 − self.assertEqual(reverse_compliment("ATG"), "CAT")
6 − self.assertEqual(reverse_compliment("GATCCCTATTGGATATCTAGCATCATA"), "TATGATGCTAGATATCCAATAGGGATC")
7 − self.assertEqual(reverse_compliment("ATGGCATGA"), "TCATGCCAT")
1 + Test.assert_equals(reverse_compliment.("A"), "T")
2 + Test.assert_equals(reverse_compliment.("ATG"), "CAT")
3 + Test.assert_equals(reverse_compliment.("GATCCCTATTGGATATCTAGCATCATA"), "TATGATGCTAGATATCCAATAGGGATC")
4 + Test.assert_equals(reverse_compliment.("ATGGCATGA"), "TCATGCCAT")
ruby lambda function
where = ->(arr,n) do arr.index(n) + 1 end
1 − def where(arr,n):
2 − return arr.index(n) + 1
1 + where = ->(arr,n) do
2 + arr.index(n) + 1
3 + end
3 3 4 4
Test.assert_equals(where.([1,2,3,6,7,8,0],3),3)
1 − import codewars_test as test
2 − # TODO Write tests
3 − import solution # or from solution import example
4 − # test.assert_equals(actual, expected, [optional] message)
5 − @test.describe("Example")
6 − def test_group():
7 − @test.it("test case")
8 − def test_case():
9 − test.assert_equals(where([1,2,3,6,7,8,0],3),3)
1 + Test.assert_equals(where.([1,2,3,6,7,8,0],3),3)
10 10 11 11
def average(g): return sum(g) / len(g) if g else None
1 − def average(g)
2 − g.empty? ? nil : g.sum.to_f/g.size
3 − end
1 + def average(g):
2 + return sum(g) / len(g) if g else None
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(average([1, 2, 3]), 2) test.assert_equals(average([2, 3]), 2.5) test.assert_not_equals(average([2, 3]), 2) test.assert_equals(average([0,0,0]), 0) test.assert_equals(average([0]), 0) test.assert_equals(average([]), None)
1 − Test.assert_equals(average([1, 2, 3]), 2)
2 − Test.assert_equals(average([2, 3]), 2.5)
3 − Test.assert_not_equals(average([2, 3]), 2)
4 − Test.assert_equals(average([0,0,0]), 0)
5 − Test.assert_equals(average([0]), 0)
6 − Test.assert_equals(average([]), nil)
1 + import codewars_test as test
2 + # TODO Write tests
3 + import solution # or from solution import example
7 7 5 + # test.assert_equals(actual, expected, [optional] message)
6 + @test.describe("Example")
7 + def test_group():
8 + @test.it("test case")
9 + def test_case():
10 + test.assert_equals(average([1, 2, 3]), 2)
11 + test.assert_equals(average([2, 3]), 2.5)
12 + test.assert_not_equals(average([2, 3]), 2)
13 + test.assert_equals(average([0,0,0]), 0)
14 + test.assert_equals(average([0]), 0)
15 + test.assert_equals(average([]), None)
16 + 8 8 9 9 10 10
ruby lambda function
minimal_square = ->(a, b) do [[a, b].min*2, [a, b].max].max**2 end
1 − def minimal_square(a, b):
2 − return max(min(a, b)*2, max(a, b))**2
3 − # This is my first kumite (∩_∩)
4 − # I think this is the best solution there's not much to improve on
5 − # The problem isn't too hard
1 + minimal_square = ->(a, b) do
2 + [[a, b].min*2, [a, b].max].max**2
3 + end
Test.assert_equals(minimal_square.(2, 3), 16) Test.assert_equals(minimal_square.(3, 7), 49)
1 − import codewars_test as test
2 − # TODO Write tests
3 − import solution # or from solution import example
4 − 5 − # test.assert_equals(actual, expected, [optional] message)
6 − @test.describe("Simple tests")
7 − def test_group():
8 − @test.it("Test 1")
9 − def test_case1():
10 − test.assert_equals(minimal_square(2, 3), 16)
11 − @test.it("Test 2")
12 − def test_case2():
13 − test.assert_equals(minimal_square(3, 7), 49)
1 + Test.assert_equals(minimal_square.(2, 3), 16)
2 + Test.assert_equals(minimal_square.(3, 7), 49)
Ruby lambda function
flip_the_number = -> (num) do num.reverse end
1 − def flip_the_number(num):
2 − return num[::-1]
1 + flip_the_number = -> (num) do
2 + num.reverse
3 + end
Test.assert_equals(flip_the_number.call("12345"), "54321") Test.assert_equals(flip_the_number.call("1973572"), "2753791")
1 − Test.assert_equals(flip_the_number("12345"), "54321");
2 − 3 − Test.assert_equals(flip_the_number("1973572"), "2753791");
1 + Test.assert_equals(flip_the_number.call("12345"), "54321")
2 + Test.assert_equals(flip_the_number.call("1973572"), "2753791")