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
class Ivan
  def give_me_beer?
    true
  end
end
class Andy < Ivan
end

class Vasa < Andy
end

class Kolya < Vasa
end

A gozinta chain for n is a sequence {1,a,b,...,n} where each element properly divides the next.

There are eight gozinta chains for 12:
{1,12} ,{1,2,12}, {1,2,4,12}, {1,2,6,12}, {1,3,12}, {1,3,6,12}, {1,4,12} and {1,6,12}.

Let g(n) be the number of gozinta chains for n, so g(12)=8.
g(48)=48 and g(120)=132.

Given n, return the number of gozinta chains for g(n).

(Adapted from Project Euler, problem #548)

const g = (n) => {
  return "Good luck!"
}

Squaring a number is multiplying it by itself . in this kata We Will not use the multiplication Operator or Even the Built-in POw Function .
sample input_output ::

3 >- 9
4 >- 16
5 >- 25

hint :
observe this kata Tags (Reveal EverThing ) !!

int sqauring (int num)
{
int times ; 
 times = num ;
 int squared ; 
 squared = 0;
 
 for (int i = 0 ; i <times ; i++ )
 {
 squared = squared + num ; 
 }
 
return squared ;
}

https://en.wikipedia.org/wiki/Quaternion

Quaternions are a number system that extends complex numbers. Like complex numbers, it has one real component, but quaternions have three imaginary components instead of just one. It retains many operations and properties of complex numbers, such as addition, subtraction and multiplication. However, due to its complexity, it has fewer properties than complex numbers, the most notable one being that multiplication is no longer commutative, i.e. given two quaternions p and q, pq /= qp in most cases. Quaternions form an algebraically closed system like the complex numbers, i.e. given any polynomial with quaternion coefficients, it is guaranteed that all roots of the polynomial will also be quaternions. Quaternions find a wide range of applications in other branches of mathematics, such as 3D rotations (in place of 3 by 3 matrices), and was even widely used in 3D geometry before the "invention" of the concept of the modern 3D vector (which provided simpler calculations for certain types of problems).

This Kumite contains a rudimentary definition/implementation of quaternions in Haskell with only a small fraction of possible operations and/or functions defined because I don't want to spoil a potential future Kata on quaternions (which I plan to author in Fortran once it arrives on Codewars, among other languages). Stay tuned ;)


Note to Haskell experts/mathematicians: I originally intended to create two constructors for the Quaternion data type, one initialized with 4 real numbers and one with 2 complex numbers (in agreement with the Cayley-Dickson construction of quaternions from complex numbers), but couldn't quite find a way to make all the operators work without defining an extra equation for each one (which doesn't feel pure but instead rather hacky). So feel free to remix this Kumite and add this functionality if you're feeling up for it, cheers :D

module Quaternions where

import Test.Hspec

data Quaternion a = Quaternion a a a a
  deriving (Eq, Show)

instance RealFloat a => Num (Quaternion a) where
  (Quaternion a1 b1 c1 d1) + (Quaternion a2 b2 c2 d2) = Quaternion (a1 + a2) (b1 + b2) (c1 + c2) (d1 + d2)
  (*) = error "Quaternion multiplication is not implemented here to minimize spoilers for my potential future Kata"
  abs (Quaternion a b c d) = Quaternion (sqrt $ a ^ 2 + b ^ 2 + c ^ 2 + d ^ 2) 0.0 0.0 0.0
  signum = error "Not implemented in this Kumite"
  fromInteger n = Quaternion (fromInteger n) 0.0 0.0 0.0
  negate (Quaternion a b c d) = Quaternion (negate a) (negate b) (negate c) (negate d)

instance RealFloat a => Fractional (Quaternion a) where
  (/) = error "General division is not well-defined for quaternions - please specify whether left division or right division is desired"
  recip = error "Not implemented in this Kumite to minimize spoilers"
  fromRational x = Quaternion (fromRational x) 0.0 0.0 0.0

infix 7 `ldiv`
infix 7 `rdiv`

ldiv, rdiv :: Quaternion a -> Quaternion a -> Quaternion a
ldiv = error "Left division of a quaternion: not implemented in this Kumite to minimize spoilers"
rdiv = error "Right division of a quaternion: not implemented in this Kumite to minimize spoilers"

