2

There is a Location class

class Location(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def move(self, deltaX, deltaY):
        return Location(self.x + deltaX, self.y + deltaY)
    def getX(self):
        return self.x
    def getY(self):
        return self.y
    def dist_from(self, other):
        xDist = self.x - other.x
        yDist = self.y - other.y
        return (xDist**2 + yDist**2)**0.5
    def __eq__(self, other):
        return (self.x == other.x and self.y == other.y)
    def __str__(self):
        return '<' + str(self.x) + ',' + str(self.y) + '>'

and a get_cars method which I wrote to retrieve data from the list (doesn't belong to LOcation class) according to the spec below:

def get_cars(self):
    """ Returns a list of all cars on the parking. The list should contain 
    the string representation of the Location of a car. The list should 
    be sorted by the x coordinate of the location. """


    result = ''
    for i in sorted(self.car_loc_list, key=lambda Location: Location.x):
        if self.car_loc_list[0] == i:
            result += '\'' + str(i) + '\''
        else:    
            result += ', ' + '\'' + str(i) + '\''

    return '[' + result + ']'

self.car_loc_list is just a list which holds objects of Location class and they contain some coordinates(x,y) (unsorted):

for i in c.car_loc_list:
    print(i)

<0,0>
<1,5>
<7,2>
<2,1>
<1,7>
<4,3>

The online grader examine my code in 2 ways:

  1. print(c.get_cars()) - OK
  2. print(sorted(c.get_cars())) - NOT OK

When I follow the first way:

print(c.get_cars())

It prints out the next result sorted by X coordinate(1-st digit):

print(c.get_cars())
Out[539]: "['<0,0>', '<1,5>', '<1,7>', '<2,1>', '<4,3>', '<7,2>']"

It is also the result which I (and grader) expected to receive.

When I do print(sorted(c.get_cars)) I get:

print(sorted(c.get_cars()))
[' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', ',', '0', '0', '1', '1', '1', '2', '2', '3', '4', '5', '7', '7', '<', '<', '<', '<', '<', '<', '>', '>', '>', '>', '>', '>', '[', ']']

I stuck at this place. Also I understand that it somehow transforms my output to the string again and that is why I get such result. Is there any idea how to implement so that both solutions give the same result according to the spec above e.g ['<0,0>', '<1,5>', '<1,7>', '<2,1>', '<4,3>', '<7,2>'] ?

UPD Seems I didin't understand the next sentence:

The list should contain the string representation of the Location of a car.

4
  • Why are you returning a string then? Your grader expects a list. That you managed to make your string look like a list shows the grader is too simplistic to detect the difference, but sorted() on a list returns individual characters in sorted order. Commented Aug 1, 2017 at 19:10
  • In other words, I'm pretty sure you should not be returning a single string object. You should be returning a list of strings, each string of the form '<x,y>', so ['<0,0>', '<1,5>', '<1,7>', '<2,1>', '<4,3>', '<7,2>'] (note that there are no double quotes around that value). Commented Aug 1, 2017 at 19:11
  • Seems I don't understand this sentence properly: The list should contain the string representation of the Location of a car. Commented Aug 1, 2017 at 19:13
  • str(i) gives you that. Add those to a list. Commented Aug 1, 2017 at 19:14

1 Answer 1

1

The description of your method is pretty clear where you are going wrong:

""" Returns a list of all cars on the parking. The list should contain 
the string representation of the Location of a car. The list should 
be sorted by the x coordinate of the location. """

You are not returning a list. You are returning a string whose contents look like a list. That's not the same thing.

Return a list instead:

def get_cars(self):
    """ Returns a list of all cars on the parking. The list should contain 
    the string representation of the Location of a car. The list should 
    be sorted by the x coordinate of the location. """

    result = []
    for loc in sorted(self.car_loc_list, key=lambda loc: loc.x):
        result.append(str(loc))
    return result

or more simply with a list comprehension:

def get_cars(self):
    """ Returns a list of all cars on the parking. The list should contain 
    the string representation of the Location of a car. The list should 
    be sorted by the x coordinate of the location. """

    return [str(loc) for loc in sorted(self.car_loc_list, key=lambda loc: loc.x)]
Sign up to request clarification or add additional context in comments.

1 Comment

OMG! Thank you so much. I've spent half of day trying to figure it out because I have no prior coding experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.