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.

Given up to 3 non-repeating numbers from a range of 1-10, identify any prime numbers where 1 <= prime_numbers <= n where n is the number given by the concatenation of the given numbers put together in the order they were chosen. So given the numbers [1, 10, 8] is 1108.
Return the midmost prime number in the list you generated before.
If there are an even number of prime numbers, get the lower number.

Code
Diff
  • def middle_prime(*args):
      nArg = 3
      minArgValue = 1
      maxArgValue = 10
      argsLen = len(args)
      try:
        if argsLen > nArg:
          raise Exception('Only accepts up to {} params.'.format(nArg))
        elif argsLen < 1:
          raise Exception('At least 1 param is required.')
        else:
          for i in args:
            if not minArgValue <= i <= maxArgValue:
              raise Exception('Only accepts numbers from {} to {}.'.format(minArgValue, maxArgValue))
        num = int(''.join([str(i) for i in args]))
        primes = generate_primes(num)
        if primes:
          mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
          return primes[mididx]
        return -1
      except Exception as e:
        print(e)
        return -1
    
    def is_prime(number:int, primes = [2,3]):
      if not primes:
        primes = [2, 3]
      for i in primes:
        if not number % i:
          return False
      if not i % 2:
        i += 1
      while i <= (number // i):
        if not number % i:
          return False
        i += 2
      return True
      
    def generate_primes(number):
      primes = [2]
      i = 3
      while i <= number:
        if is_prime(i, primes):
          primes.append(i)
        i += 2
      return primes
  • 11 def middle_prime(*args):
    2- maximum = 10
    3- try:
    4- if len(args) > 3:
    5- raise Exception('Only 3 numbers are allowed.')
    6- else:
    7- for i in args:
    8- if not 0 < i <= maximum:
    9- raise Exception('Only type numbers from 1 to {}.'.format(maximum))
    2+ nArg = 3
    3+ minArgValue = 1
    4+ maxArgValue = 10
    5+ argsLen = len(args)
    6+ try:
    7+ if argsLen > nArg:
    8+ raise Exception('Only accepts up to {} params.'.format(nArg))
    9+ elif argsLen < 1:
    10+ raise Exception('At least 1 param is required.')
    11+ else:
    12+ for i in args:
    13+ if not minArgValue <= i <= maxArgValue:
    14+ raise Exception('Only accepts numbers from {} to {}.'.format(minArgValue, maxArgValue))
    15+ num = int(''.join([str(i) for i in args]))
    16+ primes = generate_primes(num)
    17+ if primes:
    18+ mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
    19+ return primes[mididx]
    20+ return -1
    21+ except Exception as e:
    22+ print(e)
    23+ return -1
    1010
    11- num = int(''.join([str(i) for i in args]))
    12- primes = [i for i in range(2, num + 1) if is_prime(i)]
    13- if primes:
    14- mididx = len(primes) // 2 if len(primes) % 2 else len(primes) // 2 - 1
    15- return primes[mididx]
    16- return -1
    17- except Exception as e:
    18- return e
    19-
    20-def is_prime(int:int):
    21- for i in range(2, int):
    22- if not int % i:
    23- return False
    24- return True
    25+def is_prime(number:int, primes = [2,3]):
    26+ if not primes:
    27+ primes = [2, 3]
    28+ for i in primes:
    29+ if not number % i:
    30+ return False
    31+ if not i % 2:
    32+ i += 1
    33+ while i <= (number // i):
    34+ if not number % i:
    35+ return False
    36+ i += 2
    37+ return True
    38+
    39+def generate_primes(number):
    40+ primes = [2]
    41+ i = 3
    42+ while i <= number:
    43+ if is_prime(i, primes):
    44+ primes.append(i)
    45+ i += 2
    46+ return primes

I would use a dictionary for this rather than adding multiple elif statements. It's easier to add new cases and is easier to read.

There's also no need to add items to a "grade" array if you use the dictionary approach as you can look up the key's (grade) value (score) straight away.

Removing the while loop means we don't need the y variable.

Applying the .upper() method to the input straight away makes it more readable in my oppinion.

Code
Diff
  • total = 0
    
    grade_dictionary = {
        "A+":4.0,
        "A":4.0,
        "A-":3.7,
        "B+":3.3,
        "B":3.0,
        "B-":2.7,
        "C+":2.3,
        "C":2.0,
        "C-": 1.7,
        "D": 1.0,
        }
    
    for _ in range(6):
        grade = input("Enter Your Grade For Each Class Listed in Order (Letter Form): ").upper()
        total += grade_dictionary.get(grade, 0)
        
    print(total / 6)
  • 1-y = 0
    2- while (y <=5):
    3- grade = input("Enter Your Grade For Each Class Listed in Order (Letter Form): ")
    4- grade = grade.upper()
    5- grades.append(grade)
    6- y = y + 1
    7- calculate()
    1+total = 0
    88
    9-def calculate():
    10- total= 0
    11- for element in grades:
    12- if element == "A+":
    13- total = total + 4.0
    14- elif element == "A":
    30- elif element == "D":
    31- total = total + 1.0
    32- gpa = total / 6
    33- print(gpa)
    3+grade_dictionary = {
    4+ "A+":4.0,
    5+ "A":4.0,
    6+ "A-":3.7,
    7+ "B+":3.3,
    8+ "B":3.0,
    9+ "B-":2.7,
    10+ "C+":2.3,
    11+ "C":2.0,
    12+ "C-": 1.7,
    13+ "D": 1.0,
    14+ }
    3434
    35-
    36-collect()
Mathematics
Algorithms
Numbers
Code
Diff
  • const average = a => a.length ? a.reduce((acc, cur) => {return acc + cur/a.length}, 0): a[0];
  • 1-const average = a => a.length ? require("lodash").mean(a) : void 0;
    1+const average = a => a.length ? a.reduce((acc, cur) => {return acc + cur/a.length}, 0): a[0];
Code
Diff
  • module Example where
    
    isPoly :: Integer -> Bool
    isPoly = fold ( \ n z -> n `mod` len n == 0 && z ) True
    
    fold :: (Integer -> z -> z) -> z -> Integer -> z
    fold _ z 0 = z
    fold fn z n = n `fn` fold fn z (n `div` 10)
    -- Foldable doesn't allow an (Eq Integer,Integral Integer) constraint -- otherwise, this is functionally identical to foldr
    
    len :: Integer -> Integer
    len = fold ( \ _ c -> c+1 ) 0
    -- Foldable uses foldl' -- this is, m.m., otherwise verbatim
  • 11 module Example where
    22
    33 isPoly :: Integer -> Bool
    4-isPoly 0 = True
    5-isPoly n = n `mod` len n == 0 && isPoly (n `div` 10)
    4+isPoly = fold ( \ n z -> n `mod` len n == 0 && z ) True
    5+
    6+fold :: (Integer -> z -> z) -> z -> Integer -> z
    7+fold _ z 0 = z
    8+fold fn z n = n `fn` fold fn z (n `div` 10)
    9+-- Foldable doesn't allow an (Eq Integer,Integral Integer) constraint -- otherwise, this is functionally identical to foldr
    66
    77 len :: Integer -> Integer
    8-len 0 = 0
    9-len n = 1 + len (n `div` 10)
    12+len = fold ( \ _ c -> c+1 ) 0
    13+-- Foldable uses foldl' -- this is, m.m., otherwise verbatim

A short cut.

Code
Diff
  • class FirstClass {
      static byte sum(byte a, byte b) {
        return a += b;
      }
    }
    
  • 1-public class FirstClass {
    2- public static byte sum (byte a, byte b) {
    3- return (byte)(a + b);
    4- }
    1+class FirstClass {
    2+ static byte sum(byte a, byte b) {
    3+ return a += b;
    4+ }
    55 }

A bit ugly, but an efficient way of doing this :)

Complexity: O(n)

A few optimizations here:

  1. Ignores extra spaces between words;
  2. If the shortest word is found (a single character), stop searching;
Code
Diff
  • class Kata {
      static int findShort(String s) {
        int min = -1;
        for (int i = 0, l = 0; i < s.length(); i++, l++) {
          if (s.charAt(i) == ' ') {
            if (l > 0 && (min < 0 || l < min)) {
              if ((min = l) == 1) {
                break;
              }
            }
            l = -1;
          }
        }
        return min;
      }
    }
    
  • 1-import java.util.stream.*;
    2-public class Kata {
    3- public static int findShort(String s) {
    4- return Stream.of(s.split(" "))
    5- .mapToInt(String::length)
    6- .min()
    7- .getAsInt();
    1+class Kata {
    2+ static int findShort(String s) {
    3+ int min = -1;
    4+ for (int i = 0, l = 0; i < s.length(); i++, l++) {
    5+ if (s.charAt(i) == ' ') {
    6+ if (l > 0 && (min < 0 || l < min)) {
    7+ if ((min = l) == 1) {
    8+ break;
    9+ }
    10+ }
    11+ l = -1;
    12+ }
    88 }
    14+ return min;
    15+ }
    99 }
Code
Diff
  • getIDS <- function(string) {
      sum(as.integer(strsplit(string, "")[[1]]))
    }
    
  • 11 getIDS <- function(string) {
    2- sum(as.integer(unlist(strsplit(string, ""))))
    2+ sum(as.integer(strsplit(string, "")[[1]]))
    33 }