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.
a = "Baguette baguette baguette Encore!"
b = "Baguette baguette baguette Encore!"
p a == b
p a === b
describe "Solution" do
it "should test for something" do
Test.assert_equals(a, b, "equal")
end
end
This my solusion pass the KATA, but it fails on
test generated by rolling 3 elements ASC and DESC arrays.
https://www.codewars.com/kata/simple-array-rotation/ruby
Generating tests:
"ROTATING [1, 10, 100]is ASC"
"rt: 0 ar: [1, 10, 100]is ASC a<b and b<c and a<c"
"rt: 1 ar: [10, 100, 1] ac and a>c"
"ROTATING [100, 10, 1]is DESC"
"rt: 0 ar: [100, 10, 1]is DESC a>b and b>c and a>c"
"rt: 1 ar: [10, 1, 100] a>b and b<c and a<c"
PS:
code for generate tests https://gist.github.com/lbvf50mobile/85f5abb840f3926feb80f7dfe85d2e01
def solve(arr)
return "A" if asc_? arr
return "D" if desc_? arr
if( arr.size == 3)
a = arr
return "RA" if a[0] > a[1] and a[1] < a[2];
end
ch_rd = arr.chunk_while{ |x,y| x > y }.to_a
ch_ra = arr.chunk_while{ |x,y| x < y }.to_a
return "RD" if ch_rd.any?{|x| desc_? x} and ch_rd.size == 2
return "RA" if ch_ra.any?{|x| asc_? x} and ch_ra.size == 2
end
def asc_? arr
arr == arr.sort
end
def desc_? arr
arr == arr.sort.reverse
end
describe "Simple array rotation" do
it "Uncoveder 3 tests" do
Test.assert_equals(solve([10, 100, 1]),"RA")
Test.assert_equals(solve([10, 1, 100]), "RD")
end
end
Compare JS and Ruby REEGEX
RUBY s.scan(/!+|\?+/).inspect
== JS s.match(/!+|\?+/g)
RUBY s.scan(/([?!])\1*/)
!= JS s.match(/([?!])\1*/g)
https://gist.github.com/lbvf50mobile/4b3cd312ad411e47582af40c7cbd4e05/edit
let slot = s => {
console.log("ONE")
console.log("input ", s)
console.log("output" ,s.match(/!+|\?+/g))
console.log("ONE")
console.log("input ", s)
console.log("output", s.match(/([?!])\1*/g))
}
slot("!!!??")
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