I found an interesting problem on https://www.codewars.com/kata/5ae8e14c1839f14b2c00007a:
# Idea

In the world of graphs exists a structure called "spanning tree". It is unique because it's created not on its own, but based on other graphs. To make a spanning tree out of a given graph you should remove all the edges which create cycles, for example:
```
This can become      this      or      this        or       this

 A                    A                  A                   A
 |\                   |                   \                  |\
 | \       ==>        |                    \                 | \
 |__\                 |__                 __\                |  \
B   C                 B  C               B  C                B  C
```
Each *edge* (line between 2 *vertices*, i.e. points) has a weight, based on which you can build minimum and maximum spanning trees (sum of weights of vertices in the resulting tree is minimal/maximal possible).  
[Wikipedia article](https://en.wikipedia.org/wiki/Spanning_tree) on spanning trees, in case you need it.

___

# Task

You will receive an array like this: `[["AB", 2], ["BC", 4], ["AC", 1]]` which includes all edges of an arbitrary graph and a string `"min"`/`"max"`. Based on them you should get and return a new array which includes only those edges which form a minimum/maximum spanning trees.
```python
edges = [("AB", 2), ("BC", 4), ("AC", 1)]

make_spanning_tree(edges, "min")    ==>    [("AB", 2), ("AC", 1)]
make_spanning_tree(edges, "max")    ==>    [("AB", 2), ("BC", 4)]
```

___

# Notes

* All vertices will be connected with each other
* You may receive cycles, for example - `["AA", n]`
* The subject of the test are these 3 values: number of vertices included, total weight, number of edges, but **you should not return them**, there's a special function which will analyze your output instead

I tried it in Python, but could not do it. Can you solve it?
def make_spanning_tree(edges, t):
    edges.sort(reverse=True)
    last_node = ord(edges[0][0][0]) - 65
    edges.sort()
    edges.sort(key=lambda edges: edges[1])

    if(t == 'max'):
        edges.reverse()
    
    nonlocal parent
    parent = [int(x) for x in range(last_node+10000)]
    result = []

    for edge in edges:
        startNode = ord(edge[0][0])-65
        endnode = ord(edge[0][1])-65

        if(Find(startNode) != Find(endnode)):
            Union(startNode, endnode)
            result.append(edge)

    return result

def Find(x):
    if(x == parent[x]):
        return x
    else:
        p = Find(parent[x])
        parent[x] = p
        return p

def Union(x, y):
    x = Find(x)
    y = Find(y)

    if(x != y):
        parent[y] = x