Begin a new Kumite
Search
About
  • Filter by Language:
  • 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.

Algorithms
Search
Strings

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

  1. if no substring is found the function returns NULL
  2. 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
}
Code
Diff
  • define_method :f, &:*
  • 1
    f = -> (s, n) do
    
    2
      s * n
    
    3
    end
    
    1+
    define_method :f, &:*
    
Code
Diff
  • decr = lambda a: sum(map(lambda h: int(h, 16), a.split(':')))
  • 1
    decr = lambda a: sum(int(h, 16) for h in a.split(':'))
    
    1+
    decr = lambda a: sum(map(lambda h: int(h, 16), a.split(':')))
    

Recent Moves:

ruby lambda function

Code
Diff
  • 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
    

ruby lamba function

Code
Diff
  • 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
    

ruby lambda function

Code
Diff
  • 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
    
    33
                    
    
    44
                    
    
Mathematics
Algorithms
Numbers
Code
Diff
  • 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
    
Fundamentals
Puzzles
Games

ruby lambda function

Code
Diff
  • 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
    

Ruby lambda function

Code
Diff
  • 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