44
6
Last time when I tried to come up with something easy that wasn't a duplicate, it ended up being way too hard.. So hopefully this time it's indeed something newcomers can try as well.
Input:
An array/list with integers/decimals. (Or a string representing an array with integers/decimals.)
Output:
Loop through the numbers and apply the following five mathematical operands in this order:
- Addition (
+
); - Subtraction (
−
); - Multiplication (
*
or×
or·
); - Real / Calculator Division (
/
or÷
); - Exponentiation (
^
or**
).
(NOTE: The symbols between parenthesis are just added as clarification. If your programming language uses a completely different symbol for the mathematical operation than the examples, then that is of course completely acceptable.)
Keep continuing until you've reached the end of the list, and then give the result of the sum.
Challenge rules:
- Exponentiation by 0 (
n ^ 0
) should result in 1 (this also applies to0 ^ 0 = 1
). - There are no test cases for division by 0 (
n / 0
), so you don't have to worry about that edge-case. - If the array contains just a single number, we return that as the result.
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.
Test cases:
[1,2,3,4,5] -> 0
-> 1 + 2 = 3
-> 3 - 3 = 0
-> 0 * 4 = 0
-> 0 / 5 = 0
[5,12,23,2,4,4,2,6,7] -> 539
-> 5 + 12 = 17
-> 17 - 23 = -6
-> -6 * 2 = -12
-> -12 / 4 = -3
-> -3 ^ 4 = 81
-> 81 + 2 = 83
-> 83 - 6 = 77
-> 77 * 7 -> 539
[-8,50,3,3,-123,4,17,99,13] -> -1055.356...
-> -8 + 50 = 42
-> 42 - 3 = 39
-> 39 * 3 = 117
-> 117 / -123 = -0.9512...
-> -0.9512... ^ 4 = 0.818...
-> 0.818... + 17 = 17.818...
-> 17.818... - 99 -> -81.181...
-> -81.181... * 13 = -1055.356...
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2] -> 256
-> 2 + 2 = 4
-> 4 - 2 = 2
-> 2 * 2 = 4
-> 4 / 2 = 2
-> 2 ^ 2 = 4
-> 4 + 2 = 6
-> 6 - 2 = 4
-> 4 * 2 = 8
-> 8 / 2 = 4
-> 4 ^ 2 = 16
-> 16 + 2 = 18
-> 18 - 2 = 16
-> 16 * 2 = 32
-> 32 / 2 = 16
-> 16 ^ 2 = 256
[1,0,1,0,1,0] -> 1
-> 1 + 0 = 1
-> 1 - 1 = 0
-> 0 * 0 = 0
-> 0 / 1 = 0
-> 0 ^ 0 = 1
[-9,-8,-1] -> -16
-> -9 + -8 = -17
-> -17 - -1 = -16
[0,-3] -> -3
-> 0 + -3 = -3
[-99] -> -99
Not integer division? – Leaky Nun – 2016-06-13T07:01:12.253
@LeakyNun No. Perhaps I should change the input to a list with decimals instead of integers due to division (and test case 3)? – Kevin Cruijssen – 2016-06-13T07:02:46.400
Was this in the sandbox? – Bálint – 2016-06-13T07:12:44.223
@Bálint Yes, since Friday morning (CET) (it's deleted now, but those without enough rep can still view it). And I received some good feedback from TimmyD and flawr that has improved the challenge a lot. (PS: Since I get this question every time I post a challenge, should I add a link to the deleted Sandbox & datetime next time?)
– Kevin Cruijssen – 2016-06-13T07:13:58.493No need, just answer this question every time you post a challenge. :) – Leaky Nun – 2016-06-13T07:57:10.087
For clarification, does
0^0 = 1
? From the description, it seems so, but you might want to make it explicit just in case. – Mego – 2016-06-13T08:03:07.097@Mego Yes,
0^0 = 1
. A.f.a.i.k.,n ^ 0
is always1
regardless ofn
in math (or are there different mathematical rules here, since you aren't the first to ask?). It also has the answer1
in the 5th test case for0 ^ 0
. But I'll add it at the top as well to clarify it explicitly. – Kevin Cruijssen – 2016-06-13T08:12:01.3339
In math, there are two conflicting "rules":
– Mego – 2016-06-13T08:14:47.363n ^ 0 = 1
, but0 ^ n = 0
. The conflict is resolved by settingn != 0
for both of the rules, but then it leaves0 ^ 0
undefined. However, there are a lot of things that fall into place nicely in mathematics if0 ^ 0
is defined to be1
. See Wikipedia for some details.If I return infinity instead of a divide by zero error, is thst okay? – Bálint – 2016-06-13T09:11:45.510
1@Bálint The rules state that there will never be a valid input with a division by zero. You don't have to worry about that edge case. – Mego – 2016-06-13T09:12:23.123
@Mego He said there's no testcase for it, but should normally ouput an error – Bálint – 2016-06-13T09:13:32.193
@Bálint The wording could be a bit clearer, but that is the interpretation I and the other posters have been going off of, since that was the suggestion in the Sandbox that led to the rule being added. – Mego – 2016-06-13T09:14:21.470
@Bálint @Mego is indeed right, you don't have to worry about divide by 0 cases / errors. I've chanegd the wording to clarify it a bit. – Kevin Cruijssen – 2016-06-13T10:49:33.693
*
is APL's symbol for exponentiation. Is this acceptable if one of the other two multiplication symbols is used? – Adám – 2016-06-14T07:28:18.970@Adám Yes, the symbols were just added as clarification of the mathematical operations in the sense of: pictures (or symbols) say more than thousand words. It's completely fine if your language uses even none of the symbols, as long as they imply and are executed as their counterpart mathematical operation. – Kevin Cruijssen – 2016-06-14T09:11:26.783