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.

Code
Diff
  • def find_max(arr):
        return sorted(arr)[-1]
  • 11
    def find_max(arr):
    
    2
        return 0
    
    2+
        return sorted(arr)[-1]
    

Recent Moves:

Mathematics
Algorithms
Numbers

Given a positive number n > 1 find the prime factor decomposition of n. Format the result as a string in the following form :

"(p_1**n_1)(p_2**n_2)...(p_k**n_k)"
where a ** b means a to the power of b

with the p_i in increasing order without the "**n_i".

Examples:
n = 17 should return "(17)"
n = 10976 should return "(2**5)(7**3)"
n = 86240 should return "(2**5)(5)(7**2)(11)"

Code
Diff
  • require 'prime'
    def primeFactors(n)
      Prime.prime_division(n).map do |n|
        "(#{n[0]}#{n[1] > 1 ? '**' + n[1].to_s : ''})"
      end*""
    end
  • 11
    require 'prime'
    
    2
    33
    def primeFactors(n)
    
    4
      result = ''
    
    5
      Prime.prime_division(n).each do |n|
    
    6
        result += '(' + n[0].to_s
    
    7
        n[1] < 2 ? result += ')' : result += '**' + n[1].to_s + ')'
    
    8
      end
    
    9
      
    
    10
      result
    
    3+
      Prime.prime_division(n).map do |n|
    
    4+
        "(#{n[0]}#{n[1] > 1 ? '**' + n[1].to_s : ''})"
    
    5+
      end*""
    
    1111
    end
    
Fundamentals
Algorithms

In my opinion, the code is cleaner with destructuring and math.hypot. Also, snake_case is preferred.

Code
Diff
  • import math
    
    def goes_to_jail(directions):
        x = 0 ; y = 0
        for north,east,south,west in directions:
            x += (east - west) * 274
            y += (north - south) * 80
            if math.hypot(x, y) > 2025:
                return True
        return False
  • 1
    def goesToJail(directions):
    
    2
        location=[0,0]
    
    3
        #North+East are postive numbers West+South are a negative numbers
    
    4
        for direction in directions:
    
    5
            location[0]=location[0]+(direction[0]*80)-(direction[2]*80)
    
    6
            location[1]=location[1]+(direction[1]*274)-(direction[3]*274)
    
    7
            #a squared + b squared = c squared
    
    8
            if( (location[0]**2+location[1]**2)**(1/2) > 2000 ):
    
    1+
    import math
    
    2+
    3+
    def goes_to_jail(directions):
    
    4+
        x = 0 ; y = 0
    
    5+
        for north,east,south,west in directions:
    
    6+
            x += (east - west) * 274
    
    7+
            y += (north - south) * 80
    
    8+
            if math.hypot(x, y) > 2025:
    
    99
                return True
    
    1010
        return False
    

Golfed solution.

Code
Diff
  • function* generateInterval(a,b,c){for(;c>0?a<=b:a>=b;a+=c)yield a}
  • 1
    function* generateInterval(start, stop, step) {
    
    2
      if (step >= 0) { // infinite lists with step 0 are a-okay
    
    3
        for (let cursor=start; cursor<=stop; cursor+=step) yield cursor;
    
    4
      } else if (step < 0) {
    
    5
        for (let cursor=start; cursor>=stop; cursor+=step) yield cursor;
    
    6
      }
    
    7
    }
    
    1+
    function* generateInterval(a,b,c){for(;c>0?a<=b:a>=b;a+=c)yield a}
    

Recent Moves:

I'm using capturing split instead of match. This handles a couple of edge cases including empty string inputs and inputs containing two numbers in a row.

Code
Diff
  • function transformText(s) {
      return s.split(/(\d+)/)
        .map(w => w.trim())
        .filter(w => w)
        .map(w => (
          /\d/.test(w) ? `<div>${w}</div>` : `<span>${w}</span>`
        ))
        .join``;
    }
  • 1
    function transformText(string) {
    
    2
      const regExp = /[\D]+|([\d+\.]+)/g
    
    3
      const numsRegExp = /[0-9\.]+/g
    
    4
      
    
    5
      const splited = string.match(regExp)
    
    6
      
    
    7
      const res = splited.map((item) => {
    
    8
        if (numsRegExp.test(item)) {
    
    9
          return `<div>${item}</div>`
    
    10
        } else {
    
    11
          return `<span>${item.replace(/^\s|\s$/g, "")}</span>`
    
    12
        }
    
    13
      })
    
    14
      
    
    15
      return res.join("")
    
    1+
    function transformText(s) {
    
    2+
      return s.split(/(\d+)/)
    
    3+
        .map(w => w.trim())
    
    4+
        .filter(w => w)
    
    5+
        .map(w => (
    
    6+
          /\d/.test(w) ? `<div>${w}</div>` : `<span>${w}</span>`
    
    7+
        ))
    
    8+
        .join``;
    
    1616
    }
    

Recent Moves: