narimiran / checkio

my solutions for the problems on checkio.org
9 stars 7 forks source link

Hi, about wrong-family.py #1

Open Kamilet opened 6 years ago

Kamilet commented 6 years ago

I got a problem in this: checkio and found your solution here.

That isn't working now, but gave me a lot of inspiration 😄 , Thanks a lot.

Some new situations in checkio here may assert error:

` assert is_family([["Logan","Mike"],["Alexander","Jack"],["Jack","Logan"]]) == True, '!'

assert is_family([["Logan","Mike"],["Alexander","Jack"],["Jack","Alexander"]]) == False, '!'

assert is_family([["Logan","William"],["William","Jack"],["Jack","Mike"],["Mike","Alexander"]]) == True, '!'

assert is_family([["Logan","William"],["Mike","Alexander"],["William","Alexander"]]) == False, '!'`

Improved new solution here.

def is_family(tree):

    tree.sort()
    # son check, every one can be son just 1 time
    son = []
    for f_s in tree:
        son.append(f_s[1])
    if len(son) != len(set(son)):
        print('before loop, end: you can\'t be two man\'s son')
        return False
    # daddy check, every one should be daddy or have a daddy
    family = tree[0]
    tree = tree[1:]
    loop = 0
    while True:
        if loop > len(tree) or len(tree)==0:
            break
        if tree[0][0] in family and tree[0][1] not in family:
            family.append(tree[0][1])
            tree = tree[1:]
            loop = 0
        elif tree[0][1] in family and tree[0][0] not in family:
            family.append(tree[0][0])
            tree = tree[1:]
            loop = 0
        else:
            tree.append(tree[0])
            tree = tree[1:]
            loop += 1
    print('after loop, end, this left:',tree)
    return not len(tree)
narimiran commented 6 years ago

That isn't working now

Thanks for letting me know the test have changed since I solved this. Probably there are some other solutions too which might not work currently, due to improved test cases.

Improved new solution here.

While this might pass the new tests, I wouldn't call it the improved solution (compared to my original one). It lacks the elegance and simplicity of the original.

If I return to solving Checkio tasks regularly, I might consider rewriting my solution, but I wouldn't hold my breath waiting for that to happen.