Вступ:
Натхненний дискусією, яка триває вже багато років щодо виразу .
З виразом математики швидко побачать, що правильна відповідь - , тоді як люди з простим математичним фоном зі школи швидко побачать, що правильна відповідь - це . То звідки береться ця суперечка і тому різні відповіді? Існує два суперечливих правила в тому, як пишеться . Один через частину
2(
, а один через символ поділу÷
.Хоча і математики, і «звичайні люди» використовуватимуть PEMDAS ( парентез - експоненти - ділення / множення - додавання / віднімання), для математиків вираз оцінюється так, як нижче, тому що само, як, наприклад, одночлен aka " єдиний доданок через мається на увазі множення на співставлення " (і, отже, частина
P
вPEMDAS
), який буде оцінюватися інакше, ніж (двочлен як і два доданки):
MD
PEMDAS
However, even if we would have written the original expression as , there can still be some controversy due to the use of the division symbol ÷
. In modern mathematics, the /
and ÷
symbols have the exact same meaning: divide. Some rules pre-1918† regarding the division symbol ÷
†† state that it had a different meaning than the division symbol /
. This is because ÷
used to mean "divide the number/expression on the left with the number/expression on the right"†††. So then, would be or now. In which case would be evaluated like this by people pre-1918:
†: Although I have found multiple sources explaining how ÷
was
used in the past (see ††† below), I haven't been able to find
definitive prove this changed somewhere around 1918. But for the sake
of this challenge we assume 1918 was the turning point where ÷
and
/
starting to mean the same thing, where they differed in the past.
††: Other symbols have also been used in the past for division, like
:
in 1633 (or now still in The Netherlands and other European non-English speaking countries, since this is what I've personally learned in primary school xD) or)
in the 1540s. But for this challenge we only focus on the pre-1918 meaning of the obelus symbol÷
.
†††: Sources: this article in general. And the pre-1918 rules regarding÷
are mentioned in: this The American Mathematical Monthly article from February 1917; this German Teutsche Algebra book from 1659 page 9 and page 76; this A First Book in Algebra from 1895 page 46 [48/189].Slightly off-topic: regarding the actual discussion about this expression: It should never be written like this in the first place! The correct answer is irrelevant, if the question is unclear. *Clicks the "close because it's unclear what you're asking" button*.
And for the record, even different versions of Casio calculators don't know how to properly deal with this expression:
Challenge:
You are given two inputs:
- A (valid) mathematical expression consisting only of the symbols
0123456789+-×/÷()
- A year
And you output the result of the mathematical expression, based on the year (where ÷
is used differently when , but is used exactly the same as /
when ).
Challenge rules:
- You can assume the mathematical expression is valid and only uses the symbols
0123456789+-×/÷()
. This also means you won't have to deal with exponentiation. (You are also allowed to use a different symbols for×
or÷
(i.e.*
or%
), if it helps the golfing or if your language only supports ASCII.) - You are allowed to add space-delimiters to the input-expression if this helps the (perhaps manual) evaluation of the expression.
- I/O is flexible. Input can be as a string, character-array, etc. Year can be as an integer, date-object, string, etc. Output will be a decimal number.
- You can assume there won't be any division by 0 test cases.
- You can assume the numbers in the input-expression will be non-negative (so you won't have to deal with differentiating the
-
as negative symbol vs-
as subtraction symbol). The output can however still be negative! - You can assume
N(
will always be written asN×(
instead. We'll only focus on the second controversy of the division symbols/
vs÷
in this challenge. - Decimal output-values should have a precision of at least three decimal digits.
- If the input-expression contains multiple
÷
(i.e. ) with , they are evaluated like this: . (Or in words: number is divided by expression , where expression in turn means number is divided by number .) - Note that the way
÷
works implicitly means it has operator precedence over×
and/
(see test case ). - You can assume the input-year is within the range .
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 with default I/O rules, 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 (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input-expression: Input-year: Output: Expression interpretation with parenthesis:
6÷2×(1+2) 2018 9 (6/2)×(1+2)
6÷2×(1+2) 1917 1 6/(2×(1+2))
9+6÷3-3+15/3 2000 13 ((9+(6/3))-3)+(15/3)
9+6÷3-3+15/3 1800 3 (9+6)/((3-3)+(15/3))
4÷2÷2 1918 1 (4/2)/2
4÷2÷2 1900 4 4/(2/2)
(1÷6-3)×5÷2/2 2400 -3.541... ((((1/6)-3)×5)/2)/2
(1÷6-3)×5÷2/2 1400 1.666... ((1/(6-3))×5)/(2/2)
1×2÷5×5-15 2015 -13 (((1×2)/5)×5)-15
1×2÷5×5-15 1719 0.2 (1×2)/((5×5)-15)
10/2+3×7 1991 26 (10/2)+(3×7)
10/2+3×7 1911 26 (10/2)+(3×7)
10÷2+3×7 1991 26 (10/2)+(3×7)
10÷2+3×7 1911 0.434... 10/(2+(3×7))
4÷2+2÷2 2000 3 (4/2)+(2/2)
4÷2+2÷2 1900 2 4/((2+2)/2)
4÷2×2÷3 9999 1.333... ((4/2)×2)/3
4÷2×2÷3 0000 3 4/((2×2)/3)
((10÷2)÷2)+3÷7 2000 2.928... ((10/2)/2)+(3/7)
((10÷2)÷2)+3÷7 1900 0.785... (((10/2)/2)+3)/7
(10÷(2÷2))+3×7+(10÷(2÷2))+3×7
1920 62 (10/(2/2))+(3×7)+(10/(2/2))+(3×7)
(10÷(2÷2))+3×7+(10÷(2÷2))+3×7
1750 62 (10/(2/2))+(3×7)+(10/(2/2))+(3×7)
10÷2/2+4 2000 6.5 ((10/2)/2)+4
10÷2/2+4 0100 2 10/((2/2)+4)
9+6÷3-3+15/3 9630 13 9+(6/3)-3+(15/3)
9+6÷3-3+15/3 0369 3 (9+6)/(3-3+(15/3))
`=`=`/`
is diabolical! Great solution!