1

I am making a 'cutest pet bracket' for my company while on quarantine, and I am confused as how to update the competitors of subsequent rounds after one round is played (somebody wins).

Say my winner is 3 and the round after is:

[[3, 14], [6, 11]]

and the round after is:

[[2, [7, 10]], [[3, 14], [6, 11]]]

I want to replace the children nodes with the winner in them to reflect the fact that now just the winner remains:

[[3, 14], [6, 11]] --> [3, [6, 11]]

[[2, [7, 10]], [[3, 14], [6, 11]]] --> [[2, [7, 10]], [3, [6, 11]]]

I came up with some code but it doesn't work properly

[[3, 14], [6, 11]] --> [[3], [6, 11]] (close, but no cigar)

[[2, [7, 10]], [[3, 14], [6, 11]]] --> [[2, [7, 10]], [[3, 14], [6, 11]]] (no change)

    def traverse(traversed_element_of_list_of_lists, winner):
        rebuild = []
        if isinstance(traversed_element_of_list_of_lists, list):
            for value in traversed_element_of_list_of_lists:
                if isinstance(value, int):
                    continue#rebuild.append(value)
                if winner in value:
                    rebuild.append([winner])
                else:
                    rebuild.append(value)
                traverse(value, winner)
        return rebuild

There are plenty of answers on SO about replacing item in nested list of defined shape, and there are answers about how to traverse and flatten a nested list of arbitrary depth, but I cannot seem to find anything about how to replace an item in a list of arbitrary depth.

Thank you for help!

1 Answer 1

1

Your recursion has 3 cases:

  1. The base case, in which the nested object is not really nested, it's just a number. Return that directly.
  2. The special case, where the winner is in the list, in which case you just want the single winner number returned.
  3. The recursive case, in which you return a new list by traversing each element recursively.

Bring it together as a recursive function:

def traverse(nested, winner):
    if not isinstance(nested, list):
        return nested

    if winner in nested:
        return winner

    return [traverse(o, winner) for o in nested]

This produces the desired result on your examples.

traverse([[3, 14], [6, 11]], 3)                                                                                                                                                                                                     
# [3, [6, 11]]

traverse([[2, [7, 10]], [[3, 14], [6, 11]]], 3)                                                                                                                                                                                     
# [[2, [7, 10]], [3, [6, 11]]]
Sign up to request clarification or add additional context in comments.

2 Comments

you are a superstar
You're going to make me blush! I just love recursion :)

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.