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.

Fundamentals
Arrays
Code
Diff
  • var sum=0;
    const getSum = (array) => {sum=0; array.every( Sum = (curvalue) => {sum+=curvalue; return true; }); return sum;}
  • 1-function getSum(array) {
    2- //your code
    3-}
    1+var sum=0;
    2+const getSum = (array) => {sum=0; array.every( Sum = (curvalue) => {sum+=curvalue; return true; }); return sum;}

Define a method/function that removes from a given array of integers all the values contained in a second array.

Examples:


int[] integerList = new int[]{ 1, 2, 2, 3, 1, 2, 3, 4 }
int[] valuesList = new int[]{ 1, 3 }
Kata.Remove(integerList, valuesList) == new int[]{ 2, 2, 4 } // --> true
int[] integerList = new int[]{ 1, 1, 2, 3, 1, 2, 3, 4, 4, 3 ,5, 6, 7, 2, 8 }
int[] valuesList = new int[]{ 1, 3, 4, 2 }
Kata.Remove(integerList, valuesList) == new int[]{ 5, 6 ,7 ,8 } // --> true
int[] integerList = new int[]{ 8, 2, 7, 2, 3, 4, 6, 5, 4, 4, 1, 2 , 3 }
int[] valuesList = new int[]{ 2, 4, 3 }
Kata.Remove(integerList, valuesList) == new int[]{ 8, 7, 6, 5, 1 } // --> true
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Kata
{
public static int[] Remove(int[] integerList,int[] valuesList)
	{
		if (integerList==null||integerList.length<1) return new int[0];
		if (valuesList==null||valuesList.length<1) return integerList;
		List<Integer> list= Arrays.stream(valuesList).boxed().collect(Collectors.toList());
		return Arrays.stream(integerList).filter(i->!list.contains(i)).toArray();
	} 
}
Theoretical Computer Science
Mathematics
Algorithms
Numbers

It is not uncommon to see a middle schooler incorrectly simplify the expression (a + b)^2 to a^2 + b^2 in his/her algebra assignments. While the two expressions may be equivalent for particular values of a and b (e.g. (0 + 0)^2 = 0^2 = 0 + 0^2 = 0^2 + 0^2), it is not true in general. So, how can we convince said middle-schooler that (a + b)^2 is not equal to a^2 + b^2 in general? This is equivalent to saying:

-- N.B. The domain of a, b is the natural numbers for
-- sake of simplicity
exists a b. (a + b)^2 /= a^2 + b^2

The general method for proving statements involving existential quantifiers is to provide a specific value or set of values such that the statement to be proven holds true - such value(s) are called witnesses. So, for example, we could provide the witnesses a = 1 and b = 1 in our case which reduces the statement to:

(1 + 1)^2 /= 1^2 + 1^2

And, of course, evaluating both sides of our equation gives 4 /= 2 which trivially holds.

Unfortunately, there is no direct way to encode existential quantifiers (and inequality) as a Haskell type. So what we could do is transform our original statement using known logical equivalences. The first thing to notice is that a /= b is defined as not (a = b) for any a, b:

exists a b. not ((a + b)^2 = a^2 + b^2)

Then, de Morgan's law for quantifiers tells us that exists a. not b is logically equivalent to not (forall a. b) for any b (which may or may not be related to a). Finally, to prove that not (forall a. b), it suffices to prove that asserting forall a. b leads to a contradiction (Void in Haskell). So our final type (minus a bit of required Haskell boilerplate code) is:

(forall a b. (a + b)^2 = a^2 + b^2) -> Void

See the code provided for the exact type signature of our proof and additional clarifications/explanations.

