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.
Data.Set is better. It's safer than head/tail and faster than nub
(O(n^2)). For instance, size
is O(1) and fromList
is O(n*log n).
module AllEqual where import Data.Set (fromList, size) allEqual :: [Int] -> Bool allEqual xs = (size $ fromList xs) <= 1
- module AllEqual where
import Data.List- import Data.Set (fromList, size)
- allEqual :: [Int] -> Bool
allEqual xs = length (nub xs) <= 1- allEqual xs = (size $ fromList xs) <= 1
You can declare Array with running numbers up by using [...Array(n).keys]
. Then if you want it start from 1
, you just increase array size then slice it of.
const isPerfect = n => [...Array(n).keys()].slice(1).filter(e => !(n % e)).reduce((a, b) => a + b) === n
- const isPerfect = n =>
Array(n - 1).fill(1).map((e, i) => e + i).filter(e => n % e === 0).reduce((a, b) => a + b) === n- [...Array(n).keys()].slice(1).filter(e => !(n % e)).reduce((a, b) => a + b) === n
# Method with three named arguments # https://robots.thoughtbot.com/ruby-2-keyword-arguments def c one: "one", two: "tow", three: "three" p "one: %s two: %s three: %s" % [one,two,three] end c # calling without arguments c two: "TWO2" # calling with one argument c two: "2", one: 1111111 # Calling with 2 arguments with no order c(one: 1, two: 2, three: 3) # Passing 3 Named arguments c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order) begin c fore: "4" # calling with wrong argument rescue Exception => e p e.message p e.class end begin #c (fore:"4") # calling with wrong argument # this lead to syntaxix error rescue Exception => e puts("#{e.message}\n#{e.class}") end hash = {one: 'One', two: 'Two', three: 'Three'} c hash # calling with hash hash = {two: 'Two', three: 'Three', one: 'One', } c hash # calling with hash mess in order hash = { one: 'One', } c hash # calling with hash where not all argumetns listed # Super syntax hash = { two: '222', } c one: 1, **hash # c one: 1, hash # <== leads to error begin hash = { one: 'One', fore: "4" } c hash # calling with hash that contain pair unlisetd in named arguments rescue Exception => e puts("#{e.message}\n#{e.class}") end
- # Method with three named arguments
- # https://robots.thoughtbot.com/ruby-2-keyword-arguments
- def c one: "one", two: "tow", three: "three"
- p "one: %s two: %s three: %s" % [one,two,three]
- end
- c # calling without arguments
- c two: "TWO2" # calling with one argument
- c two: "2", one: 1111111 # Calling with 2 arguments with no order
- c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
- c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
- begin
- c fore: "4" # calling with wrong argument
- rescue Exception => e
- p e.message
- p e.class
- end
- begin
- #c (fore:"4") # calling with wrong argument
- # this lead to syntaxix error
- rescue Exception => e
p e.messagep e.class- puts("#{e.message}\n#{e.class}")
- end
- hash = {one: 'One', two: 'Two', three: 'Three'}
- c hash # calling with hash
- hash = {two: 'Two', three: 'Three', one: 'One', }
- c hash # calling with hash mess in order
- hash = { one: 'One', }
- c hash # calling with hash where not all argumetns listed
- # Super syntax
- hash = { two: '222', }
- c one: 1, **hash
- # c one: 1, hash # <== leads to error
- begin
- hash = { one: 'One', fore: "4" }
- c hash # calling with hash that contain pair unlisetd in named arguments
- rescue Exception => e
p e.messagep e.class- puts("#{e.message}\n#{e.class}")
- end
This is a response to the kumite https://www.codewars.com/kumite/5a4edf17d8e145968c00015e to show that it has an elegant solution for ruby using the compiled bash utility "tr"
It's my first kumite so I'm not sure if this is how to use it. Don't mind if I do!
def pattern a,b a.tr(a,b)==b && b.tr(b,a)==a end
def pattern(p,st):def encode(s):dic={} # dictionary to hold values from the input stringnum=0sn="" # a numerical string to hold the encodingfor i in s:if i not in dic:dic[i]=numnum+=1sn+=str(dic[i])return snreturn encode(st)==encode(p)- def pattern a,b
- a.tr(a,b)==b && b.tr(b,a)==a
- end
describe "Basic Tests" do it "should pass basic tests" do Test.assert_equals(pattern("abab", "1010"), true); Test.assert_equals(pattern("mmmm", "0000"), true); Test.assert_equals(pattern("a", "1"), true); Test.assert_equals(pattern("1010111", "0101000"), true); Test.assert_equals(pattern("777a9", "aba"), false); Test.assert_equals(pattern("123au", "00000"), false); end end
test.assert_equals(pattern("abab", "1010"), True )test.assert_equals(pattern("mmmm", "0000"), True )test.assert_equals(pattern("a", "1"), True )test.assert_equals(pattern("1010111", "0101000"), True )test.assert_equals(pattern("777a9", "aba"), False )test.assert_equals(pattern("123au", "00000"), False )- describe "Basic Tests" do
- it "should pass basic tests" do
- Test.assert_equals(pattern("abab", "1010"), true);
- Test.assert_equals(pattern("mmmm", "0000"), true);
- Test.assert_equals(pattern("a", "1"), true);
- Test.assert_equals(pattern("1010111", "0101000"), true);
- Test.assert_equals(pattern("777a9", "aba"), false);
- Test.assert_equals(pattern("123au", "00000"), false);
- end
- end
Reduced, printing only simple stuff.
bool alwaysTrue(int, int, int) { return true; }
template <class... Args>bool alwaysTrue(Args&&...) {- bool alwaysTrue(int, int, int) {
- return true;
- }
template <class... Args> void printInput(const char* name, const Args&... args); #define alwaysTrue(...) (printInput("alwaysTrue", __VA_ARGS__), alwaysTrue(__VA_ARGS__)) Describe(codewarriors) { It(do_print_arguments) { Assert::That(alwaysTrue(1, 2, 3), Is().True()); Assert::That(alwaysTrue(4, 5, 6), Is().True()); } }; #include <iostream> template <class Arg> void printArg(const Arg& arg) { std::cout << arg; } template <class Arg> void printArgs(const Arg& arg) { printArg(arg); } template <class Arg, class... Args> void printArgs(const Arg& arg, const Args&... args) { printArgs(arg); std::cout << ", "; printArgs(args...); } template <class... Args> void printInput(const char* name, const Args&... args) { std::cout << name << "("; printArgs(args...); std::cout << ")\n"; }
#include <list>#include <tuple>#include <utility>#include <vector>- template <class... Args> void printInput(const char* name, const Args&... args);
- #define alwaysTrue(...) (printInput("alwaysTrue", __VA_ARGS__), alwaysTrue(__VA_ARGS__))
- Describe(codewarriors) {
- It(do_print_arguments) {
using vi = std::vector<int>;using li = std::list<int>;using vvi = std::vector<std::vector<int>>;using vs = std::vector<std::string>;Assert::That(alwaysTrue(vi{0, 1, 2}), Is().True());Assert::That(alwaysTrue(li{0, 1, 2}), Is().True());Assert::That(alwaysTrue((int[]){0, 1, 2}), Is().True());Assert::That(alwaysTrue(vvi{{0, 1, 2}, {0, 1}, {1}}), Is().True());Assert::That(alwaysTrue("012"), Is().True());Assert::That(alwaysTrue((const char*)"012"), Is().True());Assert::That(alwaysTrue(std::string{"012"}), Is().True());Assert::That(alwaysTrue(vi{0, 1, 2}, "012", 123), Is().True());Assert::That(alwaysTrue(vs{"a", "b", ""}, 'c'), Is().True());Assert::That(alwaysTrue("pair", std::make_pair(1, "2")), Is().True());Assert::That(alwaysTrue("tuple", std::make_tuple(1, "2", '3', vi{1})), Is().True());- Assert::That(alwaysTrue(1, 2, 3), Is().True());
- Assert::That(alwaysTrue(4, 5, 6), Is().True());
- }
- };
#include <cstddef>#include <algorithm>- #include <iostream>
#include <iterator>#include <type_traits>#include <utility>template <class... Ts> struct make_void { using type = void; };template <class... Ts> using void_t = typename make_void<Ts...>::type;template <class, class = void> struct is_container : std::false_type { };template <class T> struct is_container<T, void_t<decltype(std::cbegin(std::declval<T>()))>> : std::true_type { };template <class, class = void> struct is_string : std::false_type { };template <class T> struct is_string<T, std::enable_if_t<std::is_same<decltype(*std::cbegin(std::declval<T>())), const char&>::value>> : std::true_type { };template <class Arg> void printArgs(const Arg& arg);template <class Arg, class... Args> void printArgs(const Arg& arg, const Args&... args);template <class Arg, std::enable_if_t<!is_container<Arg>::value && !is_string<Arg>::value>* = nullptr>- template <class Arg>
- void printArg(const Arg& arg) {
- std::cout << arg;
- }
void printArg(const char arg) {std::cout << "'" << arg << "'";}void printArg(const char* const arg) {std::cout << '"' << arg << '"';}template <class Arg, std::enable_if_t<is_string<Arg>::value>* = nullptr>void printArg(const Arg& arg) {std::cout << '"';std::copy(std::cbegin(arg), std::cend(arg), std::ostreambuf_iterator<char>(std::cout));std::cout << '"';}template <class T1, class T2>void printArg(const std::pair<T1, T2>& arg) {std::cout << "{";printArgs(arg.first);std::cout << ", ";printArgs(arg.second);std::cout << "}";}#include <tuple>template <typename T, std::size_t... Is>void printArgsApplyImpl(const T& arg, std::index_sequence<Is...>) {printArgs(std::get<Is>(arg)...);}template <typename... Ts>void printArgsApply(const std::tuple<Ts...>& arg) {printArgsApplyImpl(arg, std::make_index_sequence<std::tuple_size<std::tuple<Ts...>>::value>());}template <class... Ts>void printArg(const std::tuple<Ts...>& arg) {std::cout << "{";printArgsApply(arg);std::cout << "}";}template <class Arg, std::enable_if_t<is_container<Arg>::value && !is_string<Arg>::value>* = nullptr>void printArg(const Arg& arg) {const auto b = std::cbegin(arg), e = std::end(arg);std::cout << "{";for (auto i = b; i != e; ++i) {if (i != b)std::cout << ", ";printArgs(*i);}std::cout << "}";}- template <class Arg>
- void printArgs(const Arg& arg) {
- printArg(arg);
- }
- template <class Arg, class... Args>
- void printArgs(const Arg& arg, const Args&... args) {
- printArgs(arg);
- std::cout << ", ";
- printArgs(args...);
- }
- template <class... Args>
- void printInput(const char* name, const Args&... args) {
- std::cout << name << "(";
- printArgs(args...);
- std::cout << ")\n";
- }