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 container that you access like a sorted container, but only sorts the parts it needs to.
Based off of quicksort, so O(N**2)
for a single access.
However, everything only needs to be sorted once, so it is O(N**2+Q)
for Q queries.
#include <iostream>
#include <utility>
#include <memory>
#include <vector>
using pii = std::pair<int, int>;
class kata {
std::vector<std::shared_ptr<pii>> ranges;
public:
std::vector<int> &data;
kata(std::vector<int> &in_data)
: data(in_data)
, ranges(in_data.size(), std::shared_ptr<pii>(new pii(0, in_data.size())))
{
}
int operator[](int index) {
for (int x : data) {
std::cout << x << ' ';
}
std::cout << "| " << ranges[index]->first << ' ' << ranges[index]->second << '\n';
int start = ranges[index]->first;
int end = ranges[index]->second;
if (end - start == 1) {
return data[index];
}
int pivot = data[index];
data[index] = data[end - 1];
data[end - 1] = pivot;
std::shared_ptr<pii> lt_pivot(new pii(start, start));
int p = start;
for (int i = start; i < end - 1; ++i) {
if (data[i] < pivot) {
int temp = data[p];
data[p] = data[i];
data[i] = temp;
ranges[p] = lt_pivot;
++p;
}
}
lt_pivot->second = p;
data[end - 1] = data[p];
data[p] = pivot;
ranges[p]->first = p;
ranges[p]->second = p + 1;
std::shared_ptr<pii> gt_pivot(new pii(p + 1, end));
for (int i = p + 1; i < end; ++i) {
ranges[i] = gt_pivot;
}
return this->operator[](index);
}
};
// TODO: Replace examples and use TDD by writing your own tests
Describe(any_group_name_you_want)
{
It(should_do_something)
{
//Assert::That("some value", Equals("another value"));
}
};
capslock
Uppercase the first character of each word in a string.public function caps($c)
{
return ucwords($c);
}
<?php
use PHPUnit\Framework\TestCase;
class UpperallTest extends TestCase
{
public function testSampleTests()
{
$this->assertEquals("Hello My Friends.", $this->Upperall->caps("hello my friends."));
}
}
In order to understand the everyday hasstle of people that suffer from dislexia my school made us do this activity:
- replace all the (A's O's E's I's) of a certain phrase with (4's 0's 3's 1's) respectively
and we did it, but it took soooo long, would you write a bit of code that does the work for me?
e.g.:
dislexifier ->
- input : "Hey Arianna, how u doing?"
- output : "H3y 4ri4nn4, h0w u d01ng?"
ps.: try to avoid just using .replace(), it will make it more fun.
class Dislexifier
{
public static String theDislexifier(String str)
{
// TODO: Do your magic here
return str.replace("a","4").replace("A","4").replace("o","0").replace("O","0").replace("e","3").replace("E","3").replace("i","1").replace("I","1");
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD by writing your own tests
public class SolutionTest {
@Test
public void testDislixifier() {
assertEquals("1'm s0 hungry 1 c0uld 34t 4 b34r rn",Dislexifier.theDislexifier("I'm so hungry I could eat a bear rn"));
assertEquals("Wh4t 4r3 th000000s3???",Dislexifier.theDislexifier("What are thoooooose???"));
assertEquals("",Dislexifier.theDislexifier(""));
assertEquals("üüüü 1s just 4 f4ncy u, 4lth0ugh n0t 4s f4ncy 4s y0u",Dislexifier.theDislexifier("üüüü is just a fancy u, although not as fancy as you"));
assertEquals("щф, l1t3r4lly just g00gl3d \"russ14n l3tt3rs\", th4t sh1dd's w31rd\"",Dislexifier.theDislexifier("щф, literally just googled \"russian letters\", that shidd's weird\""));
assertEquals("Try gypsy hymns rhythm !",Dislexifier.theDislexifier("Try gypsy hymns rhythm !"));
}
}
It will get a dictionary and return a list of the keys, all sorted by their corrosponding values in increasing order.
def dict_index(dictionary, value):
dict_keys = list(dictionary)
dict_values = list(dictionary.values())
return dict_keys[dict_values.index(value)]
def dict_sort(dictionary):
dict_values = list(dictionary.values())
new_keys = []
for x in range(len(dictionary)):
max_value = max(dict_values)
dict_values.remove(max_value)
new_keys.append(dict_index(dictionary, max_value))
new_keys.reverse()
return new_keys
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(dict_sort({'a':1,'b':2,'c':-1}), ['c','a','b'])
test.assert_equals(dict_sort({'dog':25,'rat':-7}), ['rat','dog'])
A Python class that converts strings to integers and back.
itos: int-to-string
stoi: string-to-int
Note: Currently broken
def dict_sort(dictionary):
return sorted(dictionary, key=dictionary.__getitem__)
def convert_to_base(decimal_number, base, digits):
remainder_stack = []
while decimal_number > 0:
remainder = decimal_number % base
remainder_stack.append(remainder)
decimal_number = decimal_number // base
new_digits = []
while remainder_stack:
new_digits.append(digits[remainder_stack.pop()])
return ''.join(new_digits)
def base_to_dec(string, base, digits):
num_str = string[::-1]
num = 0
for k in range(len(num_str)):
dig = num_str[k]
if dig.isdigit():
dig = int(dig)
else:
dig = digits.index(dig.lower())-digits.index('a')+10
num += dig*(base**k)
return int(num)
class converter:
def __init__(self, codec=None):
if codec == None:
self.codec = ''
else:
self.codec = codec
def fit(self, strings):
chars = {}
if type(strings) == list:
string = '\n'.join(strings)
else:
string = strings
for x in string:
if x not in chars:
chars[x] = string.count(x)
self.codec = ''.join(dict_sort(chars))
def stoi(self, string):
return base_to_dec(string, len(self.codec), self.codec)
def itos(self, number):
return convert_to_base(number, len(self.codec), self.codec)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Basic tests")
def test_group():
converter_var = converter()
converter_var.fit('Hello World! I am a program!')
test.assert_equals(converter_var.itos(converter_var.stoi('I am a program!')))
You need to write a function that checks if a number is prime or not.
Examples:
prime_checker(100) => False
prime_checker(5) => True
prime_checker(709) => True
So if the given number is prime you need to return True
, and if not you need to return False
Hint: Probably you will need to import math:
import math
import math
def prime_checker(n):
if n == 1:
return False
max_divisor = math.floor(math.sqrt(n))
for d in range(2, 1 + max_divisor):
if n % d == 0:
return False
return True
import codewars_test as test
from solution import prime_checker
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(prime_checker(653), True)
test.assert_equals(prime_checker(654), False)
test.assert_equals(prime_checker(5), True)
test.assert_equals(prime_checker(777), False)
test.assert_equals(prime_checker(977), True)
test.assert_equals(prime_checker(125), False)
test.assert_equals(prime_checker(997), True)
test.assert_equals(prime_checker(709), True)
test.assert_equals(prime_checker(15), False)
There is a vector class consisting of 3 coordinates and a segment class consisting of 2 vectors. The task is to write a function that finds the intersection of two segments, if it exists.
The idea of solving the problem is to find the minimum distance between the segments, and if it is less than a certain specified value, then we can assume that the segments intersect.
float scalar_product(Vector3D a, Vector3D b)
{
return (a.get_x()*b.get_x() + a.get_y()*b.get_y() + a.get_z()*b.get_z());
}
Vector3D intersec(Segment a, Segment b)
{
Segment seg;
float t1 = 0, t2 = 0, t = 0;
t1+= scalar_product(a.start-b.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
t1-= scalar_product(a.start-b.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
t2+= scalar_product(a.end-a.start, a.end-a.start)*scalar_product(b.end-b.start, b.end-b.start);
t2-= scalar_product(a.end-a.start, b.end-b.start)*scalar_product(b.end-b.start, a.end-a.start);
t = t1/t2;
float u1 = 0, u2 = 0;
u1+= (scalar_product(a.start-b.start, b.end-b.start));
u1+= t*scalar_product(a.end-a.start, b.end-b.start);
u2 = scalar_product(b.end-b.start, b.end-b.start);
seg.end = a.start + (a.end - a.start)*t;
seg.start = b.start + (b.end - b.start)*u1/u2;
Vector3D tmp = seg.end - seg.start;
Vector3D close_point = (seg.end + seg.start) / 2;
try
{
if (scalar_product(close_point - a.start, a.end - a.start) < 0 ||
scalar_product(close_point - a.end, a.start - a.end) < 0)
throw std::string("point outside first segment bound");
if (scalar_product(close_point - b.start, b.end - b.start) < 0 ||
scalar_product(close_point - b.end, b.start - b.end) < 0)
throw std::string("point outside second segment boundaries");
if ((scalar_product(tmp, tmp) - 0.001) > 0)
throw std::string("no intersection point");
}
catch(const std::string& ex)
{
std::cerr << ex << '\n';
exit(1);
}
return close_point;
}
Vector3D::Vector3D(){}
Vector3D::Vector3D(float x, float y, float z):x(x), y(y), z(z){}
Vector3D::Vector3D(const Vector3D &v): x(v.x), y(v.y), z(v.z){}
const float Vector3D::get_x(){return x;}
const float Vector3D::get_y(){return y;}
const float Vector3D::get_z(){return z;}
void Vector3D::set_x(float x){this->x = x;}
void Vector3D::set_y(float y){this->y = y;}
void Vector3D::set_z(float z){this->z = z;}
Vector3D& Vector3D::operator=(const Vector3D &v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector3D Vector3D::operator+(const Vector3D &v)
{
return Vector3D(x+v.x, y+v.y, z+v.z);
}
Vector3D Vector3D::operator-(const Vector3D &v)
{
return Vector3D(x-v.x, y-v.y, z-v.z);
}
Vector3D Vector3D::operator*(const float &value)
{
return Vector3D(x*value, y*value, z*value);
}
Vector3D Vector3D::operator/(const float &value)
{
return Vector3D(x/value, y/value, z/value);
}
Segment::Segment(){};
Segment::Segment(Vector3D start, Vector3D end): start(start), end(end){}
Describe(any_group_name_you_want)
{
It(should_do_something)
{
Assert::That("some value", Equals("another value"));
}
};
Return a string that says "Test"
let rTest = ()=>"Test"
// 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() {
assert.strictEqual(rTest(), "Test")
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
It has been decided that I like Ruby.
require "net/http"
require "nokogiri"
langs = {}
langs.default = 0
(1..10).each { |page|
Nokogiri::XML(Net::HTTP.get(URI("https://www.codewars.com/kumite?page=#{page}")))
.xpath("//div[contains(@class, 'flex flex-row items-center mb-4')]")
.each { |node|
lang = node.xpath("string(./div[contains(@class, "\
"'icon-container mb-0 bg-gray-200 dark:bg-k-20')]"\
"/i/@class)")
langs[lang[10...lang.length - 1]] += 1
}
}
puts langs
# From Ruby 3.0, RSpec is used under the hood.
# See https://rspec.info/
# Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
describe "Example" do
it "should return the sum" do
expect(1 + 1).to eq(2)
# The following is still supported, but new tests should now use them.
# Test.assert_equals(add(1, 1), 2)
end
end
def factorial(n: int) -> int:
"""Returns factorial of a non-negative integer."""
return 1 if n <= 0 else eval('*'.join([str(i) for i in range(1, n + 1)]))
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
test.assert_equals(factorial(-1), 1)
test.assert_equals(factorial(0), 1)
test.assert_equals(factorial(1), 1)
test.assert_equals(factorial(2), 2)
test.assert_equals(factorial(3), 6)
test.assert_equals(factorial(4), 24)
test.assert_equals(factorial(5), 120)
test.assert_equals(factorial(6), 720)
test.assert_equals(factorial(7), 5040)
test.assert_equals(factorial(8), 40320)
test.assert_equals(factorial(9), 362880)
test.assert_equals(factorial(10), 3628800)