-4
def finalvalue(a,b,c,d):
    o=round(450-a-(b*2+c+d)*.75+.3)
    return ("Impossible, you " if(o)>100 else "You ")+"would need a "+str(o)+"%"

So here I have a function that calculates the weighted score of each of the inputs and what the final score someone will have to earn in order to get at least 90% overall. If the value is NOT an integer, in this case "o", then round to the nearest upper integer. If the score is greater than 100 return a different message with "impossible" instead.

I have already put the return statement with the if clause.

So my question is, how would I optimize this and do this in a fewer lines of code?

Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    Please don't make more work for others by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under a [CC BY-SA license](//creativecommons.org/licenses/by-sa/4.0), for SE to distribute the content (i.e. regardless of your future choices). By SE policy, the non-vandalized version is distributed. Thus, any vandalism will be reverted. Please see: [How does deleting work? …](//meta.stackexchange.com/q/5221). If permitted to delete, there's a "delete" button below the post, on the left, but it's only in browsers, not the mobile app. – Makyen Feb 23 '20 at 02:46
  • Please stop vandalising your post. It shows no respect for the amount of time and effort I put into giving you an answer. There is no reason not to leave the post as it stands. – Nick Feb 23 '20 at 02:54

1 Answers1

1

Here is a 144 character solution:

import math
def finalvalue(a,b,c,d):
 o=math.ceil(450-a-(b*2+c+d)*.75)
 return("Impossible, y"if o>100 else"Y")+"ou would need a "+str(o)+"%"

If a,b,c and d are integers then you can simplify further by noting the smallest fraction you can get on o is 1/4 i.e. 0.25, so we can add 0.3 and round:

def finalvalue(a,b,c,d):
 o=round(450-a-(b*2+c+d)*.75+.3)
 return("Impossible, y"if o>100else"Y")+"ou would need a "+str(o)+"%"

This comes in at 127 characters.

You can also simplify further using old-style string formatting and get rid of the round:

def finalvalue(a,b,c,d):
 o=450.9-a-(b*2+c+d)*.75
 return("Impossible, y"if o>100else"Y")+"ou would need a %d%%"%o

I think this is 114.

If you have access to Python 3.8, you can use a lambda expression and the walrus operator to reduce to 104 characters:

finalvalue=lambda a,b,c,d:("Y","Impossible, y")[(o:=450.9-a-(b*2+c+d)*.75)>100]+"ou would need a %d%%"%o
Nick
  • 138,499
  • 22
  • 57
  • 95
  • You must let me know what the best solution was when you find out. I took a bit more of a look today but couldn't see anything. – Nick Feb 26 '20 at 07:53
  • That's not fair - everyone should be able to see it and learn from it. – Nick Feb 29 '20 at 23:32
  • here it is bro: finalvalue=lambda a,b,c,d:["Y","Impossible, y"][(f:=450-int(a+.75*(2*b+c+d)))>100]+f"ou would need a {f}%" –  Mar 07 '20 at 21:51
  • i knew it had to be something with the string –  Mar 07 '20 at 21:51
  • basically we won (well mainly you). i am content now –  Mar 07 '20 at 21:53
  • i also told u it was integers only so that's my bad. but yea we did good homie, we won. –  Mar 07 '20 at 21:55
  • yea that's cuz this one incorporates floats not just intergers –  Mar 08 '20 at 00:50
  • its basically the same tho, we used all the same tricks and everything we won –  Mar 08 '20 at 00:52
  • Cool. It was an interesting puzzle. – Nick Mar 08 '20 at 01:03