+ 1

How can i reduce the execution time of my program

#here is the simple BMI calculator program which gives your BMI and gives you target to loose/weight based on the BMI calculated. # the instance of Human class takes basically takes three arguments. # 1. Name as String # 2. height in centimeters # 3. weight in kgs class Human: def __init__(self,name,height,weight): self.name=name self.height=height self.weight=weight def bmi(self): bmi= round(float(self.weight/((self.height/100)**2)),1) return bmi # calculate the bmi based on formula= weight/(height in meters)raise to power 2) # the round method trucates the float value to one decimal place def bmirange(self,bmi): # to compute the range of BMI # BMI btw 24.9 and 18.5 is normal # BMI below 18.5 is underweight # BMI btw 25 and 29.9 is overweight # BMi above 30 is obese if((bmi <= 24.9) & (bmi > 18.5)): print("You are fit") elif (bmi<=18.5) : print("You are underweight") print ("Target : Gain ",self.goal(bmi,.1), " kg") elif ((bmi>=25) & (bmi<=29.9)): print("You are overweight") print ("Target : Loose "+str(self.goal(bmi,-.1))+ " kg") elif(bmi >= 30): print("You are obese") print ("Target : Loose "+str(self.goal(bmi,-.1))+ " kg") def goal(self,current_bmi, step): # the step argument is has two values either 0.1 in case of underweight or -0.1 in case of overweight and obese normalrange=(18.5,24.9) target=0.0 #weight=self.weight while(current_bmi not in normalrange): #here the step can be -0.1 or +0.1 based on the calcuted BMI passed from bmirange method self.weight+=step current_bmi=self.bmi() target+=float(abs(step)) return round(target,1) david= Human("David",167,89) rock= Human("Rock",189,102) print(david.name," BMI :",david.bmi()) david.

30th May 2018, 12:29 PM
Sandeep Rana
16 Réponses
+ 3
but you are doing so in your bmi function just use def goal(current_bmi): if current_bmi > 25: return 25*((self.height/100)**2) if current_bmi < 18.5: return 18.5 * ((self.height/100)**2 return self.weight or something similar
30th May 2018, 1:07 PM
Max
Max - avatar
+ 3
Hello Sandeep Rana . I rewrote your program a bit. I think it actually covers what Max was talking about. I rewrote the ifs... They are a lot more efficient now. The first one starts with the less than 18.5 condition. Else means that it is already bigger than 18.5 so we just need to say elif less than 24.9 and so on.... I also rewrote the goal methods. Loops consumes time and should be avoided if not needed. Basically the logic is: If a person is underweight they should shoot for the lowest end of the bmi range 18.5. If they are overweight their goal should be the highest end of the range 24.9... And I go from there If you don't understand something, let me know. https://code.sololearn.com/cHPcU6wPI9Q0/?ref=app
30th May 2018, 1:09 PM
cyk
cyk - avatar
+ 3
Sandeep Rana Well, we always formulate algorithms to solve problems. It just that with time algorithms come more easily and quickly... So inthis case: For the ifs: I know everything falls into 4 groups and the groups are consecutive with no breaks between them and the variable has to belong to exactly one group That means that if, elif and else are the best way to structure those 4 groups. And because the groups are consecutive, if it is not the first group it has to be the second, if not then the 3rd and so on. So it is better to structure the groups in a logical way. That is why 18.5 first then 24.9 then the following then the last case...
30th May 2018, 2:09 PM
cyk
cyk - avatar
+ 3
Sandeep Rana Regarding the goal method and I like Max's code better than mine there though we rely on the same thing, the idea is very simple. If the person is underweight but wants to be fit the quickest way is get to the lower end of the range and the opposite for the person who is overweight. So there are two options meaning two ifs, and then for each option I use the lower end of the range or the upper end As you can see a lot of this is logic and you will get a lot better at coming up with solutions that easily solve your problem with practice ☺
30th May 2018, 2:10 PM
cyk
cyk - avatar
+ 2
you can solve the equation analyticaly instead of numericaly like you are doing. weight = bmi * (height/100)^2 and just plug in the bmi values you want or at leat use newtons method or something
30th May 2018, 12:47 PM
Max
Max - avatar
+ 2
Anytime! ☺
30th May 2018, 1:26 PM
cyk
cyk - avatar
+ 1
you dont need to find the goal weight in such a strange way. if the person is underweight just solve the equation of the bmi for weight with bmi set to the lower end of the range and if the person is lverweight just solve the equation for weight with bmi set to the upper end of the normal range
30th May 2018, 12:38 PM
Max
Max - avatar
+ 1
im not saying anything about python, i am saying that you should use mathematics instead of brute force to find the weight
30th May 2018, 12:57 PM
Max
Max - avatar
+ 1
wow this is promising code all of a sudden. thanks for the help. your code is just like reading an article on BMI lol. stay healthy coders. thanks man. much appreciated.
30th May 2018, 1:25 PM
Sandeep Rana
0
it automatically does that. for example. if BMI is 15 then goal method will increment the weight by .1 and hence it will get the lower end of normal range i.e. 18.5 similarly if BMI is 30 then it will decrement using -1. so it is doing as you per required.
30th May 2018, 12:44 PM
Sandeep Rana
0
as I am beginner in python. I am yet to learn what you are saying. :)
30th May 2018, 12:51 PM
Sandeep Rana
0
yeah I understand you. that's what I am telling you. I am yet to learn how to implement mathematical functions is python. :)
30th May 2018, 1:00 PM
Sandeep Rana
0
yeah I understand you. that's what I am saying. I am yet to learn how to implement mathematical functions in python. :)
30th May 2018, 1:00 PM
Sandeep Rana
0
cyk what was your approach to solve this problem. did you formulate algorithm for it. I mean how did you go through the process in general. I know this seems vague question. but still. thanks. :)
30th May 2018, 1:34 PM
Sandeep Rana
0
Max thanks for your concise algorithm too.
30th May 2018, 1:38 PM
Sandeep Rana
0
these are really very useful tips cyk . I shall always keep them in while coding now on. :). thanks.
30th May 2018, 2:28 PM
Sandeep Rana