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
Theoretical Computer Science
Mathematics
Algorithms
Logic
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 {}

Update getIDS to get the iterated digit sum.

The iterated digit sum of 1563 e.g is 1 + 5 + 6 + 3 = 15.

function getIDS($number) {
  return array_sum(str_split($number));
}

Write a function which would take two figures as a parameters and generate List of numbers which would contains figures from 1 fo n (n - first input) and lenght of each number should be equals to n .

Then return number at m position ( m - second input).
If number at that position doesn't exist return -1;

Examples :

Generator(2 ,3) => 11 12 21 22

should return 21

Generator(3, 10) => 111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 232 233 311 312 313 321 322 323 331 332 333

should return 211

Good luck :)

using System;
using System.Linq;
using System.Collections.Generic;


public class NumbersFinder
{
    public static int Generator(int size , int position){
        if ( Math.Pow( size , size ) < position )
            return -1;
        List<string>  arr = new List<string>();
        for (int i = 0; i < size; ++i)
            arr.Add((i+1).ToString());
        List<string>  newArr;
        while (arr.Count != Math.Pow(size,size)){
            newArr = new List<string>();
            for (int j = 0; j < arr.Count; j++)
                for (int i = 0; i < size; i++)
                    newArr.Add(arr[j]+(i+1));
            arr.Clear();
            arr.AddRange(newArr);
        }
        return Convert.ToInt32(arr[position - 1]);
    }
}

Challenge

Implement a linked list function

const linkedList = {
  list: {},
  addNode: {}
}
const linkedList = {};

Validate a porperty in object

function validateProperty(objectForValidate, specificProperty) {
  if(objectForValidate[specificProperty]){
    return true;
  } else{
    return false;
  }
}

Validate a property in object

function validateProperty(objectForValidate, specificProperty) {
  if(objectForValidate[specificProperty]){
    return true;
  } else{
    return false;
  }
}

List of packages recently added

  • parsec, attoparsec, megaparsec
  • hspec-attoparsec, hspec-megaparsec
  • regex-pcre, regex-tdfa, regex-posix

List of packages tested here

  • parsec
  • regex-*
module Example where

import qualified Text.Regex.Posix as Posix
import qualified Text.Regex.PCRE as PCRE
import qualified Text.Regex.TDFA as TDFA

import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.String

-- The most basic functionalities of Regex modules

posixMatches :: String -> String -> Bool
posixMatches = (Posix.=~)

pcreMatches :: String -> String -> Bool
pcreMatches = (PCRE.=~)

tdfaMatches :: String -> String -> Bool
tdfaMatches = (TDFA.=~)

pcreVersion :: Maybe String
pcreVersion = PCRE.getVersion

-- The most basic functionalities of Parsec

number :: Parser Integer
number = (\a b -> read a) <$> many1 digit <*> eof

parseInt :: String -> Either ParseError Integer
parseInt = parse number ""

List of packages tested

  • megaparsec
  • hspec-megaparsec

If you can build a working attoparsec example, please post a kumite on it.

module Example where

import Text.Megaparsec
import Text.Megaparsec.Char

import Data.Void

type Parser = Parsec Void String

singleX :: Parser Char
singleX = char 'x'

PureScript is a strongly, statically typed functional programming language that compiles to and is easily interoperable with JavaScript. Its syntax and semantics are heavily influenced by Haskell so Haskell enthusiasts should find it easy to pick up PureScript.

Please don't forget to help PureScript leave Beta on Codewars and support the Codewars PureScript community by completing, authoring and translating more PureScript Kata. - @donaldsebleung

module WelcomePureScript (welcomeMsg) where

welcomeMsg :: String
welcomeMsg = "Welcome, PureScript!"