Python Forum
GrandParents : For fun :)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GrandParents : For fun :)
#1
[FOR FUN]
Hello everyone, I had the idea yesterday to improve my program (genealogical tree) which currently displays on the screen the ascendants, descendants, frere/sister of a person indicated in parameter.
Unfortunately this idea did not bear fruit as I find it a bit complex level of verification.

My ascendants program :
def ascendant(num3):
    ascendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][1]):
            ascendants.append(listeparentes[i][0])
    return (ascendants)
My descendant program :

def descendants(num3):
    descendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][0]):
            descendants.append(listeparentes[i][1])
    return (descendants)
I call them with :
def selectionnumero(nom, prenom):
    for i in range(len(listepersonnes)):
        if (nom == listepersonnes[i][0]) and (prenom == listepersonnes[i][1]):
            return (i)
            nomchoix = input("\033[31mSaisir le Nom : \033[0m")
            prenomchoix = input("\033[31mSaisir le Prenom : \033[0m")
            num1 = selectionnumero(nomchoix, prenomchoix)
            print("\033[31mLes descendants connus de",nomchoix, prenomchoix, "sont : \033[0m")
Actually , it's confused :

def grandparents(grandparents):
    grandparents = []
    for i in range(len(listeparentes)):
        if ((Ascendants[0] == listeparentes[i][0]) or (Ascendants[1] == listeparentes[i][0])) and ....                ))
The idea is to ask for the seizure of a person as usual and to display the name of his grandfather if he has one and to show that he does not have one if he does not.
Thanks for fun :)
Reply
#2
If you know how to find the parents (is this the ascendant function?), you only need to call the same function for every parent found and find the parents of the parents.
Reply
#3
In order to find parents yes it's the ascendant function
(Exemple : JAMES is the father of KEVIN )
So i enter KEVIN and it return me JAMES :)


def ascendant(num3):
    ascendants = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][1]):
            ascendants.append(listeparentes[i][0])
    return (ascendants)
def grandfather(num3):
    grandfather = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][1]):
            grandfather.append(listeparentes[i][0])
        for i in range(len(listeparentes)):
           if (num3 == listeparentes[i][1]):
               grandfather.append(listeparentes[i][0])
    return (grandfather)
Or ?????

def grandfather(num3):
    grandfather = []
    for i in range(len(listeparentes)):
        if (num3 == listeparentes[i][1]):
            grandfather.append(listeparentes[i][0])
        for i in range(len(grandfather)):
           if (num3 == grandfather[i][1]):
    return (grandfather)
I'm not sure at all..
To find the parents of my parents i understand i can use the same function but i'm not at all sure for what i did.
Reply
#4
Why not try
def grandparents(num3):
    result = []
    for x in ascendant(num3):
        result.extend(ascendant(x))
    return result
Reply
#5
OMG i think you are just a genius...
I never thought this feature would be so short to write.

http://prntscr.com/i07b9x

So if i want to find great-grandparents i should only do that right ? :
def arrieregrandparents(num3):
    result2 = []
    for x in grandparents(num3):
        result2.extend(grandparents(x))
    return result2
Reply
#6
(Jan-14-2018, 08:58 AM)William049 Wrote: So if i want to find great-grandparents i should only do that right ? :
That would return the great-great-grandparents!
Reply
#7
Yes sry, so know that should only return great-grandparents!
def arrieregrandparents(num3):
    result2 = []
    for x in grandparents(num3):
        result2.extend(ascendant(x))
    return result2
Reply
#8
Exactly. Note that these functions may return several times the same person, for example if both parents have a common grand parent. You can avoid this with
return list(set(result))
Reply
#9
Ok you learn me a function :)
Thanks :) Now i can do great-great-great-great-grandparents! lol :)
Reply
#10
You could simplify all this by adding a level parameter to ascendant(), so level=1 would return the parents, level=2 the grandparents, etc
def ascendant(num, level=1):
    assert level >= 1
    if level == 1:
        ascendants = []
        for i in range(len(listeparentes)):
            if (num3 == listeparentes[i][1]):
                ascendants.append(listeparentes[i][0])
        return ascendants
    else:
        result = set()
        for a in ascendant(num, level - 1):
            result.update(ascendant(a))
        return list(result)
    
def grandparents(num):
    return ascendant(num, 2)

def greatgrandparents(num):
    return ascendant(num, 3)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020