Given three integers a ,b ,c, return the largest number obtained after inserting the following operators and brackets: +, *, ()
In other words , try every combination of a,b,c with [*,+,()]
, and return the highest number.
Here's an example:
1 * (2 + 3) = 5
1 * 2 * 3 = 6
1 + 2 * 3 = 7
(1 + 2) * 3 = 9 <-- So the maximum value that you can obtain is 9.
Tests
expression_matter(5, 1, 3)
expression_matter(3, 5, 7)
expression_matter(5, 6, 1)
expression_matter(1, 6, 1)
expression_matter(2, 6, 1)
Good luck!
This challenge comes from MrZizoScream on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion (11)
If one of the numbers is 1, and if the next bigger number is y and largest number is z, ans will be z * (1 + y), else ans will be x * y * z
So
If the smallest number is 0 this will return 0 though, even if y and z are non-zero.
Hmm yes, but looking at the examplea given, I assumed a, b and c would be positive
The description says “given three integers”, so based on that I don’t think we can assume that.
Here is the simple solution for PHP:
JavaScript
I had considered using
Array#reduce
... but went for this option instead. Might add a few modifiers to the challenge, and see how it turns out (I will have them posted elsewhere if I do, to avoid clutter here).Tested in Node v14.2.0 with ava verbose, 1 rule was disabled (no-mixed-operators).
Here is a Python solution,
Output,
maybe not the most elegant, but it work!
Alternative Ruby version:
My solution using Rust .
Playground
In javascript