My calculator is behaving weird. Sometimes when I type in an
8 it displays a
2. And sometimes when I type in a
6 it displays a
+. Some buttons are mixed up!
Could anyone help me determine which?
Input: List of incorrect equations, with correct results.
Output: The two buttons that are swapped.
An input could be:
123 = 3 8423 = 252 4+4 = 8 4*7-10 = 417 9/3 = 3 42-9 = -36
For which the expected outputs are:
Why? Because ALL the equations would be correct if we swap the 2's and *'s:
1*3 = 3 84*3 = 252 4+4 = 8 427-10 = 417 9/3 = 3 4*-9 = -36
- Input can be in any reasonable format. Can be a single string with space delimited; a string-list or -array; a list with equations and another list with the correct results. Your call. Please state which input format you've used!
NOTE: This also means you are allowed to input the test case
-5 - -15. However, a number resulting in
--should either be inputted without spaces or with a space between every digit. So test case
9119can be inputted like
9 1 1 9(reason
91 19isn't allowed is because you can then be guided by the space for finding
- -). So spaces are (somewhat) optional and allowed.
- Output format can be in any reasonable format as well. Can be two characters; a single two-character string; a string-list containing the two characters. Your call. Again, please state which output format you've used!
- You are allowed to use any distinct 14 outputs that map to
0123456789+-*/. So you are even allowed to output two distinct integers if you want to (again, please specify the mapping you've used, if any).
- You only have to support integers. So there won't be any test cases like
- Arithmetic operands you'll have to support: addition (
+); subtraction (
-); multiplication (
·); division (
÷). (NOTE: Characters between parenthesis are only added as clarification.)
- You'll have to support negative numbers. This means
-can be interpreted in the equation as both a mathematical operand or a negative indicator.
- You can assume the given incorrect equations and supposed correct equations are always valid (so there won't be things like
- The incorrect input-equations can contain a division by 0, but the corrected and expected equations will never contain division by 0.
- The incorrect input-equations can already be correct even if you swap the intended buttons back.
- A given input equation can be irrelevant for the buttons to swap (like the
9/3=3equations, with the swapped buttons
- You can assume there will always be only one possible swap that can be made with the given test cases.
- Both buttons to swap will always be present in at least one of the incorrect equations.
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
- Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Input: 123 = 3 8423 = 252 4+4 = 8 4*7-10 = 417 9/3 = 3 42-9 = -36 Output: 2 * Input: 4/2 = 6 3/0 = 3 0/8+2 = 4 95-5 = 90 4+2 = 2 Output: + / Input: 7+4 = 11 5-15 = 46 212-23 = -2121 Output: 1 - Input: 4+8/2-9*1 = -5 99/3-13 = 20 1+2+3+4 = 10 4-3-2-1 = -6 Output: 2 4 Input: 18/18 = 1 98-8 = 90 55*88 = 4840 -5--15 = 10 Ouput: 5 8 Input: 9119 = 18 5-3 = 513 8*-9 = 152 13116/3 = -1 Output: 1 -