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.
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)
Test.describe("Simple test")
test.assert_equals(number_to_english(0),'zero')
test.assert_equals(number_to_english(9),'nine')
test.assert_equals(number_to_english(11),'eleven')
test.assert_equals(number_to_english(20),'twenty')
test.assert_equals(number_to_english(100),'one hundred')
Test.describe("Not valid case")
test.assert_equals(number_to_english(-1),'')
test.assert_equals(number_to_english(9999999),'')
Make hahahah progamm
Improve it for different x inputs.
def ha x
"Ha" + "-ha" * (x - 1)
end
describe "Solution" do
it "should test for something" do
Test.assert_equals(ha(1), "Ha")
Test.assert_equals(ha(2), "Ha-ha")
end
end
Need to find amount of natural number that can be generated from an array of digit characters.
- Answers is one number: total amount of natural numbers can be generated by moving elements of the array.
- Each number have N digits, where N is an Array size.
- 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
describe "Solution" do
it "should test for something" do
Test.assert_equals(g(['9','0']), 1)
Test.assert_equals(g(['0','9','0']), 1)
Test.assert_equals(g(['5','7','2']), 6) # ans = !3
Test.assert_equals(g(['5','7','2','4']), 24) # ans = !4
Test.assert_equals(g(['1','3','4','0']), 18)
end
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
describe "Solution" do
it "should test for something" do
Test.assert_equals(enumer.take(4), [1, 2, 4, 8])
Test.assert_equals(simple(4), [1, 2, 4, 8])
end
end
class Ivan
def give_me_beer?
true
end
end
class Andy < Ivan
end
class Vasa < Andy
end
class Kolya < Vasa
end
describe "Solution" do
it "should test for something" do
Test.assert_equals(Kolya.new.give_me_beer?, true)
end
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!"
}
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
describe("Gozinta-fy", function(){
it("should solve the test problems", function(){
Test.assertEquals(g(12), 8);
});
it("should solve the test problems", function(){
Test.assertEquals(g(48), 48);
});
it("should solve the test problems", function(){
Test.assertEquals(g(120), 132);
});
});
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 ;
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(any_group_name_you_want)
{
It(should_do_something)
{
Assert::That(3, Equals(9));
Assert::That (4, Equals(16));
Assert::That(5, Equals(25));
}
};
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
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', {})
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals(weird, 'sdf');
Test.assertEquals(weirdness, '1,2sdf');
Test.assertEquals(volatile, '[object Object]sdf');
});
});