The task is writing a function, which returns average hours of sleep in a week. Excluding fridays and saturdays. The input is a list of hours, starting at monday.
def averageSleep(hours):
fridays = hours[4::-5]
saturdays = hours[5::-6]
length = len(hours)
final = (sum(hours)-sum(fridays)-sum(saturdays)) / (length - 2)
return final
print(averageSleep([6, 7.5, 3.5, 6, 9, 10, 7, 6, 7, 4]))
The function that I wrote returns:
5.875
Which is correct. Excluding items 9 and 10 (friday and saturday) from the list. The problem is, however, that passing in more list items (yielding more fridays and saturdays) makes the function return wrong results. hours[4::-5]
and hours[5::-6]
just don't work for more fridays and saturdays.
Any idea how to pick all the fridays and saturdays from the list?