DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #160 - Expression Matters

Task

Given three integers a ,b ,c, return the largest number obtained after inserting the following operators and brackets: +, *, ().

Consider an Example :

With the numbers are 1, 2 and 3 , here are some ways of placing signs and brackets:

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.

Notes

  • The numbers are always positive.
  • The numbers are in the range (1  ≤  a, b, c  ≤  10).
  • You can use the same operation more than once.
  • It's not necessary to place all the signs and brackets.
  • Repetition in numbers may occur.
  • You cannot swap the operands. For instance, in the given example you cannot get expression (1 + 3) * 2 = 8.

Input >> Output Examples:
expressionsMatter(1,2,3) ==> return 9

Explanation:
After placing signs and brackets, the maximum value obtained from the expression (1+2) * 3 = 9.

expressionsMatter(1,1,1) ==> return 3

Explanation:
After placing signs, the Maximum value obtained from the expression is 1 + 1 + 1 = 3.

expressionsMatter(9,1,1) ==> return 18

Explanation:
After placing signs and brackets, the maximum value obtained from the expression is 9 * (1+1) = 18.


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!

Top comments (7)

Collapse
 
celyes profile image
Ilyes Chouia • Edited

Here's a solution in PHP

function expressionMatters($a, $b, $c){

    if($a < 0 || $b < 0 || $c < 0) {
        die("one of the operands is negative!");
    }
    $results = [];
    $results[] = $a * ($b + $c);
    $results[] = $a * $b * $c;
    $results[] = $a * $b + $c;
    $results[] = $a + $b * $c;
    $results[] = ($a + $b) * $c;
    $results[] = $a + $b + $c;
    return max($results);
}

echo expressionMatters(9, 1, 1); // 18
Collapse
 
rafaacioly profile image
Rafael Acioly

Notes

  • The numbers are always positive.

:)

Collapse
 
celyes profile image
Ilyes Chouia

I don't trust the user though lol

Collapse
 
rburmorrison profile image
Ryan Burmeister-Morrison

Here's a Nim submission:

proc expressionsMatter(a, b, c: Natural): Natural =
  ## Accepts three numbers in the Natural range [0..high(int)], then
  ## returns the maximum value after inserting parentheses, addition
  ## operators, and multiplication operators in any order.
  result = max(result, a + b + c)
  result = max(result, a * b * c)
  result = max(result, a + b * c)
  result = max(result, (a + b) * c)
  result = max(result, a * b + c)
  result = max(result, a * (b + c))

assert expressionsMatter(1, 2, 3) == 9
assert expressionsMatter(1, 1, 1) == 3
assert expressionsMatter(9, 1, 1) == 18
Collapse
 
elishaking profile image
King Elisha

JavaScript Solution

const expressionMatters = (a, b, c) => {

  [a, b, c].forEach((n, i) => {
    if (n < 1)
      throw new Error(`Operand at index: ${i} with value: ${n} 
is less than 1. All operands must be greater than 0 and less than 11`);
    else if (n > 10)
      throw new Error(`Operand at index: ${i} with value: ${n} 
is greater than 10. All operands must be greater than zero and less than 11`);
  });

  const results = [];
  results[0] = a * (b + c);
  results[1] = a * b * c;
  results[2] = a * b + c;
  results[3] = a + b * c;
  results[4] = (a + b) * c;
  results[5] = a + b + c;
  return Math.max(...results);
}

console.log(expressionMatters(1, 2, 3)); // 9
console.log(expressionMatters(1, 1, 1)); // 3
console.log(expressionMatters(9, 1, 1)); // 18
Collapse
 
alvaromontoro profile image
Alvaro Montoro

Can we do a ** b? ;)

Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

Also, a "don't ever do this" in JavaScript (or any other language). A ternary inside a ternary inside a ternary inside a ternary:

const expressionsMatter = (a, b, c) => b < 2
                                         ? a > c
                                           ? (b + c) * a
                                           : c > a
                                             ? (a + b) * c
                                             : a == 1
                                               ? a + b + c
                                               : a * b * c
                                         : a < 2 
                                           ? (a + b) * c
                                           : c < 2
                                             ? (b + c) * a
                                             : a * b * c;