Re: Sorting a double linked list

Posted 25 March 2013 - 02:27 PM

Heres the error Im getting:
File "d_linked_list.py", line 170, in sort
for index in range(len(self)):
builtins.TypeError: object of type 'd_linked_list' has no len()

I get the same error for both tests when I run them.
For the def sort(self), I want to sort a d-list in ascending order and for def sort by_att(self, attar):, I want to sort the list by either price or quantity
Im hoping you can look at my sort functions and tell me what Im doing wrong.

Re: Sorting a double linked list

Posted 25 March 2013 - 02:37 PM

def sort(self):
for index in range(len(self)):
smallindex=index
for i in range(index,len(self)):
if self[i]<self[smallindex]:

How exactly does len(self) work with your class? How does self[i] work? If you want that kind of functionality, you could write it. You haven't. Though you do have some methods you might consider calling. Note, sorting a linked list based on index position is probably the worst possible choice.

Re: Sorting a double linked list

Not quite. Keep in mind, you're swapping "values" In a selection sort, there are two ways this is done; at the time or at the end of the inner loop, the latter being much more efficient.

Let's go back to your first sort:

# we're going to sort a list a, for demonstration purposes.
# def sort(self):
def selSort(a):
# for each item
# for index in range(len(self)):
# note, a proper selection sort stops one from the end
for i in range(len(a)-1):
# look at what you're doing here
# you're holding a reference to index i
selected = i
# you actually want to loop from i+1 to the end
# for i in range(index,len(self)):
for j in range(i+1,len(a)):
# compare the selected value to the inner loop value
if a[j]<a[selected]:
# if the inner loop value is smaller
# save the INDEX
selected = j
# you leave the inner loop, it's swap time
# however, don't swap unless selected has changed from i
if not selected==i:
# we are swapping values
tempVal = a[i] # hold so we don't loose
a[i] = a[selected] # replace with selected value
a[selected] = tempVal # complete swap

Back to a linked list. There is no len, but using head and last node as your start and stop values, it's pretty much the same logic.

The next line in the code I gave would be:

if j.value < selected.value:
selected = j # understand? We're hold the "index." The node with the value we want to swap with node i.

Re: Sorting a double linked list

You know, you still have to write the rest of that sort. You have swaps to make, nodes to move...

The integer_list.get_size() doesn't really make sense out of context. So, context:

# this is essentially one pass of a bubble sort
# if you have to make a swap, you fail
prev_data = integer_list.pop(0) # get the first value
cur_data = integer_list.pop(0) # get the second value
# this is actually pretty sloppy, we're assuming more than two items
while integer_list.get_size() > 0: # basically, we're just testing if there are more items
# this check should to make sure that prev_data <= cur_data is true
# as a sort should have made it
# however, the = in >= is a bug and will fail if two values are the same, which should be valid
assert cur_data >= prev_data, 'error: linked list is not sorted'
# move forward
# the current is now the previous
prev_data = cur_data
# the newly popped value is current
cur_data = integer_list.pop(0)

Note, if the last value is out of order, this code will give you a pass.

Re: Sorting a double linked list

Posted 27 March 2013 - 09:45 AM

This was quite a hard exercise for me and my classmates so my prof decided to forget about it. But im so frustrated now because Ive tried a bunch of things and I cant figure it out. Is it possible if you can show me your completed code and explain to me how you got it one last time if you have time.