''' Created on Feb 3, 2015 @author: Brett Paufler Copyright Brett Paufler ''' #family_tree is a list of the form # [('Person's Name', 'Sex', 'Mommy', 'Daddy'), # ('Person's Name', 'Sex', 'Mommy', 'Daddy'), # ...] # # Sex is 'Boy' or 'Girl' # Name is of format 'First Last' # Mommy & Daddy have same format as Name family_tree = [**redacted**] def are_brothers((a,b,c,d),(w,x,y,z)): '''True if two boys have the same parents ''' return all([b=='Boy', x=='Boy',c==y,d==z]) def are_sisters((a,b,c,d),(w,x,y,z)): '''True if two girls have the same parents ''' return all([b=='Girl', x=='Girl',c==y,d==z]) def are_siblings((a,b,c,d),(w,x,y,z)): '''True if share the same parents ''' return all([c==y,d==z]) def list_all_brothers(data): '''returns a list of all brothers in data list ''' return [(a,w) for (a,b,c,d) in family_tree for (w,x,y,z) in family_tree if are_brothers((a,b,c,d),(w,x,y,z)) and a < w] def list_all_ancestors((a,b,c,d),family): '''returns a list of ancestors for subject person ''' ancestors = set([]) parents = [c,d] while parents: p = parents.pop() p = [(a,b,c,d) for (a,b,c,d) in family_tree if a==p] if p: a,b,c,d = p[0] ancestors.add((a,b,c,d)) parents += [c,d] return ancestors def are_related(a,b,family=family_tree): '''returns true if a and b share a common ancestor from list_all_anscestors ''' aF = set(list_all_ancestors(a,family)) aF.add(a) bF = set(list_all_ancestors(b,family)) bF.add(b) if aF.intersection(bF): return True else: return False a = list_all_ancestors(family_tree[0], family_tree) #print list_all_brothers(family_tree) print a for ab in a: print ab ''' #print are_brothers(family_tree[0],family_tree[1]) #print are_brothers(family_tree[2],family_tree[1]) #print are_sisters(family_tree[0],family_tree[1]) #print are_sisters(family_tree[2],family_tree[1]) siblings = [(a,x) for (a,b,c) in family_tree for (x,y,z) in family_tree if b==y and c==z] print siblings siblings = [(a,b) for (a,b) in siblings if a !=b] print siblings siblings += [(b,a) for (a,b) in siblings] siblings = set(siblings) print sibling '''