Python Forum
Maths and python: Compute stress level - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Maths and python: Compute stress level (/thread-24413.html)



Maths and python: Compute stress level - cheerful - Feb-12-2020

Hi Expert,

I have a very though question to implement in python. It looks simple but hard. The question is as follow:

A student is preparing for the mid-semester exams and wants to make the least stressful schedule to study. There are 'n' subjects each of which has certain stress-level, and the student has 'days' study days left. The student must study the subjects in order, ie study the ith subject before going on to the (i+1)th subject.

The stress level of any day is defined as the maximum stress level of any subject the student studies on that day. Write a function to find the minimum stress level in which the student can finish studying all subjects, while still studying for at least one subject on each of the study days.

Constraints
1<= n <= 200
1<= days <= n <= 300
1<= stressLevel[i] <= 10^5


If days = 2, the solution is below:

stresslevel = [30, 10, 40, 20, 50]
# stresslevel = [1, 5, 3, 2, 4]

n = len(stresslevel)

days = 2

total_stress = []

for i in range(n-1): 
    x = stresslevel[:i+1]
    y = stresslevel[i+1:]
    
    x_max = max(x)
    y_max = max(y)
    
    temp = x_max + y_max
    total_stress.append(temp)
    
min_stress = min(total_stress)
min_stress
However, if days > 2 ie 5, it becomes difficult because there will be more pointers pointing to the stresslevel list and they will have to count up and down separately.


Thank you for your help
cheerful


RE: Maths and python: Compute stress level - Larz60+ - Oct-20-2021

This sounds like the typical knapsack problem which is very well covered by John Guttag in the second lecture of the MIT 'Introduction to Computational Thinking and Data Science' course.
You can watch that video here: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0002-introduction-to-computational-thinking-and-data-science-fall-2016/lecture-videos/lecture-2-optimization-problems/
or download the transcript and lecture materials from the same page.
This one lecture covers greedy, brute-force, and other algorithms to get the best answer.

He used food, your assignment uses stress level. The algorithms are quite similar if not exact.