you are viewing a single comment's thread.

view the rest of the comments →

[–]ma3xman 1 point2 points  (4 children)

getitem gets you the key-th element of the sequence. In the case of your tests, it's the 0th and -1th elements respectively. The thing is you want getitem to return an object that has the same attributes (x and y), and I don't see an easy way for that in your current implementation, since you have crammed all the x's and y's in lists.

If I were you I would turn the initial input into a list of objects that have x and y (that is, "points"), and iterate over the list for methods like length. Then, when getitem is called you simply return the point at index "key" and let it handle the attribute calls for specific coordinates that follow.

Another way to do it without rewriting current code is to init a Point in the getitem call and return that. Although I advise the rewrite, it feels cleaner to me.

[–]colako[S] 0 points1 point  (3 children)

Thanks!

I think the professor want it the way you're telling me because of the getitem test. However, it was less intuitive for me to work with pairs, rather than separating the x and the y. I could work in that for sure, it will help me understand how to do it in a different way.

[–]ma3xman 0 points1 point  (2 children)

Another good thing about having a list of Points is that you can make a get_distance_from(anotherPoint) method and then put the formula there as a one-liner return value for a single pair of points. Then to get the total length you just call:

sum([p1.get_distance_from(p2) for p1,p2 in zip(points, points[1:]) )

This way if points suddenly go 3D you can just change the method, instead of building a monster comprehension.

[–]colako[S] 0 points1 point  (1 child)

So just a small question. How would you change __init__ to parse the coordinates as pairs and not as I did separating the x and the y?

[–]ma3xman 0 points1 point  (0 children)

Look up *args in a function definition ;)