0

Max sum

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 Maximum Obtained Here is my code: def expression_matter(a, b, c): r = a * (b+c) d = a * b * c g = a + b * c f = (a+b)*c if r > d or g or f: return r elif d > r or g or f: return d elif g > f or r or d: return g else: return f Only some of the test cases are correct. Please explain where I'm wrong! Here you can check my and your code: https://www.codewars.com/kata/5ae62fcf252e66d44d00008e/train/JUMP_LINK__&&__python__&&__JUMP_LINK

30th May 2021, 12:23 AM
Ailana
Ailana - avatar
23 ответов
+ 1
your code test for 4 equations (r, d, g, f) but you are missing 3 others possibilities ^^
30th May 2021, 2:23 AM
visph
visph - avatar
+ 1
that's not the right way to write conditions: if r > d or g... doesn't test for r greater than d or r greater than g... but only if r greater than g or g is different from zero (g falsy if zero) ^^
30th May 2021, 2:48 AM
visph
visph - avatar
+ 1
however, rather than high verbosity with a bunch of if..elif and lot of conditions, python provide a function wich could help you sparing typing ;P
30th May 2021, 2:49 AM
visph
visph - avatar
+ 1
a function wich return the maximum value from a list ^^ but don't loose your time by searching it, instead try to write correct conditions for your if..elif... once you will pass the tests, you could explore other solutions and you will quickly find it ^^
30th May 2021, 2:59 AM
visph
visph - avatar
+ 1
still not... your second attempt seems fine for the 7 combinations (I don't have looked precisely), but your all three attempts don't use the correct conditions (as I explained previously)... in your last code, you only replaced the 'or' by 'and'... that's a first step, but you should test if x is greater than y and x is greater than z... rather than if x is greater than y and z is different of zero ^^ (I use only x, y, and z to show you the pattern, but you should use all your seven result variables, from your second attempt (in the third you downgrade your solution to the first pointed error): if x > y and x > z and ...
30th May 2021, 5:20 AM
visph
visph - avatar
+ 1
all the seven equations from your second attempt... and test for all seven results at each if..elif... and your code should work.
30th May 2021, 6:32 AM
visph
visph - avatar
+ 1
Rahul Prajapati when you ask someone to message you instead of guiding them to the answer here, you deny others a learning opportunity. I don't beleive in downvoting from the shadows for the same reason (otherwise I would have added a downvote to the tally) I feel we should rather let someone know why we don't like their answer just as I have done here.
31st May 2021, 8:25 PM
Paul K Sadler
Paul K Sadler - avatar
0
codewars rules forbid to cheat... and requesting help is like cheating ^^ however, as this is only a 8kyu kata, I will give you an hint about where you are wrong: you are only testing 4 combinations among 7 (other combinations do same result than one among the seven's) ;P
30th May 2021, 1:17 AM
visph
visph - avatar
0
Ailana what is your username on codewars? I can't find "Ailana" nor "ailana"...
30th May 2021, 1:37 AM
visph
visph - avatar
0
I didnt understand your hint please explain ?
30th May 2021, 2:19 AM
Ailana
Ailana - avatar
0
Oh
30th May 2021, 2:24 AM
Ailana
Ailana - avatar
0
one should be obvious, as you noticed the difference between g and f ;)
30th May 2021, 2:26 AM
visph
visph - avatar
0
New code but still doesn't work... def expression_matter(a, b, c): r = a * (b+c) d = a * b * c g = a + b * c f = (a+b)*c h = a + b + c s = a * b + c k = a * c + b if r > d or g or f or h or s or k: return r elif d > r or g or f or h or s or k: return d elif g > f or r or d or s or k or h: return g elif h > s or k or g or f or r or d: return h elif s > k or h or g or f or r or d: return s elif k > s or h or g or f or r or d: return k else: return f
30th May 2021, 2:44 AM
Ailana
Ailana - avatar
0
Rlly?! What us it!!!!!
30th May 2021, 2:50 AM
Ailana
Ailana - avatar
0
def expression_matter(a, b, c): r = a * (b+c) d = a * b * c g = a + b * c f = (a+b)*c if r > d and g and f: return r elif d > r and g and f: return d elif g > f and r and d: return g elif f > g and r and d: return f
30th May 2021, 4:50 AM
Ailana
Ailana - avatar
0
def expression_matter(a, b, c): r = a * (b+c) d = a * b * c g = a + b * c f = (a+b)*c if r > d and r > g and r > f: return r elif d > r and d > g and d > f: return d elif g > f and g > r and g > d: return g elif f > g and f > r and f > d: return f
30th May 2021, 6:24 AM
Ailana
Ailana - avatar
0
I'm sorry, but my code still doesn't work...
30th May 2021, 6:40 AM
Ailana
Ailana - avatar
0
post your code, i cannot trust that if you did all what I said it will not work... in fact, I'm quite sure you make again some mistake, as you seems to half read what I wrote :(
30th May 2021, 6:46 AM
visph
visph - avatar
0
def Max_method(a,b,c): k=a+b+c l=a*b*c m=a+b*c n=a*b+c o=a*(b+c) p=(a+b)*c if k>l or k>m or k>n or k>o or k>p: return k if l>k or l>m or l>n or l>o or l>p: return l elif m>l or m>k or m>n or m>o or m>p: return m elif n>l or n>m or n>k or n>o or n>p: return n elif o>l or o>m or o>n or o>k or o>p: return o else: return p
31st May 2021, 12:54 PM
Rahul Prajapati
0
also, as I stated in my first post, it's against codewars rules to cheat (and if to be helped is form/kind of cheat, fullfilling a kata in place of other one IS cheating)
31st May 2021, 8:41 PM
visph
visph - avatar