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.

I have undertaken a study of recursion. I find myself reaching for it fairly consistenly to solve different Kata but often find my understanding of certain concepts laking enough that I end up going with an iterative approach instead.

the goal of this was to produce in exponent notation the prime factors of a number.

The problem with this one is that because I created a new dictionary at the top of primeFactoring and because there is not an array or dictionary passed into it I ended up creating another function (factorPart) to fill the dictionary.
even ignoring all the messy formating code the solution seems convoluted.

What would be a better approach and what concepts should be understood to improve this answer?

def primeFactoring(n):
    dict = {}
    print(n)
    factorPart(n,dict)
    output = ""
    for d, v in dict.items():
        if v > 1:
            output += str(d)+"^"+str(v)+" X "
        else: output += str(d)+" X "
    output = output.strip(" X ")
    print(dict) #key being the factor and value being the number of occurrences
    print(output) #formated in exponant form
    return output
    
def factorPart(num, dict):
    x = 2
    while x <= num:
        if num / x == num // x:
            dict[x] = dict[x] + 1 if x in dict else 1
            return factorPart(num // x, dict)
        x+=1
Numbers
Algorithms

Summary

In this kata you have to make a function named converter.

What Does the converter Function do?

It converts a negitive number to a positive one and a positive number to a negetive one.

  1. Check the parameter
  • If it is not integer just return False
  1. Check the word if it is negitive or positive

  2. Convert the number

  • if negitive convert to positive
  • if positive convert to negitive

Now just return the final answer... Done!

Examples

  • # Negitive to Positive
    convert(-1) === 1
    convert(-72) === 72
    
    # Positive to Negitive
    convert(24) === -24
    convert(32) === -32
    
    # Invalid Sequences
    convert("23") === False
    convert("-94") === False
    convert(True) === False
    convert([123, 321]) == False
    convert({123, 1232, 34}) === False
    
def convert(number):
        if isinstance(number, int):
            # Check if number is negitive
            if number < 0:
                # negitive -> positive
                return (abs(number))

            else:
                # positive -> negitive
                return (number * -1)
        
        else:
            return False
Code
Diff
  • def find_max(arr):
        return max(arr)
  • 11
    def find_max(arr):
    
    2
        return sorted(arr)[-1]
    
    2+
        return max(arr)
    

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}