0

I was working on the sorting but I'm not able to call the function with the specific way.

Basically, what I want to do is to create a function that takes a list of object Node with attribute Value and returns a list with the items from the original list stored into sublists. Items of the same value should be in the same sublist and sorted in descending order.

For continuing the code I want to know what should be the parameter of this.

def advanced_sort(<What will come here according to the call>):

Function call:

advanced_sort([Node(1), Node(2), Node(1),Node(2)])

Can anyone please help me out with the code? Thanks in advance.

8
  • Have you tried: advanced_sort(list=[Node(1), Node(2), Node(1),Node(2)]) This instantiates the Nodes as a list in the function. def advanced_sort(list=None): <- as code advanced_sort(list=[Node(1), Node(2), Node(1),Node(2)]) <- as a call Commented Oct 8, 2019 at 13:06
  • I don't want a call specific as next time the function can be called something like advanced_sort([Node(3), Node(2), Node(1),Node(2)]) Then there will be a problem Commented Oct 8, 2019 at 13:09
  • Also, the Node is not defined. Commented Oct 8, 2019 at 13:11
  • 1
    Your function signature just needs to be def advanced_sort(nodes): Commented Oct 8, 2019 at 13:22
  • @quamrana I tried but seems this is not the way. Commented Oct 8, 2019 at 13:28

3 Answers 3

1

advanced_sort takes a single argument: a list (or possibly an arbitrary iterable). As such, the signature only has one argument:

def advanced_sort(nodes):

Ignoring type hints, the signature does not and cannot reflect the internal structure of the single argument; it's just a name to refer to the passed value inside the body of the function.

Inside the body, you can write code that assumes that nodes is a list, and that further each element of the list is a Node instance, so that you can do things like assume each value as a Value attribute.

def advanced_sort(nodes):
    # If nodes is iterable, then x refers to a different
    # element of the iterable each time through the loop.
    for x in nodes:
        # If nodes is a list of Node instances, then
        # x is a Node instance, and thus you can access
        # its Value attribute in the normal fashion.
        print("Found value {}".format(x.Value))

Assuming a definition of Node like

class Node:
    def __init__(self, v):
        self.Value = v

the above definition of advanced_sort will produce the following output:

>>> advanced_sort([Node(3), Node(2), Node(1),Node(2)])
Found value 1
Found value 2
Found value 3
Found value 4
Sign up to request clarification or add additional context in comments.

8 Comments

I am bound to call the function the specific way as I mentioned advanced_sort([Node(1), Node(2), Node(1),Node(2)]) or advanced_sort([Node(3), Node(2), Node(1),Node(2)])
The function should take the Node object and its attributes from the call
That is exactly how you do call the function. nodes will refer to the single list value you provides. [Node(1), Node(2), Node(1), Node(2)] is a single value.
Man, I need to exactly call the function this way advanced_sort([Node(3), Node(2), Node(1),Node(2)]). Don't ignore the Node term in a call.
I am not ignoring the Node terms. Have you actually read my answer? The variable x in the for loop will refer to each individual Node in the list.
|
1

The argument is a single iterable object such as a list, a tuple, a set, ...

Then you iterate on the items as in chepner's response.

For exemple you can use a dictionary to group the Nodes by value:

def advanced_sort(node_list): 
    ret = dict() 
    for node in node_list: 
        if node.value not in ret.keys(): 
            ret[node.value] = list() 
        ret[node.value].append(node)

     return [ret[value] for value in sorted(ret.keys(), reverse=True)] #descending order
advanced_sort([Node(3), Node(2), Node(1),Node(1)])
>>> [[Node(3)], [Node(2)], [Node(1),Node(1)]] 

1 Comment

I know there are a lot type to work around, but the problem with my case is I'm bound to call the function this way only- advanced_sort([Node(3), Node(2), Node(1),Node(2)])
0

Are you able to make changes to the Node class? In that case, you could do something like this:

from functools import total_ordering


@total_ordering
class Node:

    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        if not isinstance(other, Node):
            return NotImplemented
        return self.value == other.value

    def __lt__(self, other):
        if not isinstance(other, Node):
            return NotImplemented
        return self.value < other.value

    def __str__(self):
        return f"({self.value})"


def main():

    from itertools import groupby


    nodes = [Node(1), Node(2), Node(1), Node(2)]
    nodes_sorted = sorted(nodes, reverse=True)
    nodes_sublists = [list(group) for key, group in groupby(nodes_sorted)]

    for sublist in nodes_sublists:
        print(*map(str, sublist))

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

Output:

(2) (2)
(1) (1)

1 Comment

The function call is specific can't change the way of call.

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.