Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad
Code
Diff
  • #include <fmt/format.h>
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <vector>
    #include <bitset>
    #include <algorithm>
    #include <cmath>
    #include <cctype>
    #include <random>
    using namespace std;
    
    
    static const string msgs[]={
      "Enter your guess (a 4-digit number with non-repeating digits): ",
      "Correct numbers: {} Correct position: {}",
      "Congratulations! You guessed the number {} correctly in {} attempts!",
      "Oups.. You could not guess the number in {} attempts!",
      "Invalid input. Please enter a 4-digit number with non-repeating digits."
    };
    
    // Initialize random number generator outside functions
    static random_device rd;  // Obtain a random number from hardware
    static mt19937 g(rd());   // Seed the generator
    
    string generateRandomNumber() {
        static string base{"0123456789"}; 
    
        // Shuffle digits
        shuffle(base.begin(), base.end(), g);
    
        // Return the first 4 digits as a string
        return base.substr(0,4);
    }
    
    bool hasDuplicateDigits(const string &number) {
        return accumulate(number.cbegin(),number.cend(), bitset<10>{0},
                             [](auto &bs,auto c) {return bs.set(c-'0');}
                             ).count()!=number.size();
    }
    
    bool isNumeric(const string &s) {
        return all_of(s.begin(), s.end(), ::isdigit);
    }
    
    string getUserGuess(istream &in = cin, ostream &out = cout) {
        string guess;
        out << msgs[0];
        in >> guess;
        return guess;
    }
    
    pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
        pair<int, int> ret {0,0};
      
        for (int i = 0; i < 4; ++i) {
            if (randomNumber[i] == userGuess[i]) {
                ++get<0>(ret), ++get<1>(ret);
            } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
                ++get<0>(ret);
            }
        }
    
        return ret;
    }
    
    int acceptGuess(const string &expected, istream &in = cin, std::ostream &out = std::cout, int maxattempts=20) {
        int attempts = 0;
        std::pair status{0,0};
        do {
            std::string userGuess = getUserGuess(in, out);
            if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
                out << msgs[4] << std::endl;
            } else {
              status = checkGuess(expected, userGuess);
              out << fmt::format(msgs[1],status.first,status.second) << std::endl;
            }
            attempts++;
        } while (status.second!=4 && attempts<maxattempts);
        
        if (attempts==maxattempts)
           out << fmt::format(msgs[3],attempts)<< std::endl;
        else 
           out << fmt::format(msgs[2],expected,attempts)<< std::endl;
        return attempts;
    }
    
    int _main(std::istream &in = std::cin, std::ostream &out = std::cout) {
        // Seed rand since random_shuffle _probably_ uses it.
        srand(static_cast<unsigned>(time(0)));
        std::string randomNumber = generateRandomNumber();
        acceptGuess(randomNumber, in, out);
        return 0;
    }
    
    • #include <fmt/format.h>
    • #include <iostream>
    • #include <cstdlib>
    • #include <ctime>
    • #include <vector>
    • #include <bitset>
    • #include <algorithm>
    • #include <cmath>
    • #include <cctype>
    • #include <random>
    • using namespace std;
    • static const std::string msgs[]={
    • static const string msgs[]={
    • "Enter your guess (a 4-digit number with non-repeating digits): ",
    • "Correct numbers: {} Correct position: {}",
    • "Congratulations! You guessed the number {} correctly in {} attempts!",
    • "Oups.. You could not guess the number in {} attempts!",
    • "Invalid input. Please enter a 4-digit number with non-repeating digits."
    • };
    • // Initialize random number generator outside functions
    • static std::random_device rd; // Obtain a random number from hardware
    • static std::mt19937 g(rd()); // Seed the generator
    • static random_device rd; // Obtain a random number from hardware
    • static mt19937 g(rd()); // Seed the generator
    • std::string generateRandomNumber() {
    • static std::string base{"0123456789"};
    • string generateRandomNumber() {
    • static string base{"0123456789"};
    • // Shuffle digits
    • std::shuffle(base.begin(), base.end(), g);
    • shuffle(base.begin(), base.end(), g);
    • // Return the first 4 digits as a string
    • return base.substr(0,4);
    • }
    • bool hasDuplicateDigits(const std::string &number) {
    • return std::accumulate(number.cbegin(),number.cend(), std::bitset<10>{0},
    • bool hasDuplicateDigits(const string &number) {
    • return accumulate(number.cbegin(),number.cend(), bitset<10>{0},
    • [](auto &bs,auto c) {return bs.set(c-'0');}
    • ).count()!=number.size();
    • }
    • bool isNumeric(const std::string &s) {
    • return std::all_of(s.begin(), s.end(), ::isdigit);
    • bool isNumeric(const string &s) {
    • return all_of(s.begin(), s.end(), ::isdigit);
    • }
    • std::string getUserGuess(std::istream &in = std::cin, std::ostream &out = std::cout) {
    • std::string guess;
    • string getUserGuess(istream &in = cin, ostream &out = cout) {
    • string guess;
    • out << msgs[0];
    • in >> guess;
    • return guess;
    • }
    • std::pair<int, int> checkGuess(const std::string &randomNumber, const std::string &userGuess) {
    • std::pair<int, int> ret {0,0};
    • pair<int, int> checkGuess(const string &randomNumber, const string &userGuess) {
    • pair<int, int> ret {0,0};
    • for (int i = 0; i < 4; ++i) {
    • if (randomNumber[i] == userGuess[i]) {
    • ++std::get<0>(ret), ++std::get<1>(ret);
    • } else if (std::count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
    • ++std::get<0>(ret);
    • ++get<0>(ret), ++get<1>(ret);
    • } else if (count(randomNumber.begin(), randomNumber.end(), userGuess[i])) {
    • ++get<0>(ret);
    • }
    • }
    • return ret;
    • }
    • int acceptGuess(const std::string &expected, std::istream &in = std::cin, std::ostream &out = std::cout, int maxattempts=20) {
    • int acceptGuess(const string &expected, istream &in = cin, std::ostream &out = std::cout, int maxattempts=20) {
    • int attempts = 0;
    • std::pair status{0,0};
    • do {
    • std::string userGuess = getUserGuess(in, out);
    • if (userGuess.length() != 4 || hasDuplicateDigits(userGuess) || !isNumeric(userGuess)) {
    • out << msgs[4] << std::endl;
    • } else {
    • status = checkGuess(expected, userGuess);
    • out << fmt::format(msgs[1],status.first,status.second) << std::endl;
    • }
    • attempts++;
    • } while (status.second!=4 && attempts<maxattempts);
    • if (attempts==maxattempts)
    • out << fmt::format(msgs[3],attempts)<< std::endl;
    • else
    • out << fmt::format(msgs[2],expected,attempts)<< std::endl;
    • return attempts;
    • }
    • int _main(std::istream &in = std::cin, std::ostream &out = std::cout) {
    • // Seed rand since random_shuffle _probably_ uses it.
    • srand(static_cast<unsigned>(time(0)));
    • std::string randomNumber = generateRandomNumber();
    • acceptGuess(randomNumber, in, out);
    • return 0;
    • }
Code
Diff
  • using System.Linq;
    
    public class Program
    {
      public static int FindSquaresInArray(int[] arr){
        return arr.Sum(x => x * x);
      } 
    }
    • using System.Linq;
    • public class Program
    • {
    • public static int FindSquaresInArray(int[] arr) => arr.Sum(x => x * x);
    • public static int FindSquaresInArray(int[] arr){
    • return arr.Sum(x => x * x);
    • }
    • }
Code
Diff
  • print(f'{chr(72)}{chr(101)}{2 * chr(108)}{chr(111)}{chr(44)}{chr(32)}{chr(87)}{chr(111)}{chr(114)}{chr(108)}{chr(100)}{2 * chr(33)}')
    • print(
    • chr(72) +
    • chr(101) +
    • 2 * chr(108) +
    • chr(111) +
    • chr(44) +
    • chr(32) +
    • chr(87) +
    • chr(111) +
    • chr(114) +
    • chr(108) +
    • chr(100) +
    • 2 * chr(33)
    • )
    • print(f'{chr(72)}{chr(101)}{2 * chr(108)}{chr(111)}{chr(44)}{chr(32)}{chr(87)}{chr(111)}{chr(114)}{chr(108)}{chr(100)}{2 * chr(33)}')

Constructor situation is a bit better now. Thunks are now copyable and are more flexibly constructible.

Background

C++ recreation of the Thunk API from this Kata.

Test cases are included to verify if it works as expected. The full extent of the class is used in those test cases.

Code
Diff
  • ;
    • //
    • ;

This algorithm of 'sum()' is SFINAE-friendly. By first testing if the corresponding types can be added with the type trait 'has_plus' because some types doesn't have the 'operator+' defined. If for any reason, the types doesn't have the addition operator available, the function will be disabled via 'std::enable_if' and will be discarded by SFINAE. In constrast, if the types can be added, the 'std::enable_if' trait will enable the function and returning the corresponding type of add the two types with the 'plus_result' trait.

Code
Diff
  • #include <vector>
    #include <numeric>
    #include <type_traits>
    #include <utility>
    
    namespace
    {
      template<typename, typename, typename = void>
      struct has_plus : std::false_type {};
    
      template<typename T1, typename T2>
      struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    
      template<typename T1, typename T2>
      inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    
      template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
      struct plus_result
      {
        using type = decltype(std::declval<T1>() + std::declval<T2>());
      };
      
      template<typename T1, typename T2>
      struct plus_result<T1, T2, false> {};
      
      template<typename T1, typename T2>
      using plus_result_t = typename plus_result<T1, T2>::type;
      
      template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
      plus_result_t<T, T> sum(const std::vector<T>& v)
      {
        return std::accumulate(v.cbegin(), v.cend(), T{});
      }
    }
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • #include <utility>
    • template <typename T> using add_t=decltype(T() + T());
    • namespace
    • {
    • template<typename, typename, typename = void>
    • struct has_plus : std::false_type {};
    • template <typename T = double>
    • add_t<T> sum(const std::vector<T>& v) {
    • return std::accumulate(v.cbegin(),v.cend(),add_t<T>{0});
    • template<typename T1, typename T2>
    • struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    • template<typename T1, typename T2>
    • inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    • template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
    • struct plus_result
    • {
    • using type = decltype(std::declval<T1>() + std::declval<T2>());
    • };
    • template<typename T1, typename T2>
    • struct plus_result<T1, T2, false> {};
    • template<typename T1, typename T2>
    • using plus_result_t = typename plus_result<T1, T2>::type;
    • template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
    • plus_result_t<T, T> sum(const std::vector<T>& v)
    • {
    • return std::accumulate(v.cbegin(), v.cend(), T{});
    • }
    • }