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.
const add = (a, b) => a + b + 1;
const { assert } = require("chai");
const fc = require('fast-check');
describe("Solution", () => {
it("should test for something", () => {
fc.assert(fc.property(fc.integer(1, 100), fc.integer(1, 100), (a, b) => {
assert.equal(add(a, b), a + b);
}));
});
});
Ваша задача - написать тело функции которая будет вычислять формулы.
Формула передаётся через параметр forlmula
. Формулы могут содержать в себе все значения, но также можно передать переменные вида $1
, $2
, $3
и т.д.
Значения переменных передаются вторым параметром в неограниченном кол-ве.
Посмотрите тесты что бы понять что от вас требуется.
Удачи! Она вам потребуется ;)
======
Your task is to write the body of a function that will calculate formulas.
The formula is passed through the forlmula
parameter. Formulas can contain all values, but you can also pass variables like $1
, $2
, $3
, etc.
Variable values are passed as the second parameter in an unlimited number.
Look at the tests to understand what is required of you.
Good luck! You will need it;)
using System;
public class Calculator
{
public static float Calculate(string forlmula, params float[] values)
{
// your code
}
}
using System;
using NUnit.Framework;
[TestFixture]
public class KataTests {
[Test]
public static void MyTest()
{
Assert.AreEqual(10, Calculator.Calculate("(2 + 3) * 4 / 2"));
Assert.AreEqual(25, Calculator.Calculate("50 - 10 * 2.5"));
Assert.AreEqual(3, Calculator.Calculate("2 ^ 3 - 1 * 5"));
Assert.AreEqual(5, Calculator.Calculate("2 + |-(-20 + 50)| / (5 * 2)"));
Assert.AreEqual(40, Calculator.Calculate("$1 * $2 - $1", 5, 9));
}
}
module Kata where
add_ :: Int -> Int -> Int
add_ a b = a + b
module ExampleSpec where
import Test.Hspec
import Test.QuickCheck
import Data.Foldable (for_)
import Kata
spec :: Spec
spec = do
describe "test" $ do
it "test" $ do
for_ tests $ \ (a, b) -> shouldBe a b
shouldBe 1 1
tests = [(add_ 1 2, 3), (add_ 10 20, 30), (add_ 30 40, 70)]
I would love to see a generator which accepts [(Gen s, Int)] and uses a set of generators, generates n inputs from each of them, shuffles them, and uses the inputs to feed test cases.
For example, for "Is a number prime?" kata, I'd like to have a composite generator built like compositeGen([(genPrimes, 100), (genOddComposites, 100), (genNegativePrime, 10), (genSquareOfAPrime, 20)]) or something like this, and it would run test cases over 230 shuffled inputs.
Bonus points if such generator were able to generate not only Int, but also (Int, Bool) (input + expected answer) or even (Int, Bool, String) (input, expected answer, and assertion message).
The one thing I can't fix is that if it fails, it's always after 1 test. Because that's what it sees.
module Example (isPrime) where
isPrime :: Int -> Bool
isPrime = odd
module ExampleSpec (spec) where
import Example (isPrime)
import Test.Hspec hiding (shouldBe)
import Test.QuickCheck
import Data.Foldable (for_)
import Control.Monad (unless,replicateM)
import Data.List ((\\))
import Data.Traversable (for,mapM)
type Input = Int
type Actual = Bool
type Expected = Actual -- necessarily
type Message = String
solution :: Input -> Actual
solution = isPrime
primes,composites :: [Input]
primes = [2,3,5,7,11,13,17,19]
composites = [1..20] \\ primes
genPrimes :: Gen (Input,Expected,Message)
genPrimes = ( \ prime -> (prime,True,show prime ++ " is prime") ) <$> elements primes
genComposites :: Gen (Input,Expected,Message)
genComposites = ( \ composite -> (composite,False,show composite ++ " is composite") ) <$> elements composites
-- generators could also use a reference solution
spec :: Spec
spec = do
it "random tests" $ do
once $ forAllBlind (gen [(genPrimes,50),(genComposites,100)]) $ \ tests -> do
for_ tests $ \ (arg,expected,msg) -> do
print arg
solution arg `shouldBe` expected $ msg
-- can also factor out the specific plumbing
it "random tests" $ do
forAllGenerators [(genPrimes,50),(genComposites,100)] $ \ (arg,expected,msg) -> do
print arg
solution arg `shouldBe` expected $ msg
gen :: [(Gen (Input,Expected,Message),Int)] -> Gen [(Input,Expected,Message)]
gen gens = do
shuffle =<< concat <$> for gens ( \ (gen,n) -> replicateM n gen )
forAllGenerators :: [(Gen (Input,Expected,Message),Int)] -> ((Input,Expected,Message) -> Expectation) -> Property
forAllGenerators gens prop = do
once $ forAllBlind (gen gens) $ \ tests -> do
for_ tests prop
shouldBe :: (Show a,Eq a) => a -> a -> Message -> Expectation
shouldBe actual expected msg = unless (actual == expected) $ expectationFailure $ concat [ msg, "\nexpected : ", show expected, "\nbut got : ", show actual ]
-- a shouldBe that can show a message
give me two candy!
pekora(a,b)=> 'candy';
add(a)=> 'candy';
give me two candy
pekora(a,b)=> 'candy';
add(a)=> 'candy';
// See https://pub.dartlang.org/packages/test
import "package:test/test.dart";
import "package:solution/solution.dart";
void main() {
test("aaa", () {
expect(pekora(1, 1), equals('candy'));
expect(add(11), equals('candy'));
});
}
AWDawdawdawdawd
public class wdawd() {
int dadaf = 1;
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
Write a function called findFirstSubString
that accept two not null string(string and substring
) and returns the index of the first occurrence of the substring in the string.
Examples
findFirstSubString("1234","12") => returns 0
findFirstSubString("012","12") => returns 1
findFirstSubString("4321","21") => returns 2
findFirstSubString("ABCDE","21") => returns null
findFirstSubString("","21") => returns null
Notes
- if no substring is found the function returns NULL
- the function returns only the index of the first occurrence of the string
fun findFirstSubString(string: String, subString: String): Int? {
val stringArray = string.toList()
val substringArray = subString.toList()
if (stringArray.size < substringArray.size || stringArray.isEmpty() || substringArray.isEmpty()) {
return null
}
var counter = 0
var startIndex = 0
var i = 0
while (i < stringArray.size) {
if (stringArray[i] == substringArray[counter]) {
if(counter == 0) {
startIndex = i
}
if ((counter + 1) == substringArray.size) {
return startIndex
}
counter++
i++
}
else {
if (counter > 0) {
i = startIndex + 1
}
else {
i++
}
counter = 0
startIndex = 0
}
println(i)
}
return null
}
import org.junit.Assert
import org.junit.Test
class SimpleSubStringSearchKtTest {
@Test
fun `Substring longer than string`() {
Assert.assertNull(findFirstSubString("12345", "123456"))
Assert.assertNull(findFirstSubString("54321", "123456"))
}
@Test
fun `Empty string`() {
Assert.assertNull(findFirstSubString("", "123456"))
}
@Test
fun `Empty substring`() {
Assert.assertNull(findFirstSubString("12323123", ""))
}
@Test
fun `Empty substring and string`() {
Assert.assertNull(findFirstSubString("", ""))
}
@Test
fun `Correct finding`() {
Assert.assertEquals(findFirstSubString("12345", "123"), 0)
Assert.assertEquals(findFirstSubString("54321321", "321"), 2)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "ABCDE"), 9)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "A"), 9)
Assert.assertEquals(findFirstSubString("123456789ABCDE123456789", "a"), null)
Assert.assertEquals(findFirstSubString("111234", "1234"), 2)
}
}
Summary
Given an integer number - let's call it N -, calculate the sum of the first positive integers up to N (inclusive) that are divisible by 3 or 7.
Example
Let's take N = 30:
Sum of integers divisible by 3 : 3 + 6 + 9 + 12 + 15 + 18 + 21 + 24 + 27 + 30 + 33 = 165 (#1)
Sum of integers divisible by 7 : 7 + 14 + 21 + 28 + 35 = 70 (#2)
Sum of integers divisible by 3 and 7 : 21 + 42 = 21 (#3)
Total : #1 + #2 - #3 = 165 + 70 - 21 = 214
Attention
Please be wary that N may be a large number (N larger than 1,000,000). This requires brushing up on optimization.
Caveat
There is one way of resolving this problem in O(1) complexity. For further details, please refer to this wiki.
using System;
namespace Kumite
{
public class Problem
{
public static long Sum(int N)
{
// Add your code here.
return 0;
}
}
}
namespace Solution {
using NUnit.Framework;
using System;
[TestFixture]
public class SolutionTest
{
[Test]
public void TestForProblemDescription()
{
Assert.AreEqual(214, Kumite.Problem.Sum(30));
}
[Test]
public void TestForNEquals1()
{
Assert.AreEqual(0, Kumite.Problem.Sum(1));
}
[Test]
public void TestForNEquals2()
{
Assert.AreEqual(0, Kumite.Problem.Sum(2));
}
[Test]
public void TestForNEquals3()
{
Assert.AreEqual(3, Kumite.Problem.Sum(3));
}
[Test]
public void TestForExtremeN()
{
Assert.IsTrue(498404749818 == Kumite.Problem.Sum(1525087));
}
[Test]
public void TestForNEquals8()
{
Assert.AreEqual(16, Kumite.Problem.Sum(8));
}
[Test]
public void TestForNEqualsGreaterThan300()
{
Assert.AreEqual(36346, Kumite.Problem.Sum(412));
}
}
}
Write a function that adds the digits of an integer. For example, the input receives a number: 10023. The result should be - 6 (1 + 0 + 0 + 2 + 3).
def digit_sum(number: int) -> int:
return(sum([int(num) for num in str(abs(number))]))
from unittest import TestCase
from solution import digit_sum
import codewars_test as test
from solution import digit_sum
@test.describe('Tests')
def tests():
@test.it('Test Cases')
def test_sums():
test.assert_equals(digit_sum(10000000000000000000000000000),1)
test.assert_equals(digit_sum(55001),11)
test.assert_equals(digit_sum(104),5)
test.assert_equals(digit_sum(0),0)
test.assert_equals(digit_sum(-1204),7)