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.
Dado o tamanho da base e da altura de um retângulo, calcular a sua área e o seu
perímetro.
let base = 8
let altura = 4
const area = base * altura
const perimetro = (2 * base) + (2 * altura)
console.log(`a área do retângulo é ${area} e o perímetro é ${perimetro}`)
Background Info
In Object Oriented Programming (OOP
), inheritence
is a mechanism where you can to derive a set of class attributes and methods from a hierarchical class. Polymorphism
refers to the ability of an object to take on multiple forms. For example, class objects belonging to the same hierarchical tree (inherited from a common parent class) may have functions with the same name, but with different behaviors.
Objective
- Become familiar with
OOP
, and understand concepts of classinheritance
, andpolymorphism
.
Task
Develop a program that calculates the area of a circle using OOP
, and demonstartes both inheritence
,and polymorphism
in the design. Include and update tests with any changes.
import math
class Shape:
"""A Shape base class."""
def __init__(self, color, thickness, location):
self.color = color
self.thickness = thickness
self.location = location
def __str__(self):
return f'{self.__class__.__name__}(color={self.color}, thickness={self.thickness}, location={self.location})'
def change_color(self, color):
self.color = color
class Circle(Shape):
"""A Circle class that inherits from Shape class."""
def __init__(self, radius, color, thickness, location):
super().__init__(color, thickness, location)
self.radius = radius
def __str__(self):
return f'{super().__str__()[:-1]}, radius={self.radius})'
def change_color(self, color):
"""Changes the color attribute of Shape. This is also an example of polymorphism"""
old, self.color = self.color, color
return f'The color has changed from {old} to {self.color}!'
def area(self):
"""Returns the area of a Circle"""
return math.pi * self.radius ** 2
import codewars_test as test
from solution import Shape, Circle
@test.describe("Testing Circle class kumite:")
def test_group():
@test.it("test case 1: Testing for inheritence:")
def test_case():
c = Circle(113.776, 'black', 7, (7, 6))
# (!) NOTE: Circle inheritences attributes of the Shape class:
test.assert_equals(c.color, 'black')
test.assert_equals(c.thickness, 7)
test.assert_equals(c.location, (7, 6))
@test.it("test case 2: Testing for polymorphism:")
def test_case():
s = Shape('white', 1, (0, 0))
c = Circle(113.776, 'black', 7, (7, 6))
# (!) NOTE: The change_color() method is polymorphic between the Shape and Circle class:
test.assert_equals(s.change_color('red'), None)
test.assert_equals(c.change_color('blue'), f'The color has changed from black to blue!')
рандомная фигура
from tkinter import *
from random import randint
root = Tk()
root.geometry("300x300")
figura = ""
def randomf():
global figura
c.delete("all")
num = randint(1,3)
if num ==1:
figura = c.create_oval(10,10,20,30)
elif num == 2:
figura = c.create_rectangle(10,10,20,20)
else:
figura = c.create_polygon(10,10,20,40,20,20)
b = Button(text="Нажми", command=randomf)
b.place(x=0,y=270)
c = Canvas(bg="white", height=100,width=100)
c.pack()
root.mainloop()
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)
int convert(char bin) {
int dec = 0;
for (int i = 7; i >= 0; i--) {
dec = dec << 1;
if ((bin & (1 << i)) != 0)
dec ++;
}
return dec;
}
Describe(test_cases)
{
It(main_tests)
{
Assert::That(convert(0b00000101), Equals(5));
Assert::That(convert(0b11111110), Equals(254));
Assert::That(convert(0b10101010), Equals(170));
Assert::That(convert(0b11001100), Equals(204));
}
It(min_max_tests)
{
Assert::That(convert(0b00000000), Equals(0));
Assert::That(convert(0b11111111), Equals(255));
}
};
💯
fn return_hundred() -> i32 { 100 }
// Add your tests here.
// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_return_hundred_1() {
assert_eq!(return_hundred(), 100);
}
#[test]
fn test_return_hundred_2() {
assert_eq!(return_hundred(), 100 * 1);
}
#[test]
fn test_return_hundred_3() {
assert_eq!(return_hundred(), 50 * 2);
}
#[test]
fn test_return_hundred_4() {
assert_eq!(return_hundred(), 25 * 4);
}
#[test]
fn test_return_hundred_5() {
assert_eq!(return_hundred(), (12.5 * 8 as f32) as i32);
}
#[test]
fn test_return_hundred_6() {
assert_eq!(return_hundred(), (6.25 * 16 as f32) as i32);
}
#[test]
fn test_return_hundred_7() {
assert_eq!(return_hundred(), 100 / 1);
}
#[test]
fn test_return_hundred_8() {
assert_eq!(return_hundred(), (50.0 / 0.5) as i32);
}
#[test]
fn test_return_hundred_9() {
assert_eq!(return_hundred(), (25.0 / 0.25) as i32);
}
#[test]
fn test_return_hundred_10() {
assert_eq!(return_hundred(), (12.5 / 0.125 as f32) as i32);
}
#[test]
fn test_return_hundred_11() {
assert_eq!(return_hundred(), (6.25 / 0.0625 as f32) as i32);
}
#[test]
fn test_return_hundred_12() {
assert_eq!(return_hundred(), 10 * 10);
}
#[test]
fn test_return_hundred_13() {
assert_eq!(return_hundred(), (40.0 * 2.5) as i32);
}
#[test]
fn test_return_hundred_14() {
assert_eq!(return_hundred(), 75 + 25);
}
#[test]
fn test_return_hundred_15() {
assert_eq!(return_hundred(), 500 / 5);
}
#[test]
fn test_return_hundred_16() {
assert_eq!(return_hundred(), 20 * 5);
}
#[test]
fn test_return_hundred_17() {
assert_eq!(return_hundred(), 300 / 3);
}
#[test]
fn test_return_hundred_18() {
assert_eq!(return_hundred(), 50 * 2);
}
#[test]
fn test_return_hundred_19() {
assert_eq!(return_hundred(), 400 / 4);
}
#[test]
fn test_return_hundred_20() {
assert_eq!(return_hundred(), (80.0 * 1.25) as i32);
}
#[test]
fn test_return_hundred_21() {
assert_eq!(return_hundred(), (8.0 * 12.5) as i32);
}
#[test]
fn test_return_hundred_22() {
assert_eq!(return_hundred(), (250.0 / 2.5) as i32);
}
#[test]
fn test_return_hundred_23() {
assert_eq!(return_hundred(), (30.0 * 3.33) as i32 + 1);
}
#[test]
fn test_return_hundred_24() {
assert_eq!(return_hundred(), 150 - 50);
}
#[test]
fn test_return_hundred_25() {
assert_eq!(return_hundred(), 90 + 10 * 1);
}
}
Multiplies the elements inside a vector
fn vector_multiply(input: Vec<i32>) -> i32 {
let mut out = 1;
for i in input {
out *= i;
}
out
}
// Add your tests here.
// See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
assert_eq!(vector_multiply(vec![2, 5]), 10);
}
#[test]
fn test_2() {
assert_eq!(vector_multiply(vec![2, 5, 5, 2]), 100);
}
#[test]
fn test_3() {
assert_eq!(vector_multiply(vec![200, -1000]), -200000);
}
#[test]
fn test_4() {
assert_eq!(vector_multiply(vec![0]), 0);
}
#[test]
fn test_5() {
assert_eq!(vector_multiply(vec![1, 1, 1, 1, 2, 2, 2, 2]), 16);
}
#[test]
fn test_6() {
assert_eq!(vector_multiply(vec![30, 30, 30]), 27000);
}
#[test]
fn test_7() {
assert_eq!(vector_multiply(vec![-1, 5, 10, 50]), -2500);
}
#[test]
fn test_8() {
assert_eq!(vector_multiply(vec![-598, 2]), -1196);
}
#[test]
fn test_9() {
assert_eq!(vector_multiply(vec![]), 1);
}
#[test]
fn test_10() {
assert_eq!(vector_multiply(vec![-1000, 500, -2]), 1000000);
}
}
fn nothing() {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
nothing()
}
}
all described in title lol
__uint128_t solution(char* str1, char* str2) {
__uint128_t result = 0;
for (unsigned long i = 0; i < 7; ++i) {
result +=
(((__uint128_t) str1[i]) << (8*i)) +
(((__uint128_t) str2[i]) << (8*(i + 8)));
}
return result;
}
#include <stdio.h>
#include <string.h>
#include <criterion/criterion.h>
__uint128_t solution(char* str1, char* str2);
Test(solution, hello_world) {
__uint128_t hello_r = solution("hello, ","world!\n");
cr_assert_eq(strcmp(((char*) &hello_r)+0,"hello, "),0);
cr_assert_eq(strcmp(((char*) &hello_r)+8,"world!\n"),0);
printf("%s", ((char*) &hello_r)+0);
printf("%s", ((char*) &hello_r)+8);
}
Given an array of numbers, return the total value of all the numbers.
function addArr(arr){
if(arr.length === 0) return null
let final = 0
arr.forEach(num => {
final += num
})
return final
}
const chai = require("chai");
const assert = chai.assert;
describe("Solution", function() {
it("should test for something", function() {
assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);
assert.strictEqual(addArr([1, 100]), 101)
assert.strictEqual(addArr([]), null)
});
});
The Caesar Cipher
or the shift cipher
, is a substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number position (i.e., shift) down the alphabet. This method is named after Julius Caesar, who used it during the Gallic Wars.
def caesar_cipher_encryption(message):
"""Caesar Cipher encryption"""
output = ''
for letter in message.upper():
if not letter.isalpha():
continue
cipher = ord(letter) + 1
if cipher > ord('Z'):
cipher = ord('A')
output += chr(cipher)
return output
def caesar_cipher_decryption(cipher):
output = ''
for letter in cipher.upper():
if not letter.isalpha():
continue
text = ord(letter) - 1
if text < ord('A'):
text = ord('Z')
output += chr(text)
return output
import codewars_test as test
from solution import caesar_cipher_encryption, caesar_cipher_decryption
@test.describe("Example")
def test_group():
@test.it("test case 1: Testing encryption function")
def test_case():
encryption_samples = (('CODEWARS', 'DPEFXBST'),
('ENCRYPTING', 'FODSZQUJOH'),
('CAESAR', 'DBFTBS'),
('CIPHER', 'DJQIFS'),
('KATA', 'LBUB'),
('ZULU', 'AVMV'),
('ALGORITHM', 'BMHPSJUIN'))
for s, e in encryption_samples:
test.assert_equals(caesar_cipher_encryption(s), e)
@test.it("test case 2: Testing decryption function")
def test_case():
decryption_sample = (('DPEFXBST', 'CODEWARS'),
('FODSZQUJOH', 'ENCRYPTING'),
('DBFTBS', 'CAESAR'),
('DJQIFS', 'CIPHER'),
('LBUB', 'KATA'),
('AVMV', 'ZULU'),
('BMHPSJUIN', 'ALGORITHM'))
for s, e in decryption_sample:
test.assert_equals(caesar_cipher_decryption(s), e)