Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad
add(A, B, R) :- R is A + B.
import java.util.stream.*; public class Kata { public static int findShort(String s) { return Stream.of(s.split(" ")) .mapToInt(String::length) .min() .getAsInt(); } }
public class AbbreviateTwoWords {

  public static String abbrevName(String name) {
    String[] names = name.split(" ");
    return (names[0].charAt(0) + "." + names[1].charAt(0)).toUpperCase();
  }
}
public class FirstClass {
    public static byte sum (byte a, byte b) {
        byte c = (byte) (a + b);
        return c;
    }
}

This is what I have for ROT13.

Make it better without using codecs and one line at the same time!

def rot13(s):
    return rot.encode(s,"rot_13")

wrvcqwre

using System;


namespace Interpreter.Cata
{
    public static class FunctionalExtensions
    {

        public static Reader<Env, T1> App<Env, T, T1>(this Reader<Env, Func<T, T1>> @this, Reader<Env, T> fa)
            => new Reader<Env, T1>(env => { return @this.Map(f => fa.Map(f).Run(env)).Run(env); });



        public static T1 Eval<T, T1>(this Expr<T> @this,
            (Func<T1, T1, T1> add, Func<T1, T1, T1> sub, Func<T, T1> val) algebra)
            =>
            @this.MatchWith(pattern: (
                   add: (x, y) => algebra.add(x.Eval(algebra), y.Eval(algebra)),
                   sub: (x, y) => algebra.sub(x.Eval(algebra), y.Eval(algebra)),
                   val: v => algebra.val(v)
            ));

    }
    public abstract class Expr<T>
    {
        public abstract T1 MatchWith<T1>((Func<Expr<T>, Expr<T>, T1> add, Func<Expr<T>, Expr<T>, T1> sub, Func<T, T1> val) pattern);
    }
    public class ValExpr<T> : Expr<T>
    {
        public ValExpr(T x) { X = x; }
        public T X { get; }
        public override T1 MatchWith<T1>((Func<Expr<T>, Expr<T>, T1> add, Func<Expr<T>, Expr<T>, T1> sub, Func<T, T1> val) pattern)
                 => pattern.val(X);
    }
    public class AddExpr<T> : Expr<T>
    {
        public AddExpr(Expr<T> x, Expr<T> y) { X = x; Y = y; }

        public Expr<T> X { get; }
        public Expr<T> Y { get; }

        public override T1 MatchWith<T1>(
            (Func<Expr<T>, Expr<T>, T1> add,
            Func<Expr<T>, Expr<T>, T1> sub,
            Func<T, T1> val) pattern)
             => pattern.add(X, Y);
    }
    public class SubExpr<T> : Expr<T>
    {
        public SubExpr(Expr<T> x, Expr<T> y) { X = x; Y = y; }

        public Expr<T> X { get; }
        public Expr<T> Y { get; }

        public override T1 MatchWith<T1>((Func<Expr<T>, Expr<T>, T1> add,
             Func<Expr<T>, Expr<T>, T1> sub,
            Func<T, T1> val) pattern)
             => pattern.sub(X, Y);
    }
    public class Reader<Env, T>
    {
        public Func<Env, T> Fn { get; set; }
        public Reader(Func<Env, T> fn) => Fn = fn;
        public Reader<Env, T1> Map<T1>(Func<T, T1> f) => new Reader<Env, T1>((env) => f(Fn(env)));
        public T Run(Env env) => Fn(env);
    }
    public class Config
    {
        public string Name { get; set; }
    }

    public class Demo
    {
        public static void Run()
        {
            var expression = new SubExpr<int>(new AddExpr<int>(new ValExpr<int>(2), new ValExpr<int>(2)), new ValExpr<int>(2));

            var result = expression.Eval(algebra: (
                    add: (x, y) => x + y,
                    sub: (x, y) => x + y,
                    val: v => v)
                );

        }
    }



}

Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are considered here as Meta strings.

func areMetaStrings(_ str1: String, _ str2: String) -> Bool {
    return str1.lowercased().sorted() == str2.lowercased().sorted()
}
even(N) :- 0 is mod(N, 2).

This is a super-ineffective prime number finder. Can you make it better?

def prime(x):
    counter = 0
    for i in range(1,x):
        if x % i is 0:
            counter += 1
    if counter is 0:
        return True
    else:
        return False
Algorithms
Logic
Formats
Strings
Data Types
Formatting

I just did the 4 kyu Human readable duration format Kata, and Although I managed to answer correctly, I feel like my code is unpolished.

I want to see the best approaches to this, and if possible, the explanation of what I did wrong.

Thanks.

RULES (taken straight out of the page):
Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

For the purpose of this Kata, a year is 365 days and a day is 24 hours.

Note that spaces are important.

Detailed rules
The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.

The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.

A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.

public class TimeFormatter {
    
  private final static int YEAROFSECONDS = 31536000;
	private final static int DAYOFSECONDS = 86400;
	private final static int HOUROFSECONDS = 3600;
	private final static int MINUTEOFSECONDS = 60;

	public static String formatDuration(int seconds) {
		int[] counter = new int[5];
		int componentCounter = 0;
		StringBuilder sb = new StringBuilder();
		while(seconds / YEAROFSECONDS >= 1) {
			counter[0]++;
			seconds = seconds - YEAROFSECONDS;
		}
		while(seconds / DAYOFSECONDS >= 1) {
			counter[1]++;
			seconds = seconds - DAYOFSECONDS;
		}
		while(seconds / HOUROFSECONDS >= 1) {
			counter[2]++;
			seconds = seconds - HOUROFSECONDS;
		}
		while(seconds / MINUTEOFSECONDS >= 1) {
			counter[3]++;
			seconds = seconds - MINUTEOFSECONDS;
		}
		counter[4] = seconds;
		if(counter[0] == 0 && counter[1] == 0 && counter[2] == 0 && counter[3] == 0 && counter[4] == 0) {
			return "now";
		} else {
			for(int i:counter) {
				if(i != 0) {
					componentCounter++;
				}
			}
			int index = 0;
			String toAdd = "error";
			for(int i:counter) {
				if(i != 0) {
					switch(index) {
					case 0:
						if(i == 1) {
							toAdd = "year";
						}  else {
							toAdd = "years";
						}
						index++;
						componentCounter--;
						break;
					case 1:
						if(i == 1) {
							toAdd = "day";
						}  else {
							toAdd = "days";
						}
						index++;
						componentCounter--;
						break;
					case 2:
						if(i == 1) {
							toAdd = "hour";
						}  else {
							toAdd = "hours";
						}
						index++;
						componentCounter--;
						break;
					case 3:
						if(i == 1) {
							toAdd = "minute";
						}  else {
							toAdd = "minutes";
						}
						index++;
						componentCounter--;
						break;
					case 4:
						if(i == 1) {
							toAdd = "second";
						}  else {
							toAdd = "seconds";
						}
						index++;
						componentCounter--;
						break;
					}
				if(componentCounter == 1) {
					sb.append(i);
					sb.append(" ");
					sb.append(toAdd);
					sb.append(" and ");
				} else {
					sb.append(i);
					sb.append(" ");
					sb.append(toAdd);
          if(componentCounter !=0){
					sb.append(", ");
          }
					
				}
					
				} else {
					index++;
				}
			}
			return sb.toString();
		}
	}

}