GrandParents : For fun :) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: GrandParents : For fun :) (/thread-7519.html) Pages:
1
2
|
GrandParents : For fun :) - William049 - Jan-14-2018 [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 :) RE: GrandParents : For fun :) - Gribouillis - Jan-14-2018 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. RE: GrandParents : For fun :) - William049 - Jan-14-2018 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. RE: GrandParents : For fun :) - Gribouillis - Jan-14-2018 Why not try def grandparents(num3): result = [] for x in ascendant(num3): result.extend(ascendant(x)) return result RE: GrandParents : For fun :) - William049 - Jan-14-2018 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 RE: GrandParents : For fun :) - Gribouillis - Jan-14-2018 (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! RE: GrandParents : For fun :) - William049 - Jan-14-2018 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 RE: GrandParents : For fun :) - Gribouillis - Jan-14-2018 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)) RE: GrandParents : For fun :) - William049 - Jan-14-2018 Ok you learn me a function :) Thanks :) Now i can do great-great-great-great-grandparents! lol :) RE: GrandParents : For fun :) - Gribouillis - Jan-14-2018 You could simplify all this by adding a level parameter to ascendant() , so level=1 would return the parents, level=2 the grandparents, etcdef 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) |