Ad

func to multiply two numbers

int multiply(int a, int b) {
  return a * b;
}
// TODO: Replace examples and use TDD by writing your own tests

Describe(any_group_name_you_want)
{
    It(should_do_something)
    {
        int result = multiply(10,2);
        Assert::That(result, Equals(20));
    }
};
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;
    • }