Five a day¶
We have a list with the food servings that takes a person in a determined period of time (food list). This food list is a nested list of sublists and each sublist has two elements: the food name and its number of servings.
Write the function
nutrition1()that given a food list, returns the total number of servings of fruit and vegetables in the list.Save this function into a file named
nutrition1.py. Examples:>>> nutrition1([['bread', 2], ['meat', 1], ['milk', 3], ... ['bread', 3], ['vegetable', 5], ['meat', 1], ['fruit', 2], ... ['milk', 1], ['fruit', 3], ['bread', 2], ['vegetable', 3], ... ['fruit', 2], ['meat', 1]]) 15
Note
More tests are provided in file
nutrition1.txtWrite the function
nutrition2()that takes a food list, food, and a number of servings, nr, and returnsTrueif the person has eaten more than nr servings of fruit and vegetables (in total) andFalseotherwise.Save this function into a file named
nutrition2.py. Examples:>>> nutrition2([['bread', 2], ['meat', 1], ['milk', 3], ... ['bread', 3], ['vegetable', 5], ['meat', 1], ['fruit', 2], ... ['milk', 1], ['fruit', 3], ['bread', 2], ['vegetable', 3], ... ['fruit', 2], ['meat', 1]], 9) True
Note
More tests are provided in the
nutrition2.txtHe have another nested list of sublists with data corresponding to several weeks (week list). Each sublist of a week list represents a week and has 7 integers. These integers correspond to the number of fruit and vegetables servings (in total) eaten by the person each day of the week. The main list has an undefined number of sublists.
Write the function
nutrition3()that from a week list, week, and a number of servings, nr, returns the number of weeks where there is a day in which the number of fruit and vegetables servings (in total) is lower than nr.Save this function into a file named
nutrition3.py. Examples:>>> nutrition3([[3, 6, 7, 4, 5, 7, 1], [6, 6, 6, 6, 7, 7, 5], ... [5, 6, 6, 6, 6, 6, 6], [5, 5, 5, 5, 5, 5, 3]], 5) 2
Note
More tests are provided in the
nutrition3.txt
Solution
A solution of these functions is provided in files nutrition1.py, nutrition2.py, nutrition3.py