BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 3e — Tuples

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab3e.

Highest Score

10 minutes

Write a function called find_highest_score(scores). This function takes a list of scores, where each score is a tuple (last, first, score). This function should return the tuple that has the highest score.

You have starter code in scores.py:

def find_highest_score(scores: list[tuple[str, str, int]]) -> tuple[str, str, int]:
    # Write code here
    pass


def main():
    scores = [('Johns', 'Hayden', 72),
              ('Rodriguez', 'Emily', 94),
              ('Young', 'Henry', 91),
              ('Bean', 'Alma', 95),
              ('Peterson', 'Roger', 83)]
    last, first, score = find_highest_score(scores)
    print(f"Highest score: {first} {last}, {score}")


if __name__ == '__main__':
    main()

The list of scores is coded into the program, so the output should be:

Highest score: Alma Bean, 95

If you need help, look at find_max() in the guide on lists of tuples.

Discussion with TA:

  • What pattern is this?
  • What does None mean?

Ice Cream

15 minutes

A bunch of your friends rated three ice cream flavors on a scale from 1 to 10:

ratings = [('chocolate', 10), ('vanilla', 8.5), ('huckleberry', 6),
           ('chocolate', 9), ('banana nut', 9.3), ('vanilla', 7.5),
           ('huckleberry', 9.3), ('banana nut', 7.5), ('chocolate', 8),
           ('chocolate', 8), ('banana nut', 10), ('vanilla', 4.1),
           ('chocolate', 10), ('huckleberry', 10), ('vanilla', 10)]

They are really picky, so some of the scores are floats!

You need to write two functions:

  • First, ratings_for_flavor(ratings, requested_flavor) takes a list of ratings, which are tuples like above, and a requested flavor. The function returns a list of tuples that are ratings for that flavor. So if the requested flavor is ‘chocolate’, the returned list should have only ratings for chocolate ice cream.

  • Second, get_average(ratings) takes a list of ratings, which are tuples like above, and returns the average score for all of the ratings in the list. The average should be rounded to one decimal place. Information on rounding can be found in the guide for floats.

The rest of the code is given to you in ice_cream.py:

def ratings_for_flavor(ratings: list[tuple[str, float]], requested_flavor: str) -> list[tuple[str, float]]:
    # Write code here
    pass


def get_average(ratings: list[tuple[str, float]]) -> float:
    # Write code here
    pass


def print_info(ratings: list[tuple[str, float]], flavor: str):
    average = get_average(ratings)
    print(f"The average score for {flavor} is {average}.")


def main():
    ratings = [('chocolate', 10), ('vanilla', 8.5), ('huckleberry', 6),
               ('chocolate', 9), ('banana nut', 9.3), ('vanilla', 7.5),
               ('huckleberry', 9.3), ('banana nut', 7.5), ('chocolate', 8),
               ('chocolate', 8), ('banana nut', 10), ('vanilla', 4.1),
               ('chocolate', 10), ('huckleberry', 10), ('vanilla', 10)]
    flavors_to_report_on = ['chocolate', 'banana nut', 'vanilla']
    for flavor in flavors_to_report_on:
        new_ratings = ratings_for_flavor(ratings, flavor)
        print_info(new_ratings, flavor)


if __name__ == '__main__':
    main()

If you need help with the ratings_to_flavor() function, look at filter_to_type() in the guide on lists of tuples.

The list of ratings is coded into the program, so the output should be:

The average score for chocolate is 9.0.
The average score for banana nut is 8.9.
The average score for vanilla is 7.5.

Discussion with TA:

  • What pattern do these two function use?
  • How did you compute the average?

More ice cream

15 minutes

This is the same program as above, except the person running the program can enter all of the ice cream ratings, in addition to which flavors should be included in the report.

You need to write two functions:

  • The get_ratings() function should return a list of tuples (flavor, score) , as input by the user.
  • The get_flavors() function should return a list of flavors, like ['chocolate', 'banana nut', 'vanilla']

You have starter code in more_ice_cream.py:

def print_info(ratings: list[tuple[str, float]], flavor: str):
    average = get_average(ratings)
    print(f"The average score for {flavor} is {average}.")


def get_ratings() -> list[tuple[str, float]]:
    # Write code here
    pass


def get_flavors() -> list[str]:
    # Write code here
    pass


def main():
    ratings = get_ratings()
    flavors = get_flavors()
    for flavor in flavors:
        new_ratings = ratings_for_flavor(ratings, flavor)
        print_info(new_ratings, flavor)


if __name__ == '__main__':
    main()

You should be able to copy your functions for ratings_for_flavor() and get_average() from the previous problem.

To get the ratings, remember to use score = float(score) to convert a string score like '9.5' into a float 9.5. You can see examples of how to get the ratings in the guide on tuples. Getting the flavors is just getting a list of strings.

Your output should look something like this:

Enter all the ice cream ratings. Enter an empty flavor to end.
Enter a flavor: chocolate
Enter a score: 10
Enter a flavor: banana nut
Enter a score: 7.3
Enter a flavor: chocolate
Enter a score: 8
Enter a flavor: banana nut
Enter a score: 9.1
Enter a flavor: chocolate
Enter a score: 9.9
Enter a flavor: banana nut
Enter a score: 8.6
Enter a flavor:

Enter flavors to get info on, ending with a blank line.
Flavor: chocolate
Flavor: banana nut
Flavor:

The average score for chocolate is 9.3.
The average score for banana nut is 8.3.

Discussion with TA:

  • Did you use a helper function for get_flavors()?
  • Did you use None?
  • How comfortable are you with tuples and lists of tuples?

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. 😊