I am trying to do a coding challenge where we have to shorten and minimise direction instructions. I am using an unordered map prior to implementing the while loop. The rest of the code works as expected but the while loop cannot be reached so I cannot output an appropriate value.
std::vector<std::string> dirReduc(std::vector <std::string>& arr)
{
enum Direction
{
NORTH = 1, SOUTH = -1,
EAST = 1, WEST = -1
};
int x=0, y=0;
std::unordered_map<std::string, Direction> uMapDir = { {"North",NORTH},{"NORTH",NORTH},
{"South",SOUTH},{"SOUTH",SOUTH},
{"East",EAST},{"EAST",EAST},
{"West",WEST},{"WEST",WEST} };
for (std::vector<std::string> ::const_iterator i =arr.begin(); i!=arr.end() ; i ++)
{
if ((*i=="North")||(*i == "NORTH")||(*i=="South")|| (*i == "SOUTH"))
{
y += uMapDir[*i];
}
else
{
x += uMapDir[*i];
}
}
std::vector<std::string> outPutArr;
while ((x!=0)&&(y!=0))
{
if (x<0)
{
x++;
outPutArr.push_back("WEST");
}
else
{
x--;
outPutArr.push_back("EAST");
}
if (y < 0)
{
y++;
outPutArr.push_back("SOUTH");
}
else
{
y--;
outPutArr.push_back("NORTH");
}
}
return outPutArr;
}
Aucun commentaire:
Enregistrer un commentaire