# Daily Challenge #242 - Expressions Matter

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 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

``````def expressions_matter(a, b, c):
x = sort([a, b, c])
if x == 1:
return (1 + x) * x
return x * x * x
``````

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:

``````function expressionMatter(\$a, \$b, \$c) {
\$possible_ans = [
\$a * \$b * \$c,
\$a + \$b + \$c,
(\$a + \$b) * \$c,
\$a + \$b * \$c,
\$a * \$b + \$c,
\$a * (\$b + \$c),
];

return max(\$possible_ans);
}
``````

JavaScript

``````export default (a, b, c) => Math.max(
a + b + c,
a * b * c,
a + b * c,
(a + b) * c,
a * b + c,
a * (b + c)
);
``````

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,

``````expression_formats = [
'{}+{}+{}',
'{}*{}*{}',
'{}+{}*{}',
'{}*{}+{}',
'({}+{})*{}',
'{}*({}+{})'
]

def expression_matter(a,b,c):
return max(eval(s.format(a,b,c)) for s in expression_formats)
``````

Output,

``````print(expression_matter(1, 2, 3)) # output -> 9
print(expression_matter(5, 1, 3)) # output -> 20
print(expression_matter(3, 5, 7)) # output -> 105
print(expression_matter(5, 6, 1)) # output -> 35
print(expression_matter(1, 6, 1)) # output -> 8
print(expression_matter(2, 6, 1)) # output -> 14
``````

maybe not the most elegant, but it work!

``````def expression_matter(a,b,c)
sum = [a,b,c].sum

tmp = [a,b,c].inject('*')
sum = tmp if sum < tmp

tmp = (a + b) * c
sum = tmp if sum < tmp

tmp = a + (b * c)
sum = tmp if sum < tmp

tmp = a * (b + c)
sum = tmp if sum < tmp

tmp = (a + c ) * b
sum = tmp if sum < tmp

sum
end
``````
``````pry(main)> expression_matter(5, 1, 3)
=> 20
pry(main)> expression_matter(3, 5, 7)
=> 105
pry(main)> expression_matter(5, 6, 1)
=> 36
pry(main)> expression_matter(1, 6, 1)
=> 12
pry(main)> expression_matter(2, 6, 1)
=> 18
``````

Alternative Ruby version:

``````def expression_matter(a, b, c)
[
a + b + c,
a * b * c,
a + b * c,
(a + b) * c,
a * b + c,
a * (b + c)
].max
end
``````

My solution using Rust .
Playground

``````fn expressions_matter(a: u32, b: u32, c: u32) -> u32 {
let mut all_calculations = [
a + b + c,
a * b * c,
a + b * c,
(a + b) * c,
a * b + c,
a * (b + c),
];
all_calculations.sort();
all_calculations
}
``````

In javascript

``````const expression_matter=(t,a,e)=>Math.max(t+a+e,t*a*e,t+a*e,(t+a)*e,t*a+e,t*(a+e));
``````  