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.

Write a function that takes an integer array, and without sorting the array returns its largest and smallest number as a two element array.

INPUT:                             OUTPUT:
{-1,-2,-3,-4,-5,-6,-7,-8,-9,-1} -> {-9,-1}
public class HighLow{
  public static int[] printLargestAndSmallest(int[] nums){
		int low[] = nums;
		int high[] = nums;
		int lowest = nums[0];
		int highest = nums[0];
		int count = 0;
		
		for(int i = 0; i < nums.length;i+=2){
			int num1 = nums[i];
			int num2 = nums[i+1];
			
			if(num1<num2){
				low[count] = num1;
				high[count] = num2;
				count++;
			}else{
				low[count] = num2;
				high[count] = num1;
				count++;
			}
			
			lowest = low[0];
			highest = high[0];
			
			for(int j = 1; j < low.length; j++){
				if(low[j] < lowest){
					lowest = low[j];			
				}
			}
			
			for(int j = 1; j < high.length; j++){
				if(high[j] > highest){
					highest = high[j];
				}
			}

		}
    int[] finalHighLow = new int[2];
    finalHighLow[0] = lowest;
    finalHighLow[1] = highest;
    return finalHighLow;
	}
}
Strings
Algorithms

Write a function that takes a string as input and removes all variations of the phrase "yasss".

Variations of yass include a single y followed by any number of a's followed by any number of s.

Words are all separated by spaces, but may have punctuation at the end.

The function should return the string with each letter of the offending yass replaced with a '*'.


Some Examples:

yass -> ****
remove the yaaass -> remove the ******
yas? -> ***?
import java.util.regex.*;
public class YasRemover{
  public static String remove(String orig){
    String[] words = orig.split(" ");
    String output = "";
    for(int i=0;i<words.length;i++){
      boolean cut = false;
      String[] letters = words[i].split("");
      if(letters[0].equalsIgnoreCase("y") && letters[1].equalsIgnoreCase("a")){
        for(int j=1;j<letters.length;j++){
          if(letters[j].equalsIgnoreCase("a")){
          }else if(letters[j].equalsIgnoreCase("s") || letters[j].matches("[.!?,]")){
            if(j == letters.length-1){
              cut = true;
            }else{
              for(int x = j+1;x<letters.length;x++){
                if(letters[x].equalsIgnoreCase("s") || letters[x].matches("[.!?,]")){
                  cut = true;
                }else{
                  cut = false;
                }
              }
            }
          }else{
            cut = false;
          }
        }
      }
      if(cut){
        words[i] = words[i].replaceAll("[a-zA-Z]","*");
        cut = false;
      }
    }
    for(int i=0;i<words.length-1;i++){
      output += words[i] + " ";
    }output+= words[words.length-1];
    return output;
  }
}
Regular Expressions
Declarative Programming
Advanced Language Features
Fundamentals
Strings

replace seems like the perfect tool for the job in this case.

This version is in a one-liner format but could pretty easily be split on multiple lines for clarity.

There are also some potential very slight performance improvements as it does not need to create a temporary array in memory and the inner word/digit test only checks the first character (but this is likely irrelevant in most real-world use cases)

Code
Diff
  • let transformText = s => s.replace(/\D+|[\d.]+/g, m => /^\D/.test(m) ? (m = m.trim()) && `<span>${m}</span>` : `<div>${m}</div>`);
  • 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``;
    
    9
    }
    
    1+
    let transformText = s => s.replace(/\D+|[\d.]+/g, m => /^\D/.test(m) ? (m = m.trim()) && `<span>${m}</span>` : `<div>${m}</div>`);
    
Code
Diff
  • interface Calculator {
      static int calculate(int a, int b, char ops) {
        int[] calcs = {a * b, a + b, 0, a - b, 0, a / b};
        return calcs[ops - 42];
      }
    }
    
  • 11
    interface Calculator {
    
    2
      static int calculate(int a, int b, char operation) {
    
    3
        int result = 0;
    
    4
        switch(operation) {
    
    5
            case '+':
    
    6
            result = a + b; break;
    
    7
            case '-':
    
    8
            result = a - b; break;
    
    9
            case '*':
    
    10
            result = a * b; break;
    
    11
            default:
    
    12
            System.exit(0); break;
    
    13
        }
    
    14
        return result;
    
    2+
      static int calculate(int a, int b, char ops) {
    
    3+
        int[] calcs = {a * b, a + b, 0, a - b, 0, a / b};
    
    4+
        return calcs[ops - 42];
    
    1515
      }
    
    1616
    }
    

Recent Moves:

Code
Diff
  • find_max=max
  • 1
    def find_max(arr):
    
    2
        return max(arr)
    
    1+
    find_max=max
    

Recent Moves:

Numbers
Algorithms

It is bad practice to have different return types. Maybe instead you could return 0 if the input is invalid.

You may also want to specify whether floats should be converted too.

Code
Diff
  • def convert(number):
        return number*-1 if type(number) == int else 0
    
  • 11
    def convert(number):
    
    2
            if isinstance(number, int):
    
    3
                # Check if number is negitive
    
    4
                if number < 0:
    
    5
                    # negitive -> positive
    
    6
                    return (abs(number))
    
    7
    8
                else:
    
    9
                    # positive -> negitive
    
    10
                    return (number * -1)
    
    11
            
    
    12
            else:
    
    13
                return False
    
    2+
        return number*-1 if type(number) == int else 0
    

Recent Moves:

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