189k views
3 votes
Def subject_study_hours(subject_log, hour_log, subject):

Calculate the total number of study hours for `subject`.
:param subject_log: list of strings representing subjects that correspond
to weekly study sessions. Each occurrence of a subject has also a
corresponding number of study hours in the `hour_log`.
:param hour_log: list of integers representing study hours corresponding
to each subjet in the subject_log, e.g., [1, 3, 2]
:param subject: string, representing a subject
:return: integer
Example: course_study_hours(['comp', 'math', 'comp'], [1, 3, 2], 'comp')
returns 3

User Grover
by
7.3k points

1 Answer

4 votes

Final answer:

To calculate the total number of study hours for a specific subject, iterate through the subject_log and hour_log lists, sum up the study hours for the desired subject, and return the total hours.

Step-by-step explanation:

To calculate the total number of study hours for a specific subject, you can use the subject_log and hour_log lists in Python. First, find the indices of the subject occurrences in subject_log using the index() method. Then, iterate through these indices and sum up the corresponding study hours from hour_log. Finally, return the total study hours.

Here is an example:

def subject_study_hours(subject_log, hour_log, subject):
total_hours = 0
for i in range(len(subject_log)):
if subject_log[i] == subject:
total_hours += hour_log[i]
return total_hours

If you call subject_study_hours(['comp', 'math', 'comp'], [1, 3, 2], 'comp'), it will return 3, which is the total number of study hours for the subject 'comp'.

User Ivoronline
by
7.9k points