1

The task is to code a sorting algorithm using the below code as a starting point. The issue is I cannot seem to figure out how I go about starting the code, I'm not looking for the full solution - just techniques in how to sort lists of tuples that are actually part of an object. I get errors when I try to iterate through the list, saying cannot iterate through an object.

class LinkedList:
    def __init__(self, data):
        self.label = data[0][0]
        self.value = data[0][1]
        self.tail = None if (len(data) == 1) else LinkedList(data[1:])

countries = LinkedList([("Ukraine",41879904),("Brunei",442400),("Christmas Island (Australia)",1928)
2
  • 1
    I get errors when I try to iterate through the list saying can not iterate through an object Show us. Commented Jun 4, 2020 at 15:34
  • 'for x in countries: for y in x: print(countries)' Traceback (most recent call last): File "C:/Users/cheet/.PyCharmCE2019.3/config/scratches/scratch_2.py", line 11, in <module> for x in countries: TypeError: 'LinkedList' object is not iterable Commented Jun 4, 2020 at 15:38

3 Answers 3

1

You can use a pointer to iterate through linked list.:

curr = countries

while curr:
    print("Label {}, Value {}".format(curr.label, curr.value))
    curr = curr.tail

In order to sort linked list, firstly, you need to implement helper functions to remove/insert a node to given linked list at certain position. Once you have such methods, you can implement any of the famous sorting algorithms (e.g quick sort) using your helper methods you just created.

Sign up to request clarification or add additional context in comments.

Comments

0

To iterate over this list you need to keep getting the tail reference of the object:

class LinkedList:
  def __init__(self, data):
    self.label = data[0][0]
    self.value = data[0][1]
    self.tail = None if (len(data) == 1) else LinkedList(data[1:])

countries = LinkedList([("Ukraine",41879904),("Brunei",442400),("Christmas Island (Australia)",1928)])
nextObj = countries

while nextObj is not None:
  print(nextObj.label, nextObj.value)
  nextObj = nextObj.tail

print("done!")

output:

Ukraine 41879904
Brunei 442400
Christmas Island (Australia) 1928
done!

To get an element at a certain index, you start iterating from the first element and just keep a counter.

Comments

0

functools, @total_ordering

One of the powerful features in python. you can sort objects in a classic and easy straitforward way. Functools module in python helps in implementing higher-order functions. Higher-order functions are dependent functions that call other functions. Total_ordering provides rich class comparison methods that help in comparing classes without explicitly defining a function for it. So, It helps in the redundancy of code.

There are 2 essential conditions to implement these comparison methods

At least one of the comparison methods must be defined from lt(less than), le(less than or equal to), gt(greater than) or ge(greater than or equal to).

The eq function must also be defined

from functools import total_ordering

@total_ordering
class LinkedList(object):
    def __init__(self, data):
        self.label = data[0][0]
        self.value = data[0][1]

    def __lt__(self, other):
        return self.label < other.value  ##lets assume that the sort is based on label data member(attribute)

    def __eq__(self, other):
        return self.label == other.value

##I dont know what data is. just pass your data objects to list of LinkedList objects. and sort it with sorted method (treat them like int objects)! 
my_list = [LinkedList(data0),LinkedList(data1),LinkedList(data2)]

new_list=my_list.sorted()
for obj in new_list:
    print(...)

Comments

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.