Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Created February 8, 2024 22:35
Show Gist options
  • Select an option

  • Save youssef3wi/76922f00d21376b197b40e9a9d0939fc to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/76922f00d21376b197b40e9a9d0939fc to your computer and use it in GitHub Desktop.
My daughter is currently learning addition at school. She sometimes gets things wrong when summing up two digits.
/**
* My daughter is currently learning addition at school. She sometimes gets things
* wrong when summing up two digits.
* <p>
* Given the two initial numbers and her calculated result, you have to check if is
* correct.
* <ul>
* <li>If yes, output the string "ok"</li>
* <li>If no, return the digit index on where she got it wrong (converted to a string).
* Unit digit has the index "0", tens digit has the index "1", etc.</li>
* </ul>
* My daughter is not bad, she gets at most only one digit wrong.
* <p>
* She just began to learn addition, so there is no carry in any digit of all the numbers
* she has to sum up.
* <p>
* All the numbers have a maximum of 5 digits, but the two initial numbers and the
* result may not have the same number of digits.
* <p>
* <b>Example</b>
* <p>
* The two initial values are <mark>123</mark> and <mark>672</mark>. Her calculated result is <mark>785</mark>.
* <ul>
* <li>Unit digit check: <mark>3 + 2 = 5</mark>. ok</li>
* <li>Tens digits check: <mark>2 + 7 = 9</mark>, but my daughter wrote 8. It's wrong.</li>
* <li>Hundreds digit check: <mark>1 + 6 = 7</mark>. ok</li>
* </ul>
* You must output "1", which is the index of the tens digit.
* <table border="1px">
* <tr>
* <td>Parameters</td>
* <td>Return value</td>
* </tr>
* <tr>
* <td>123</td>
* <td>1</td>
* </tr>
* <tr>
* <td>672</td>
* </tr>
* <tr>
* <td>785</td>
* </tr>
* </table>
*/
public class DaughterAddition {
/**
* @param valueOne The first operand of the addition. Positive or zero.
* @param valueTwo The second operand of the addition. Positive or zero.
* @param calcResult the addition result calculated by my daughter (maybe wrong).
* @return the text 'ok' if the calculated result is correct. The index digit
* where the error is, if the result is not correct.
*/
public static String compute(int valueOne, int valueTwo, int calcResult) {
if (valueOne + valueTwo == calcResult) {
return "ok";
}
String operandOne = valueOne + "";
String operandTwo = valueTwo + "";
String resultStr = calcResult + "";
String indexOfWrong = "0";
String longestOperand = operandOne;
if (operandTwo.length() > operandOne.length()) {
longestOperand = operandTwo;
}
for (int idx = longestOperand.length() - 1; idx >= 0; idx--) {
int digitOperandOne = 0;
if (operandOne.length() - 1 >= idx) {
digitOperandOne = Integer.parseInt(operandOne.charAt(idx) + "");
}
int digitOperandTwo = 0;
if (operandTwo.length() - 1 >= idx) {
digitOperandTwo = Integer.parseInt(operandTwo.charAt(idx) + "");
}
int digitResult = Integer.parseInt(resultStr.charAt(idx) + "");
int actualResult = digitOperandOne + digitOperandTwo;
if (digitResult != actualResult) {
// Check if two digits
int unitOfResult = actualResult % 10;
if (unitOfResult != 0) {
if (unitOfResult != digitResult) {
indexOfWrong = (longestOperand.length() - idx - 1) + "";
} else {
// Update the next digit if exists
if (operandOne.length() - 1 > idx && (digitOperandOne + unitOfResult) <= 9) {
operandOne = operandOne.substring(0, idx - 1) + (actualResult - unitOfResult + "") + operandOne.substring(idx + 1);
} else if (operandTwo.length() - 1 > idx && (digitOperandTwo + unitOfResult) <= 9) {
operandTwo = operandTwo.substring(0, idx - 1) + (actualResult - unitOfResult + "") + operandTwo.substring(idx + 1);
}
}
} else {
indexOfWrong = (longestOperand.length() - idx - 1) + "";
}
}
}
return indexOfWrong;
}
}
@Enswerbee
Copy link
Copy Markdown

Enswerbee commented Apr 22, 2025

My daughter is just now learning how to add numbers without carrying over, and it is not easy for her. We solve examples together, but she often gets confused about the digits. Unfortunately, I work a lot now, so I can't do lessons with her often. I use https://domyhomework.net/do-my-math-homework/ to get quality assistance with my homework. It saves time and reduces stress. My daughter maintains a strong interest in learning. She always has support. I hope that over time it will become easier for her and she will master this topic with confidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment