Tuples and sets · 14 / 16 Exercise Medium

Points with named tuples

Objective

A named tuple (namedtuple) is a tuple whose fields have names: you access them with p.x instead of p[0], and it is still immutable and sortable.

Instructions

  • The first line has the number n of points; each one is a line x y (integers).
  • Create Point = namedtuple("Point", ["x", "y"]) with x. Sort the points by their squared distance to the origin (x² + y²) and, if tied, by the tuple itself. Show Point(x=3, y=4) 25 for each point and at the end Farthest: Point(...).
Input
3
3 4
1 1
-5 0
Output
Point(x=1, y=1) 2
Point(x=-5, y=0) 25
Point(x=3, y=4) 25
Farthest: Point(x=3, y=4)

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise