16
1
Introduction:
Let's take a look at a standard Calculator in Windows:
For this challenge, we'll only look at the following buttons, and ignore everything else:
7 8 9 /
4 5 6 *
1 2 3 -
0 0 . +
Challenge:
Input:
You will receive two inputs:
- One is something to indicate the rotation in increments of 90 degrees
- The other is a list of coordinates representing the buttons pressed on the rotated calculator.
Based on the first input, we rotate the layout mentioned above clockwise in increments of 90 degrees. So if the input is 0 degrees
, it remains as is; but if the input is 270 degrees
, it will be rotated three times clockwise (or once counterclockwise). Here are the four possible lay-outs:
Default / 0 degrees:
7 8 9 /
4 5 6 *
1 2 3 -
0 0 . +
90 degrees clockwise:
0 1 4 7
0 2 5 8
. 3 6 9
+ - * /
180 degrees:
+ . 0 0
- 3 2 1
* 6 5 4
/ 9 8 7
270 degrees clockwise / 90 degrees counterclockwise:
/ * - +
9 6 3 .
8 5 2 0
7 4 1 0
The second input is a list of coordinates in any reasonable format †. For example (0-index 2D integer-array):
[[1,2],[2,3],[0,3],[1,0],[1,1]]
Output:
We output both the sum, as well as the result (and an equal sign =
).
Example:
So if the input is 270 degrees
and [[1,2],[2,3],[0,3],[1,0],[1,1]]
, the output will become:
517*6=3102
Challenge rules:
- † The inputs can be in any reasonable format. The first input can be
0-3
,1-4
,A-D
,0,90,180,270
, etc. The second input can be a 0-indexed 2D array, 1-indexed 2D array, a String, list of Point-objects, etc. Your call. It's even possible to swap the x and y coordinates compared to the example inputs given. Please state which input formats you've used in your answer! - You are allowed to add spaces (i.e.
517 * 6 = 3102
) if you want to. - You are allowed to add trailing zeros after the comma, to a max of three (i.e.
3102.0
/3102.00
/3102.000
instead of3102
or0.430
instead of0.43
). - You are not allowed to add parenthesis in the output, so
(((0.6+4)-0)/2)/4=0.575
is not a valid output. - You are allowed to use other operand-symbols for your language. So
×
or·
instead of*
; or÷
instead of/
; etc. - Since a calculator automatically calculates when inputting an operand, you should ignore operator precedence! So
10+5*3
will result in45
((10+5)*3=45
), not25
(10+(5*3)=25
)
(i.e.10
→+
→5
→*
(it now displays 15 in the display) →3
→=
(it now displays the answer45
)). Keep this in mind when usingeval
and similar functions on the resulting sum. - There won't be any test cases for division by 0.
- There won't be any test cases with more than three decimal digits as result, so no need for rounding the result.
- There won't be any test cases where multiple operands follow each other, or where two dots follow each other.
- There won't be any test cases for negative numbers. The minus-sign (
-
) will only be used as operand, not as negative. - There won't be any test cases for
.##
without a leading number before the comma (i.e.2+.7
will not be a valid test case, but2+0.7
could be).
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, 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: 270 degrees & [[1,2],[2,3],[0,3],[1,0],[1,1]]
Output: 517*6=3102
Input: 90 degrees & [[3,1],[0,0],[0,1],[3,3],[2,0],[0,3],[0,0],[0,2],[3,0],[2,1]]
Output: 800/4+0.75=200.75
Input: 0 degrees & [[0,0],[1,0],[2,0],[3,0],[1,2],[2,1],[2,2]]
Output: 789/263=3
Input: 180 degrees & [[3,0],[1,0],[1,2],[0,0],[3,2],[0,1],[2,0],[0,3],[2,1],[0,3],[3,2]]
Output: 0.6+4-0/2/4=0.575
Can I output the equation with (proper) parentheses? (e.g.
((((0.6)+4)-0)/2)/4=0.575
) – dzaima – 2017-06-22T15:00:46.7931The test cases have many errors (e.g. the 3rd and 4th has X and Y swapped (1st doesn't) and I don't even know what happened to the 2nd) – dzaima – 2017-06-22T15:14:39.107
2Should the program handle weird button presses?
1+-*/+-*/2
will gives0.5
on Windows (10) calculator. – user202729 – 2017-06-22T15:21:56.8871second test case should start with
[1,3],
– Uriel – 2017-06-22T15:37:32.8431Do we have to handle decimals less than 1 without leading 0s, like in
2+.7
? – Tutleman – 2017-06-22T15:37:38.0931In Windows 8 calculator,
3
*
-
5
is equivalent to3-5
. Does it indicate that the code needn't handle negative numbers in the input (though negatives are possible during calculation)? – Keyu Gan – 2017-06-22T15:55:16.163@KevinCruijssen Yes, the post has been in sandbox for 7 days, but was in there for only 5 hours with the precedence rule and test-cases added. – dzaima – 2017-06-22T16:10:25.793
Inb4 Mr. Mendo provides us a 13 byte MATL answer. – Magic Octopus Urn – 2017-06-22T16:12:31.517
4Operator precedence is why I never use Windows Calculator in standard mode. – Neil – 2017-06-22T17:03:37.827
@dzaima Thanks for pointing this out, and you are indeed right I shouldn't have posted it as soon after editing.. I was impatient and eager to post it I guess. Test cases should all be fixed now (I hope). As for your very first question: no, you can't have parenthesis in the output. – Kevin Cruijssen – 2017-06-22T17:24:26.393
1@user202729 I've added a rule that there won't be two operands following each other, so you won't have to deal with that. – Kevin Cruijssen – 2017-06-22T17:25:15.933
@Tutleman I've added a rule for this: all decimals will have a leading number before the comma, so
2+.7
won't be a valid test case, but2+0.7
could be a valid test case. – Kevin Cruijssen – 2017-06-22T17:26:03.397@KeyuGan The minus-sign will only be used as operand in this challenge, so you won't have to deal with negative numbers (except in the form of a test case like
5-9=-4
). – Kevin Cruijssen – 2017-06-22T17:27:06.160It seems like the 1st test case is given with
[x,y]
coordinates and the other ones with[y,x]
. Could you please clarify? (+what @Uriel already said: the first entry of the 2nd test case is not consistent) – Arnauld – 2017-06-22T17:48:47.370@Neil : The worst part is that the windows calculator displays "1 + 2 * 3" but calculates "(1 + 2) * 3" – Eric Duminil – 2017-06-22T19:38:35.833