Rightmost Place on the Map

Python for AIMedium+10 XP+10

Rightmost Place on the Map

A map is a list of (label, x) pairs. The class Place is empty.

Write two functions:

  • make_places(pairs) makes one Place for each pair, gives it a label attribute and an x attribute, and returns the list of places in the same order.
  • rightmost_label(places) takes a list of Place objects and returns the label of the one with the biggest x.
    • If two places tie, return the one that comes first.
    • An empty list gives back "".

rightmost_label must read the attributes off the objects. The tests only ever hand it what make_places built.

Examples

make_places([('gate', 7), ('pond', 2)])[1].label
returns 'pond'
places = make_places([('gate', 7), ('pond', 2)])
returns 'gate'
places = make_places([("cafe", 4), ("park", 9), ("shop", 2)])
returns 'park'
make_places([("cafe", 4)])[0].x
returns 4

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run