Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 23
[ ASSIGNMENT: ] Expressions Matter
TASK : GivenĀ three integersĀ a, b, c, returnĀ theĀ largest numberĀ obtained after inserting the following operators and bracketsĀ +,Ā *,Ā ( ) For Example : : 1. expressionsMatter(1,2,3) --> return 9 After placing signs and brackets, theĀ Maximum valueĀ obtained from the expression is (1+2) * 3 = 9 2. expressionsMatter(9,1,1) --> return 18 After placing signs and brackets, theĀ Maximum valueĀ obtained from the expression isĀ 9 * (1+1) = 18 HappyCodings!:-) https://code.sololearn.com/WK4SG3ldwZuv/?ref=a
13 Respostas
+ 22
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
the Maximum value,Ā
that you can obtain is 9
NOTE :
- 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 can't get expressionĀ (1+3) * 2 = 8
+ 19
https://code.sololearn.com/cJa3D2E7O4p7/?ref=app
+ 12
https://code.sololearn.com/cL29Mw48q51w/?ref=app
+ 7
+ 5
Even better : finds a specific target, works for any length, and set of operations including brackets https://code.sololearn.com/cTSlP5186dmM/?ref=app
+ 4
Clean but lazy solution..š
šš
https://code.sololearn.com/cAYq0sr6N8Lj/?ref=app
+ 2
Shorter version
https://code.sololearn.com/cPsbrOHTCCgD/?ref=app
+ 2
Using a little trick:
https://code.sololearn.com/c77WvMcGeRZZ/?ref=app
+ 2
My solution:
https://code.sololearn.com/ciG77Rr3d5f6/?ref=app
+ 1
My code
(Written by language python)
def d2(x1,x2):
if x1>1 and x2>1:
return x1*x2
else:
return x1+x2
def d3(x,y,z):
return max(d2(x,d2(y,z)),
d2(y,d2(x,z)),
d2(z,d2(x,y)),
x*y*z,
x+y+z
)
a,b,c=eval(input()),eval(input()),\
eval(input())
print(d3(a,b,c))