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.
\pi
$\pi$
def add(a, b):
return a + b
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
We know that Finn
is friends with Jake
, Flame Princess
is friends with Finn
as is Princess Bubblegum
, Jake
is friends with Lady Unicorn
and Princess Bubblegum
.
Write a function friends(X) -> [Y]
to find out who are the friends of a person X
.
friend('Finn', 'Jake').
friend('Flame Princess', 'Finn').
friend('Princess Bubblegum', 'Finn').
friend('Jake', 'Lady Unicorn').
friend('Jake', 'Princess Bubblegum').
friends(X, R) :- findall(Y, friend(X, Y), R1), findall(Y, friend(Y, X), R2), append(R1, R2, R).
% plunit can be used to test solution
:- begin_tests(friends).
:- include(friends).
test(friends_of_finn) :-
friends('Finn', X),
assertion(X == ['Jake', 'Flame Princess', 'Princess Bubblegum']).
test(friends_of_bubblegum) :-
friends('Princess Bubblegum', X),
assertion(X == ['Finn', 'Jake']).
:- end_tests(friends).
Crie uma classe que receba dois inteiros e retorne a soma desses dois inteiros.
public class Primeira {
public static int sum (int a, int b) {
return a + b;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FirstClassTest {
@Test
public void testSum() throws Exception {
int a = 1;
int b = 2;
assertEquals(3, Primeira.sum(a, b));
}
}
Write a metafunction whose result type reverses the template parameters of a std::tuple
.
(Maybe, possibly, functional programming.)
// For convenience
template <typename, typename>
struct append;
template <typename ...Ts, typename ...Us>
struct append<std::tuple<Ts...>, std::tuple<Us...>> {
using type = std::tuple<Ts..., Us...>;
};
template <typename>
struct reverse;
template <typename T, typename ...Ts>
struct reverse<std::tuple<T, Ts...>> {
using type = typename append<
typename reverse<std::tuple<Ts...>>::type,
std::tuple<T>
>::type;
};
template <>
struct reverse<std::tuple<>> {
using type = std::tuple<>;
};
static_assert(
std::is_same_v<
reverse<std::tuple<int, short, char>>::type,
std::tuple<char, short, int>
>
);
static_assert(
std::is_same_v<
reverse<std::tuple<int&, short, char, void, int*>>::type,
std::tuple<int*, void, char, short, int&>
>
);
// Test cases are compile-time, so if we got here, it passed.
Describe(any_group_name_you_want)
{
It(should_do_something)
{
Assert::That(true);
}
};
A positive integer is an anti-prime when it has more divisors than each positive integer lesser than it.
https://www.youtube.com/watch?v=2JM2oImb9Qg
Write a function that find the largest anti-prime number not exceeding a given number.
1 - as there is no positive numbers below 1 the 1 is a first anti-prime and has one divisor.
2 - has two divisors: 1, 2 so is's anti-prime.
3 - has also two divisors: 1, 3 so it's NOT an anti-prime because 2 already had two divisors.
Other examples of anti-primes are: 1, 2, 4, 6, 12, 24.
export function solution(max: number): number {
if (max === 0) {
return 0;
}
let maxDiv: number = 0;
let lastAnti = 1;
for (let possibleAnti: number = 2; possibleAnti <= max; possibleAnti++) {
let divCount = 1;
for (let possibleDiv = 1; possibleDiv <= max; possibleDiv++) {
if (possibleAnti % possibleDiv === 0) {
divCount++;
}
}
if (divCount > maxDiv) {
maxDiv = divCount;
lastAnti = possibleAnti;
}
}
return lastAnti;
}
import { solution } from './solution';
import { assert } from 'chai';
describe("example", function() {
it("test", function() {
assert.strictEqual(solution(0), 0);
assert.strictEqual(solution(1), 1);
assert.strictEqual(solution(2), 2);
assert.strictEqual(solution(3), 2);
assert.strictEqual(solution(4), 4);
assert.strictEqual(solution(5), 4);
assert.strictEqual(solution(11), 6);
assert.strictEqual(solution(25), 24);
assert.strictEqual(solution(5040), 5040);
});
});
A factorial is a number that has been multiplied by each number preceding it. In this exercise, use recursion to get the factorial of any given number. A recursive function is one that calls itself conditionally, for example a function that takes a number, prints it out, then calls itself with func_name(parameter_name - 1) to restart the process using a number one lower. Combine this with an if statement to avoid a recursion error, and you have a basic recursive function. Use this concept to find the factorial of any given number.
def factorial(n):
# your code goes here
return
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(factorial(5), 120)
test.assert_equals(factorial(6), 720)
test.assert_equals(factorial(3), 6)
(Moved here per the discussion in https://www.codewars.com/kata/5fbc297af8ed8d0008e971c9/discuss)
Assume you have an array of Person objects that looks like this:
[
{age: 10, hairColor: "brown"},
{age: 30, hairColor: "red"},
{age: 20, hairColor: "brown"},
{age: 40, hairColor: "black"},
];
Create the following functions:
-
totalAgeYoungerThan
- returns the total ages of the people younger than the given age -
hairColorsYoungerThan
- returns an array of the hair colors of the people younger than the given age
In each of these new functions, you may not use for
loops, and instead must solve them using any of .map
, .filter
and/or .reduce
.
// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
// let totalAge = 0;
// for (let i = 0; i < people.length; i++) {
// if (people[i].age < age) totalAge += people[i].age;
// }
// return totalAge;
// }
// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
// let totalAge = 0;
// for (let person of people) {
// if (person < age) totalAge += person.age;
// }
// return totalAge;
// }
// this code should fail the tests
// const totalAgeYoungerThan = (people, age) => {
// if (people.length === 0) return 0;
// const [first, ...rest] = people;
// return (first.age < age ? first.age : 0) + totalAgeYoungerThan(rest, age);
// }
// this code should theoretically pass the tests
// it includes the word `for` but not in a for loop
// const totalAgeYoungerThan = (people, age) => {
// return people
// .filter(person => person.age < age)
// .reduce((formula, person) => formula + person.age, 0)
// }
const totalAgeYoungerThan = (people, age) => {
return people
.filter(person => person.age < age)
.reduce((sum, person) => sum + person.age, 0)
}
const hairColorsYoungerThan = (people, age) => {
return people
.filter(person => person.age < age)
.map(person => person.hairColor)
}
const chai = require('chai');
const expect = chai.expect;
chai.config.truncateThreshold = 0;
const people = [
{age: 10, hairColor: "brown"},
{age: 30, hairColor: "red"},
{age: 20, hairColor: "brown"},
{age: 40, hairColor: "black"},
];
people.forEach(person => Object.freeze(person));
Object.freeze(people);
describe("challenge", () => {
let originalMap, originalFilter, originalReduce;
let mapCallCount = 0, filterCallCount = 0, reduceCallCount = 0;
beforeEach(function() {
originalMap = Array.prototype.map;
originalFilter = Array.prototype.filter;
originalReduce = Array.prototype.reduce;
Array.prototype.map = function() {
mapCallCount++;
return originalMap.apply(this, arguments);
}
Array.prototype.filter = function() {
filterCallCount++;
return originalFilter.apply(this, arguments);
}
Array.prototype.reduce = function() {
reduceCallCount++;
return originalReduce.apply(this, arguments);
}
});
afterEach(function() {
Array.prototype.map = originalMap;
Array.prototype.filter = originalFilter;
Array.prototype.reduce = originalReduce;
});
describe("totalAgeYoungerThan",() => {
it("returns the correct answer", () => {
expect(totalAgeYoungerThan(people, 30)).to.deep.eq(30);
})
it("returns 0 when there are no people", () => {
expect(totalAgeYoungerThan([], 30)).to.deep.eq(0);
})
it("returns 0 when there are no people who meet the criteria", () => {
expect(totalAgeYoungerThan(people, -1)).to.deep.eq(0);
})
it("does not use a for loop", () => {
// TODO: toString can be overwritten - find out how to read solution.txt?
expect(totalAgeYoungerThan.toString()).to.not.include("for");
})
it("uses reduce", () => {
totalAgeYoungerThan(people, 30);
expect(reduceCallCount).to.be.at.least(1);
})
});
describe("hairColorsYoungerThan",() => {
it("returns the correct answer", () => {
expect(hairColorsYoungerThan(people, 30)).to.deep.eq(["brown", "brown"]);
})
it("returns an empty array when there are no people", () => {
expect(hairColorsYoungerThan([], 30)).to.deep.eq([]);
})
it("returns an empty array when there are no people who meet the criteria", () => {
expect(hairColorsYoungerThan(people, -1)).to.deep.eq([]);
})
it("does not use a for loop", () => {
expect(hairColorsYoungerThan.toString()).to.not.include("for");
})
it("does use .map and .filter", () => {
totalAgeYoungerThan(people, 30);
expect(mapCallCount).to.be.at.least(1);
expect(filterCallCount).to.be.at.least(1);
})
});
describe("random tests", () => {
const dataSets = [];
for(let i = 0; i < 10; i++) {
const hairColors = ["brown", "black", "blue", "red", "yellow", "orange"];
const people = [];
const numberOfPeople = Math.random() * 20 + 5;
for(let j = 0; j < numberOfPeople; j++) {
people.push({age: j, hairColor: hairColors[Math.floor(Math.random() * hairColors.length)]});
}
people.forEach(person => Object.freeze(person));
Object.freeze(people);
dataSets.push(people);
}
describe("totalAgeYoungerThan", () => {
const totalAgeYoungerThanSolution = (people, age) => {
return people
.filter(person => person.age < age)
.reduce((sum, person) => sum + person.age, 0)
}
dataSets.forEach((people, i) => {
const randomAges = [
people[Math.floor(Math.random() * people.length)].age,
people[Math.floor(Math.random() * people.length)].age,
people[Math.floor(Math.random() * people.length)].age,
];
randomAges.forEach(randomAge => {
it(`passes for random data #${i} and age ${randomAge}`, () => {
expect(totalAgeYoungerThan(people, randomAge)).to.deep.eq(totalAgeYoungerThanSolution(people, randomAge));
});
});
});
});
describe("hairColorsYoungerThan", () => {
const hairColorsYoungerThanSolution = (people, age) => {
return people
.filter(person => person.age < age)
.map(person => person.hairColor)
}
dataSets.forEach((people, i) => {
const randomAges = [
people[Math.floor(Math.random() * people.length)].age,
people[Math.floor(Math.random() * people.length)].age,
people[Math.floor(Math.random() * people.length)].age,
];
randomAges.forEach(randomAge => {
it(`passes for random data #${i} and age ${randomAge}`, () => {
expect(hairColorsYoungerThan(people, randomAge)).to.deep.eq(hairColorsYoungerThanSolution(people, randomAge));
});
});
});
});
});
})
Given an integer num, return an array consisting of positive consecutive integers that sum to num. There may be multiple sequences that work; return the sequence with the largest number of consecutive integers. Return an empty array if there are no solutions.
Example:
maxConsecutiveSum(45) returns [1,2,3,4,5,6,7,8,9].
Notice that [14,15,16] also add to 45 but there is a solution that has more numbers in it.
public class maxConsecutiveSum{
public static int[] maxConsecutiveSum(int num){
//code goes here
return [];
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Test
public void testSomething() {
assertEquals(maxConsecutiveSum(45), [1,2,3,4,5,6,7,8,9]);
assertEquals(maxConsecutiveSum(3), [1,2]);
assertEquals(maxConsecutiveSum(2), []);
assertEquals(maxConsecutiveSum(182), [44,45,46,47]);
}
}
Nous dirons qu'un nombre n est positif n est parent du nombre à 2 chiffres ab
si son chiffre des unités est b et si ses autres chiffres sont différents de 0 et que leur somme est a.
Par exemple, les parents de 31 sont 31, 121, 211 et 1111
We say that a number n is parent of the number ab if it ones digit is equal to b and if he hasn't any digits equal to 0 and finally, if the sum of the digits of n is equal to a+b.
For example
121 is parent of 31 because :
1 = 1, 0 isn't in 1,2 and because 1 + 2 + 1 = 4 = 3 +1.
What you need to do
Complete the parents_of(n:int)
function so that she return every parents of n between 1 and 1000.
def parents_of(n:int):
toTest=[i for i in range(11,1000) if not "0" in str(i)]
dics={str(i):[] for i in toTest}
for nb in toTest:
for i in range(11,1000):
if str(i)[-1]==str(nb)[-1]: #Si ils ont le même chiffre des unités
if not "0" in str(i)[:-1]: #Si ils n'ont pas de 0
if sum([int(e) for e in str(i)])==sum([int(e) for e in str(nb)]): #Si ils ont la même some
dics[str(nb)]+=[i]
return dics[str(n)]
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("parents_of 31")
def thirty_one():
test.assert_equals(parents_of(31), [31, 121, 211])
@test.it("parents of 98")
def ninty_eight():
test.assert_equals(parents_of(99),[99, 189, 279, 369, 459, 549, 639, 729, 819])
Create an infinite list of alternating 1
s and 0
s, like so:
1:0:1:0:....
module Knot where
import Debug.Trace
onesAndZeroes :: Num a => [a]
onesAndZeroes =
let x = 1 : y
y = 0 : x
in x
module ExampleSpec where
-- Tests can be written using Hspec http://hspec.github.io/
-- Replace this with your own tests.
import Test.Hspec
import Knot
-- `spec` of type `Spec` must exist
spec :: Spec
spec = do
describe "Some taken values are correct" $ do
it "take some values" $ do
(take 4 onesAndZeroes) `shouldBe` ([1, 0, 1, 0])
-- the following line is optional for 8.2
main = hspec spec