main = hspec $ do
  describe "Quaternions" $ do
    it "should be able to be compared for equality" $ do
      Quaternion 1.0 2.0 4.0 3.0 `shouldBe` Quaternion 1.0 2.0 4.0 3.0
    it "should be able to be compared for inequality" $ do
      Quaternion 1.0 2.0 4.0 3.0 `shouldNotBe` Quaternion 2.0 1.0 4.0 3.0
    it "should support addition (of two quaternions)" $ do
      Quaternion 3.0 5.0 2.3 9.9 + Quaternion 4.3 5.5 17.3 1.2 `shouldBe` Quaternion 7.3 10.5 19.6 11.1
    it "should support finding the absolute value (of a quaternion)" $ do
      abs (Quaternion 3.0 4.0 12.0 0.0) `shouldBe` 13.0
    it "should support the construction of a quaternion from an integer" $ do
      fromInteger 5 `shouldBe` Quaternion 5.0 0.0 0.0 0.0
    it "should support negation (i.e. finding the additive inverse)" $ do
      negate (Quaternion 234.4432 43.5 (negate 5.0) (negate 55.5)) `shouldBe` Quaternion (negate 234.4432) (negate 43.5) 5.0 55.5
    it "should support the construction of a quaternion from a rational" $ do
      fromRational (negate 44235.948372) `shouldBe` Quaternion (negate 44235.948372) 0.0 0.0 0.0
Strings
Data Types
Arrays

Not really useful, but calling replace with an array as replacement calls toString on it. It doesn't work the same using objects

const weird = 'asdf'.replace('a', [])
const weirdness = 'asdf'.replace('a', [1,2])
const volatile = 'asdf'.replace('a', {})

Just a simple demonstration of how the contents of std::cout can be tested in a Kata by redirecting the stream to file I/O operations.

Credits and recommended resources:

#include <iostream>

void myFirstHelloWorld() {
  std::cout << "Goodbye World!" << std::endl;
}

Although C++ templates can be a right pain in the arse when you're trying to model Peano numbers and their operations, they can come in handy when you want to define generic functions that work over a wide range of datatypes, especially higher-order functions that map a vector, for example.

using namespace std;

template<class T, class U> vector<U> vectorMap(vector<T> v, function<U (T)> f) {
  size_t size = v.size();
  vector<U> result (size);
  for (size_t i = 0; i < size; i++) result[i] = f(v[i]);
  return result;
}
template<class T> vector<T> vectorFilter(vector<T> v, function<bool (T)> f) {
  vector<T> result;
  for (size_t i = 0; i < v.size(); i++) if (f(v[i])) result.push_back(v[i]);
  return result;
}
template<class T, class U> U vectorReduce(vector<T> v, function<U (U, T)> f, U u) {
  U result = u;
  for (size_t i = 0; i < v.size(); i++) result = f(result, v[i]);
  return result;
}
Algorithms
Logic

What is the easiest way to check if all elements are equal in a Haskell list? Perhaps something that is safer than using head or tail?

For instance:

allEqual [1,2,3,4] = False 
allEqual [1,1,1,1] = True 
module AllEqual where

allEqual :: [Int] -> Bool 
allEqual xs = and $ map (== head xs) (tail xs)

I'm trying to put together my first Kata, but don't want to rush things if there's anything faulty about my code.

You are given two strings and must determine if they both follow the same pattern.

Example

"101 001" follows the same pattern as "aba aab"

"a" follows the same pattern as "z"

"TzTz" follows the same pattern as "1d1d"

"89az" follows the same pattern as "0iuf"

"877" does not follow the same pattern as "aba"

Responses are expected to be Boolean (True or False).

def pattern(p,st):
    def encode(s):
        dic={} # dictionary to hold values from the input string 
        num=0 
        sn="" # a numerical string to hold the encoding
        for i in s:
            if i not in dic:
                dic[i]=num
                num+=1
            sn+=str(dic[i])
        return sn 
    return encode(st)==encode(p)

Just confirming that the Criterion testing framework is not supported in Objective-C in Codewars.

int add(int a, int b) {
  return a + b;
}