28
1
Introduction
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?
Challenge:
Input: List of incorrect equations, with correct results.
Output: The two buttons that are swapped.
For example:
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: 2 and *.
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
Challenge rules:
- 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--15as-5- -15or-5 - -15. However, a number resulting in--should either be inputted without spaces or with a space between every digit. So test case9119can be inputted like9119or9 1 1 9(reason91 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
1/8=0.125or1/8=0. - Arithmetic operands you'll have to support: addition (
+); subtraction (-); multiplication (*or×or·); division (/or÷). (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
4-/2or9+-+8for example). - 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
4+4=8and9/3=3equations, with the swapped buttons2and*). - 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.
General rules:
- 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.
Test cases:
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 -
2"real division" means we have to support floats? – Erik the Outgolfer – 2017-09-04T09:30:24.023
@EriktheOutgolfer Oops.. Copied that from a previous arithmetic challenge of mine. Removed, and as answer to your question, no you only have to deal with integers. – Kevin Cruijssen – 2017-09-04T10:35:39.743
Oh, so we'll never have to deal with something like
4/3=1right? – Erik the Outgolfer – 2017-09-04T10:38:04.837@EriktheOutgolfer That's right. I'll specify that more clearly in the challenge. – Kevin Cruijssen – 2017-09-04T10:38:26.537
1I'd suggest a test case where a correct equation contains
--. For example1991 = 2, -/3 = 3. (Many languages confuse this with the decrement operator.) – nwellnhof – 2017-09-04T15:17:17.630@nwellnhof Added a test case with
9119=18->9--9=18. And also specified that you are allowed to input the test cases with spaces, since I've seen multiple people add code to change-5--15to-5- -15programmatically, while just inputting it with a space is perfectly allowed. – Kevin Cruijssen – 2017-09-04T16:11:34.730Is it also allowed to input
9119as91 19? – nwellnhof – 2017-09-04T19:05:05.210@nwellnhof Hmm.. Why not. If you want you can even add spaces between every number. I was doubting for a moment, since technically
9119is one number, and you don't know beforehand if it should be replaced with--. If I had a test case like9119=9229and2+2=2[1 2], it would be weird to input it like91 19. On the other hand, you don't need to output92 29, but only1 2(the pair to swap), so yes, you are allowed to input it like91 19. – Kevin Cruijssen – 2017-09-04T19:43:21.7971The problem is that adding a space in
91 19if the solution is9--9and no space in9119if the solution is9229requires knowledge about the solution when creating the test cases. If this were allowed, I could simply add a space only before the swapped characters and the solution could be immediately derived from the test case. – nwellnhof – 2017-09-04T19:58:16.253@nwellnhof Hmm, good point, hadn't thought of that. I actually meant you should either always add a space in numbers like
9119, or never. Not sometimes based on the test case. Will have to think about it for a moment, but maybe9119as single number is a more natural input-format (although spaces between every character would still be allowed). – Kevin Cruijssen – 2017-09-04T20:32:26.307Maybe we can say that your calculator just has a display problem but it still formatting the input correctly with a space before the unary
-, no matter what character is shown instead? ;-) – Arnauld – 2017-09-04T21:42:50.6671Is evaluation left-to-right, or
*and/before+and binary-? – aschepler – 2017-09-04T22:03:31.883@aschepler It's
*and/before+and-. So you'd input the entire equation, and then click=to get the result. – Kevin Cruijssen – 2017-09-05T06:39:29.223@Arnauld I can understand why you'd say that. ;) But there are a few minor problems: you always know it's
- -when a space is present between two equal characters/digits. And if I would add spaces between every mathematical operand (+-*/), you can be guided by the spaces regarding solving the swaps, as nwellnhof mentioned above. So I thought about it carefully, but9119should be either9119or9 1 1 9. (You can easily alter your answer anyway by adding a.replace('--','- -').) Sorry that I said it was allowed in my comment before.. I wish these things were said in the Sandbox :( – Kevin Cruijssen – 2017-09-05T06:50:07.147