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

Try to make match in ruby to work like js mathc.

def slot s
  p "ONE scan"
  p "input: %s" % s
  p "output " + s.scan(/!+|\?+/).inspect
  
  p "TWO scan"
  p "input: %s" % s
  p "output " + s.scan(/([?!])\1*/).inspect
  
  p "ONE match"
  p "input: %s" % s
  p "match"
  s.match(/!+|\?+/).to_a.each{|x| p x}
  p "TWO  match"
  p "input: %s" % s
  p "match"
  s.match(/([?!])\1*/).to_a.each{|x| p x}
end

slot("!!!??")

Given a number between 0-99999, the function number_to_english, return the same number pass to argument in letters.

For example:

if pass 9 with argument of number_to_english this return nine

The task is very simple this code is very ugly, and need to refactor it.

def mil(n):
    num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
    dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}
    dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}
    mil="thousand"
    th=[]
    n_t=[x for x in n]
    for i in n_t:
        if len(n_t)==2:
            if i!='1' and n_t[1]=='0':
                th.append(dec_com[i+'0'])
                th.append(mil)
                break
            elif i=='1':
                th.append(dec[i+n_t[1]])
                th.append(mil)
                break
            else:
                th.append(dec_com[i+'0'])
                th.append(num[n_t[1]])
                th.append(mil)
                break
        else:
            th.append(num[i])
            th.append(mil)
    return th
def cen(n):
    num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
    dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}
    dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}
    cen="hundred"
    c=[]
    n_d=[x for x in n]
    for m in n_d:
        if n_d[0]!='0':
            c.append(num[m])
            c.append(cen)
            if n_d[1]=='0' and n_d[2]=='0':
                break
            elif n_d[1]=='0' and n_d[2]!='0':
                c.append(num[n_d[2]])
                break
            elif n_d[1]!='1' and n_d[2]=='0':
                c.append(dec_com[n_d[1]+'0'])
                break
            elif n_d[1]=='1':
                c.append(dec[n_d[1]+n_d[2]])
                break
            else:
                c.append(dec_com[n_d[1]+'0'])
                c.append(num[n_d[2]])
                break
        else:
            if n_d[1]=='0' and n_d[2]=='0':
                break
            elif n_d[1]=='0' and n_d[2]!='0':
                c.append(num[n_d[2]])
                break
            elif n_d[1]!='1' and n_d[2]=='0':
                c.append(dec_com[n_d[1]+'0'])
                break
            elif n_d[1]!='1' and n_d[2]!='0':
                c.append(dec_com[n_d[1]+'0'])
                c.append(num[n_d[2]])
                break
            elif n_d[1]=='1':
                c.append(dec[n_d[1]+n_d[2]])
                break
            
    return c
def number_to_english(n):
    num={0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
    dec={10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen'}
    dec_com={20:'twenty',30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety'}
    th=[]
    c=[]
    m='{0:,}'.format(n)
    m=m.split(",")
    try:
        if n<0 or type(n)==float or n>99999:
            pass
        elif n<10:
            c.append(num[n])
        elif n<20:
            c.append(dec[n])
        elif n%10==0 and n<99:
            c.append(dec_com[n])
        elif n<99:
            k=list(str(n))
            c.append(dec_com[int(k[0]+'0')])
            c.append(num[int(k[1])])
        else:
            c=cen(m[1])
            th=mil(m[0])
    except IndexError:
        if n<0 or type(n)==float or n>99999:
            pass
        elif n<10:
            c.append(num[n])
        elif n<20:
            c.append(dec[n])
        elif n%10==0 and n<99:
            c.append(dec_com[n])
        elif n<99:
            k=list(str(n))
            c.append(dec_com[int(k[0]+'0')])
            c.append(num[int(k[1])])
        else:
            c=cen(m[0])
    t=[]
    t.extend(th)
    t.extend(c)
    return " ".join(t)

Make hahahah progamm

Improve it for different x inputs.

def ha x
  "Ha" + "-ha" * (x - 1)
end

Need to find amount of natural number that can be generated from an array of digit characters.

  1. Answers is one number: total amount of natural numbers can be generated by moving elements of the array.
  2. Each number have N digits, where N is an Array size.
  3. Each number use each charachter from an Array oney once.

This code needs optimization.

P.S. Question from: https://stackoverflow.com/questions/47616564/find-amount-of-natural-numbers-generated-from-array-of-digit-characters

def g(a)
  answer = a.permutation(a.size)
  .select{|x| x.join.to_i.to_s.split("").size == a.size }
  .to_a.uniq.size
  answer
end

How do loop in Enumerator.new knows when to stop? Take a look at simple method: in this method conditions used to prevent endless loop. But, in case Enumertor.new there is no break condtion.

def enumer
  Enumerator.new do |x|
      a = 1
      loop do # How do this loop know where to stop?
       x << a
       a *= 2
      end
  end
end

def simple n
  x  = []
  a = 1
  i = 0
  loop do
    x << a
    a *= 2
    i += 1
    break unless i < n # in this case condition for stop used
  end
  x
end
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', {})