}
Evaluating Simple Algebra
Given a string containing an algebraic equation, calculate and return the value of x. You'll only be given equations for simple addition and subtraction.
Examples
evalAlgebra("2 + x = 19") ➞ 17
evalAlgebra("4 - x = 1") ➞ 3
evalAlgebra("23 + 1 = x") ➞ 24
Notes
There are spaces between every number and symbol in the string.
x may be a negative number.
in C++
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Given a string containing a simple algebraic equation, calculate and return the value of x. The equations will only be for simple addition and subtraction.
Solution
We can solve this problem by first parsing the input string to identify the numbers and the operation. We can then perform the necessary arithmetic operation to isolate x.
cpp
#include <iostream>
#include <sstream>
#include <string>
/**
* Evaluates a simple algebraic equation and returns the value of x.
*
* @param eq A string containing the algebraic equation.
* @return The value of x.
*/
int evalAlgebra(const std::string& eq) {
// Split the equation into three parts: left operand, operator, and right operand
size_t equalIndex = eq.find('=');
std::string left = eq.substr(0, equalIndex);
std::string right = eq.substr(equalIndex + 2);
int leftOperand, rightOperand;
char operator_;
// Parse the left operand
size_t spaceIndex = left.find(' ');
if (spaceIndex != std::string::npos) {
leftOperand = std::stoi(left.substr(0, spaceIndex));
operator_ = left[spaceIndex + 1];
// Check if x is on the left side
if (left[spaceIndex + 3] == 'x') {
// x is on the left side, perform the inverse operation
rightOperand = std::stoi(right);
switch (operator_) {
case '+':
return rightOperand - leftOperand;
case '-':
return leftOperand - rightOperand;
}
}
}
// Parse the right operand
spaceIndex = right.find(' ');
if (spaceIndex != std::string::npos) {
rightOperand = std::stoi(right.substr(0, spaceIndex));
operator_ = right[spaceIndex + 1];
// Check if x is on the right side
if (right[spaceIndex + 3] == 'x') {
// x is on the right side, perform the operation
leftOperand = std::stoi(left);
switch (operator_) {
case '+':
return leftOperand - rightOperand;
case '-':
return rightOperand - leftOperand;
}
}
} else {
// x is not on the right side, but on the left, so we need to parse the left operand again
spaceIndex = left.find(' ');
leftOperand = std::stoi(left.substr(0, spaceIndex));
operator_ = left[spaceIndex + 1];
if (left[spaceIndex + 3] == 'x') {
// x is on the left side, perform the inverse operation
rightOperand = std::stoi(right);
switch (operator_) {
case '+':
return rightOperand - leftOperand;
case '-':
return leftOperand - rightOperand;
}
}
}
}
int main() {
std::cout << evalAlgebra("2 + x = 19") << std::endl; // 17
std::cout << evalAlgebra("4 - x = 1") << std::endl; // 3
std::cout << evalAlgebra("23 + 1 = x") << std::endl; // 24
return 0;
}
This code uses the std::stoi function to parse the numbers from the input string. It then performs the necessary arithmetic operation to isolate x based on the operator and the position of x in the equation. The code includes comments to explain the logic and uses proper naming conventions for readability.
“int evalAlgebra(std::string eq) { } Evaluating Simple Algebra Given a string containing….” Conversation with llama-3.1-70b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m8510hr8twwd2x72n2pragg3