{-# LANGUAGE
  KindSignatures,
  GADTs,
  TypeOperators,
  TypeFamilies,
  UndecidableInstances,
  RankNTypes,
  EmptyCase #-}

module TheMiddleSchoolersMistake where

-- Void - the type representing contradictions in Haskell
import Data.Void

-- Peano's axioms for the natural numbers
data Z
data S n

data Natural :: * -> * where
  NumZ :: Natural Z
  NumS :: Natural n -> Natural (S n)

-- Axiom of reflexivity of equality: The very notion
-- of equality itself mandates that everything be
-- considered equal to itself
data (:=:) :: * -> * -> * where
  Refl :: a -> a :=: a

-- Peano addition, multiplication and exponentation
type family (:+:) (n :: *) (m :: *) :: *
type instance Z :+: n = n
type instance S n :+: m = S (n :+: m)

type family (:*:) (n :: *) (m :: *) :: *
type instance Z :*: n = Z
type instance S n :*: m = m :+: (n :*: m)

type family (:^:) (n :: *) (m :: *) :: *
type instance n :^: Z = S Z
type instance n :^: S m = n :*: (n :^: m)

-- Useful type synonyms for readability
type N1 = S Z -- The natural number 1
type N2 = S N1 -- The natural number 2
type N4 = N2 :+: N2 -- The natural number 4

-- Our proof that (a + b)^2 /= a^2 + b^2 in general
proof ::
  (forall n m.
    Natural n ->
    Natural m ->
    ((n :+: m) :^: N2) :=: ((n :^: N2) :+: (m :^: N2))) -> Void
proof mistake = let
  -- 1 is a natural number
  one :: Natural N1
  one = NumS NumZ
  -- To disprove (a + b)^2 = a^2 + b^2, we provide witnesses
  -- a = 1, b = 1 to our counterexample.  A witness is a
  -- value demonstrating the correctness of a logical statement
  -- involving existential quantifiers
  -- N.B. The witnesses could be anything else as long as it
  -- proves our point.  For example, we could've chosen a = 2,
  -- b = 3 instead and show that (2 + 3)^2 = 5^2 = 25 /= 13 =
  -- 2^2 + 3^2
  erroneous :: N4 :=: N2
  erroneous = mistake one one in
    -- Now we consider all possible cases where 4 = 2 and
    -- describe how to construct a proof of Void in each case
    -- However, there are no constructors that would allow us
    -- to construct a value of such a type!  The only possible
    -- constructor we could consider is Refl, but Refl always
    -- constructs values of a :=: a, never a :=: b where a and
    -- b are distinct types.  Therefore, we have already
    -- considered all (0) possible cases of 4 = 2 and we are
    -- done here.
    case erroneous of {}
Theoretical Computer Science
Mathematics
Algorithms
Numbers

In international chess, a knight is a chess piece which can move in an L-shape (possibly mirrored) of size 2x1 in any orientation. Due to its unique movement patterns, it is the only chess piece able to "jump" past chess pieces of any color that may be blocking its way.

However, what is more interesting is that its movement patterns allow it to move to any desired square on a given chessboard provided that no same-color chess piece is occupying that desired square. This Haskell program seeks to prove this statement, extending the chessboard to an infinite chessboard (in the rightwards and upwards directions) characterized by an ordered tuple of natural numbers.

{-# LANGUAGE KindSignatures, GADTs #-}

module KnightsOnAChessboard where

-- Peano's axioms for the natural numbers
data Z
data S n

-- Definition of natural numbers (to prevent nonsensical types like `S Bool`)
data Natural :: * -> * where
  NumZ :: Natural Z
  NumS :: Natural n -> Natural (S n)

-- Legal moves of a knight in international chess
data KnightCanReach :: * -> * where
  NoMoves :: KnightCanReach (Z, Z)
  MoveRL :: KnightCanReach (n, m) -> KnightCanReach (S (S n), S m)
  MoveRR :: KnightCanReach (n, S m) -> KnightCanReach (S (S n), m)
  MoveUL :: KnightCanReach (S n, m) -> KnightCanReach (n, S (S m))
  MoveUR :: KnightCanReach (n, m) -> KnightCanReach (S n, S (S m))
  MoveLL :: KnightCanReach (S (S n), S m) -> KnightCanReach (n, m)
  MoveLR :: KnightCanReach (S (S n), m) -> KnightCanReach (n, S m)
  MoveDL :: KnightCanReach (n, S (S m)) -> KnightCanReach (S n, m)
  MoveDR :: KnightCanReach (S n, S (S m)) -> KnightCanReach (n, m)

-- Proof that knight can reach any square on a chessboard starting from (0, 0)
knightCanMoveAnywhere :: Natural n -> Natural m -> KnightCanReach (n, m)
knightCanMoveAnywhere NumZ NumZ = NoMoves
knightCanMoveAnywhere NumZ (NumS n) =
  MoveDR $ MoveUL $ MoveRL $ knightCanMoveAnywhere NumZ n
knightCanMoveAnywhere (NumS n) m =
  MoveLL $ MoveRR $ MoveUR $ knightCanMoveAnywhere n m
Code
Diff
  • function a(){
      // We shall remove the following solution when publish as KATA
      static $poi = null;
      if ($poi) return $poi->create();
      $poi = new class {
         public function create() {
            return new static();
         }
      };
      return $poi->create();
    }
  • 11 function a(){
    2+ // We shall remove the following solution when publish as KATA
    22 static $poi = null;
    33 if ($poi) return $poi->create();
    44 $poi = new class {
    55 public function create() {
    66 return new static();
    77 }
    88 };
    99 return $poi->create();
    1010 }

Simpler ternary way: if a and b really are equal it doesn't matter which one gets returned

Code
Diff
  • public class BiggerNum{
    
      /**
       * @param a integer of param1
       * @param b integer of param2
       * @return the bigger integer of a and b
       * If a equals b, return either one
       */
      public static int compare(int a, int b) {
        return a > b ? a : b;
      }
    }
  • 11 public class BiggerNum{
    22
    33 /**
    44 * @param a integer of param1
    55 * @param b integer of param2
    66 * @return the bigger integer of a and b
    77 * If a equals b, return either one
    88 */
    99 public static int compare(int a, int b) {
    10- return a > b ? a : a < b ? b : a;
    10+ return a > b ? a : b;
    1111 }
    1212 }
Bugs

One line version with error output as lambda (analog of arrow function in Python) looks like this.

Code
Diff
  • div = lambda num1, num2 : print("Division failed: division by zero") if num2 == 0 else print("Division failed: integers and floats accepted only") if not(isinstance(num1, (int, float)) and isinstance(num2, (int, float))) else num1 / num2
    
  • 1-let divide = (num1, num2) => num2 === 0 ? console.log("Division failed: division by zero") : !(num1 === +num1 && num2 === +num2) ? console.log("Division failed: integers and floats accepted only") : num1/num2
    1+div = lambda num1, num2 : print("Division failed: division by zero") if num2 == 0 else print("Division failed: integers and floats accepted only") if not(isinstance(num1, (int, float)) and isinstance(num2, (int, float))) else num1 / num2