Question: please write in c++.ONLY NEED TO implement the function puzzleSolver in proj1.cpp. NEED TO USE RECURSION.M apping could only be from a single digit to

please write in c++.ONLY NEED TO implement the function puzzleSolver in proj1.cpp. NEED TO USE RECURSION.Mapping could only be from a single digit to a letter

In a summation puzzle, you are given three strings of the form POT + PAN = BIB. Typically each is a word, often with a theme to the three chosen. Your goal is to assign a distinct digit to each letter in the equation in order to make the resulting true. For example, if the puzzle is POT + PAN = BIB, the mapping P:2, O:3, T:1, A:7, N:4, B:5, I:0 will solve this, as 231 + 274 = 505.

You are required to implement the function puzzleSolver in proj1.cpp. This function should return true if, and only if, the puzzle is solvable: that is, if there is a mapping of the letters that appear in the three strings to distinct digits such that the sum of the first two is the third. No string will have a value larger than 4,294,967,295 in its correct substitution, nor will the addition have any integer-overflow to check for. If you do not know what integer overflow is, you do not need to check during this assignment (although its worth knowing in general).

For this project, you have a few requirements: You must implement the function puzzleSolver in proj1.cpp. You may assume it is called with three valid non-empty strings as parameters and with an otherwise empty map. The strings will always consist only of all-capital letters. Your solution must explicitly use recursion in a meaningful way towards solving the problem. You may not solve this by using a function like std::next_permutation (from ) to enumerate possibilities. The function puzzleSolver itself need not be recursive if you would prefer to have a helper function that is. The function must return a boolean indicating whether or not the puzzle has a solution. If the puzzle does not have a solution, your function should return false. If the puzzle does have a solution your function should return true and have the map parameter containing the mapping needing to verify. That is, the four parameters to the puzzleSolver function need to be such that a correct solution to project 0 would return true with those parameters. If there are multiple solutions, returning any of them is fine.

code: proj1.cpp:

#include "proj1.hpp"

#include

#include

bool puzzleSolver(std::string s1, std::string s2, std::string s3, std::map<char, unsigned> & mapping)

{

return true; // This is not the correct general solution.

}

code without using RECURSION:(You do not need to do anything with this code,but you can modify and add recursive function to achieve the goal based on this code

#include #include #include #include "proj0.hpp"

using namespace std;

bool verifySolution(std::string s1, std::string s2, std::string s3, const std::map &mapping) { //decalre string variable to hold corresponding number value std::string mapValueS1 = "", mapValueS2 = "", mapValueS3 = ""; //Iterate through s1 and get the s1 value for(std::string::size_type i = 0; i < s1.size(); ++i) { //Convert the char to uppercase as mapping has value for upper case only mapValueS1 = mapValueS1 + std::to_string(mapping.at(toupper(s1[i]))); } //Iterate through s2 and get the s2 value for(std::string::size_type i = 0; i < s2.size(); ++i) { //Convert the char to uppercase as mapping has value for upper case only mapValueS2 = mapValueS2 + std::to_string(mapping.at(toupper(s2[i]))); } //Iterate through s3 and get the s3 value for(std::string::size_type i = 0; i < s3.size(); ++i) { //Convert the char to uppercase as mapping has value for upper case only mapValueS3 = mapValueS3 + std::to_string(mapping.at(toupper(s3[i]))); } //Convert string number values to integer int mapValueTotalS1 = std::stoi(mapValueS1); int mapValueTotalS2 = std::stoi(mapValueS2); int mapValueTotalS3 = std::stoi(mapValueS3); //Display the values cout << "s1: " << mapValueS1 << endl; cout << "s2: " << mapValueS2 << endl; cout << "s3: " << mapValueS3 << endl; //return true if s1+s2 = s3 return (mapValueTotalS1 + mapValueTotalS2) == mapValueTotalS3; }

test.cpp (just for test):

namespace{

TEST(SimpleCases, PotPanBib){

std::map<char, unsigned> puzzle;

bool p1 = puzzleSolver("POT", "PAN", "BIB", puzzle);

EXPECT_TRUE( p1 && gradeYesAnswer("POT", "PAN", "BIB", puzzle) ) ;

}

TEST(SimpleCases, NeatFind){

std::map<char, unsigned> puzzle;

bool p1 = puzzleSolver("UCI", "ALEX", "MIKE", puzzle);

EXPECT_TRUE( p1 && gradeYesAnswer("UCI", "ALEX", "MIKE", puzzle) );

} // end test two, "NeatFind"

TEST(SimpleCases, FirstNo){

std::map<char, unsigned> puzzle;

bool p1 = puzzleSolver("LARRY", "CAREER", "LEGEND", puzzle);

EXPECT_FALSE( p1 );

}

} // end namespace

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!