- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Diet Plan Performance in Python
Suppose a dieter consumes calories[i], this indicates the calories on the i-th day. If we have an integer k, for every consecutive sequence of k days that is (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they find T. The T is the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]): But there are some conditions;
- If T < lower bound, they performed poorly on their diet so lose 1 point;
- If T > upper bound, they performed well on their diet so gain 1 point;
- Otherwise, they performed normally. So points are the same.
At first, the dieter has zero points. We have to find the total number of points the dieter has gained.
If the array is [6,5,0,0] and k = 2 and lower := 1 and upper := 5, then the output will be 0. C + C > upper, so increase one point, lower <= C + C <= upper so no change, after that C + C < lower, so decrease one point, so the output will be 0.
To solve this, we will follow these steps −
- temp := 0
- for i in range 0 to k
- temp := temp + C[k]
- right := k – 1, and left := 0, and points := 0
- while right < length of C, do
- if temp < lower, then decrease points by 1, if temp > upper, then increase points by 1
- temp := temp – C[left]
- increase left and right by 1
- if right >= length of C, then come out from the loop
- temp := temp + C[right]
- return points
Let us see the following implementation to get better understanding −
class Solution(object): def dietPlanPerformance(self, c, k, l, u): temp = 0 for i in range(k): temp += c[i] right = k-1 left = 0 points = 0 while right < len(c): if temp<l: points-=1 elif temp>u: points+=1 temp -=c[left] left+=1 right+=1 if(right >= len(c)): break temp+=c[right] return points ob1 = Solution() print(ob1.dietPlanPerformance([6,5,0,0],2,1,5))
[6,5,0,0] 2 1 5
- Related Articles
- The Military Diet: Safety, Effectiveness, and Meal Plan
- What Is the Sirtfood Diet and associated Weight Loss Plan?
- A Complete Mediterranean Diet Food List and 14-Day Meal Plan
- What is the best and healthy diet plan for increasing height in children?
- What Is the DASH Diet? The Scientific Plan for Lowering Blood Pressure
- How to optimize Python Dictionary for performance?
- Long-Term Financial Plan vs. Short-Term Financial Plan
- Difference between Business Continuity Plan and Disaster Recovery Plan
- How to improve file reading performance in Python with MMAP function?
- How to optimize the performance of Python regular expression?
- Specific Performance in Contracts
- Understanding MongoDB Query Plan
- Is Performance Management the Same as Performance Appraisal?
- Ethernet Performance
- Performance Metrics