DEV Community

dev.to staff
dev.to staff

Posted on

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!

Top comments (7)

Collapse
 
kira009 profile image
Shohan • Edited

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[0] == 1:
        return (1 + x[1]) * x[2]
    return x[0] * x[1] * x[2]
Collapse
 
vidit1999 profile image
Vidit Sarkar

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
Collapse
 
peter279k profile image
peter279k

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);
}
Collapse
 
daviducolo profile image
Davide Santangelo • Edited

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
Collapse
 
coolshaurya profile image
Shaurya • Edited

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[5]
}
Collapse
 
kvharish profile image
K.V.Harish

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));
Collapse
 
kira009 profile image
Shohan

Hmm yes, but looking at the examplea given, I assumed a, b and c would be positive