diff --git a/code/jupyter/HomeworkExercises/FamilyTree.py b/code/jupyter/HomeworkExercises/FamilyTree.py index 35dcbff..92a3538 100644 --- a/code/jupyter/HomeworkExercises/FamilyTree.py +++ b/code/jupyter/HomeworkExercises/FamilyTree.py @@ -56,38 +56,23 @@ def set_family_name(self, name): self.family_tree_name = name def all_men(self): - return self.name_list('male') + pass def all_women(self): - return self.name_list('female') + pass def individuals(self, key='all'): if key not in ['all', 'male', 'female']: raise TypeError - - if key == 'all': - ans = self.members[:] - else: - ans = [] - for person in self.members: - if person.gender == key: - ans.append(person) - return ans + pass def individual(self, name): ans = [] - names = name.strip().split(' ') - for person in self.members: - # see if this one is a candidate ... - if (len(names) == 1) and ((person.first_name() in names) or (person.last_name() in names)): - ans.append(person) - elif (len(names) > 1) and (person.first_name() in names) and (person.last_name() in names): - ans.append(person) + pass return tuple(ans) def add_individual(self, guy): - if guy not in self.members: - self.members.append(guy) + pass def tree_print(self): start = self.find_eldest() @@ -95,40 +80,21 @@ def tree_print(self): tree = "\nFamily Tree Name: {}\n".format(self.family_tree_name) tree += "-----------------------------------------" print(tree) - for lady in start['female']: - self.print_individual(lady) - for gent in start['male']: - self.print_individual(gent) + + % YOUR CODE BELOW # ==== internal use methods ======== def name_list(self, key='all'): - lst = self.individuals(key) ans = [] - for person in lst: - ans.append(person.full_name()) return ans def find_eldest(self): eldest = {'male': [], 'female': []} - for person in self.members: - if not person.parents(): - eldest[person.gender].append(person) return eldest - def print_individual(self, person, level=0): - exclude = [] - if person.partner(): - for partner in person.partner(): - name = level * '>' + level * ' ' + person.full_name() + '\t<=>\t' + partner.full_name() - print(name) - self.print_children(person, partner, level) - exclude += partner.children() - name = level * '>' + level * ' ' + person.full_name() - print(name) - self.print_children(person, person, level, exclusion=exclude) + def print_individual(self, person): + pass def print_children(self, person, partner, level=0, exclusion=[]): - for child in person.children(): - if (child in partner.children()) and (child not in exclusion): - self.print_individual(child, level + 1) + pass diff --git a/code/jupyter/HomeworkExercises/Individual.py b/code/jupyter/HomeworkExercises/Individual.py index 4999db4..8935f26 100644 --- a/code/jupyter/HomeworkExercises/Individual.py +++ b/code/jupyter/HomeworkExercises/Individual.py @@ -4,7 +4,7 @@ # # author: Peter Mackenzie-Helnwein # created: pmh - 10/28/2020 -# modified: +# modified: pmh - 07/28/2024 # # ************************************************************************** @@ -65,26 +65,20 @@ def __str__(self): return s def partner(self): - return tuple(self.the_partner) + pass def parents(self): - return tuple(self.the_parents) + pass def children(self): - return tuple(self.the_children) + pass def add_partner(self, partner): - if partner not in self.the_partner: - self.the_partner.append(partner) - partner.add_partner(self) + pass def add_child(self, child): - if child not in self.the_children: - self.the_children.append(child) - child.add_parent(self) + pass def add_parent(self, parent): - if parent not in self.the_parents: - self.the_parents.append(parent) - parent.add_child(self) + pass diff --git a/code/jupyter/HomeworkExercises/Solutions/FamilyTree.py b/code/jupyter/HomeworkExercises/Solutions/FamilyTree.py new file mode 100644 index 0000000..35dcbff --- /dev/null +++ b/code/jupyter/HomeworkExercises/Solutions/FamilyTree.py @@ -0,0 +1,134 @@ +class FamilyTree(): + """ + managing an entire family + + variables: + self.family_tree_name + self.members = [] + + methods (required): + __init__(self,name='unknown') + __str__(self) + __repr__(self) + family_name(self) + set_family_name(self,name) + all_men(self) + all_women(self) + individuals(self,key='all') + individual(self,name) + add_individual(self,guy) + tree_print(self) + + methods (added to simplify implementation of required methods): + name_list(self,key='all') ... like individuals() but returns names instead pf pointers + find_eldest(self) ... return the oldest male and the oldest female + print_individual(self,person,level=0) ... print information for person. + level for hirachy/indentation + print_children(self,person,partner,level=0,exclusion=[]) + ... print children shared by person and partner. + level for hirachy/indentation + """ + + # ==== overloading default methods ======== + + def __init__(self, name='unknown'): + self.family_tree_name = name + self.members = [] + + def __str__(self): + s = "Family Tree Name: {}\n".format(self.family_tree_name) + s += "-----------------------------------------\n" + cnt = 0 + for person in self.members: + cnt += 1 + s += " {}: {}\n".format(cnt, person.full_name()) + return s + + def __repr__(self): + return "{}(name='{}')".format(self.__class__.__name__, self.family_tree_name) + + # ==== interface methods (required by problem statement) ======== + + def family_name(self): + return self.family_tree_name + + def set_family_name(self, name): + self.family_tree_name = name + + def all_men(self): + return self.name_list('male') + + def all_women(self): + return self.name_list('female') + + def individuals(self, key='all'): + if key not in ['all', 'male', 'female']: + raise TypeError + + if key == 'all': + ans = self.members[:] + else: + ans = [] + for person in self.members: + if person.gender == key: + ans.append(person) + return ans + + def individual(self, name): + ans = [] + names = name.strip().split(' ') + for person in self.members: + # see if this one is a candidate ... + if (len(names) == 1) and ((person.first_name() in names) or (person.last_name() in names)): + ans.append(person) + elif (len(names) > 1) and (person.first_name() in names) and (person.last_name() in names): + ans.append(person) + return tuple(ans) + + def add_individual(self, guy): + if guy not in self.members: + self.members.append(guy) + + def tree_print(self): + start = self.find_eldest() + + tree = "\nFamily Tree Name: {}\n".format(self.family_tree_name) + tree += "-----------------------------------------" + print(tree) + for lady in start['female']: + self.print_individual(lady) + for gent in start['male']: + self.print_individual(gent) + + # ==== internal use methods ======== + + def name_list(self, key='all'): + lst = self.individuals(key) + ans = [] + for person in lst: + ans.append(person.full_name()) + return ans + + def find_eldest(self): + eldest = {'male': [], 'female': []} + for person in self.members: + if not person.parents(): + eldest[person.gender].append(person) + return eldest + + def print_individual(self, person, level=0): + exclude = [] + if person.partner(): + for partner in person.partner(): + name = level * '>' + level * ' ' + person.full_name() + '\t<=>\t' + partner.full_name() + print(name) + self.print_children(person, partner, level) + exclude += partner.children() + name = level * '>' + level * ' ' + person.full_name() + print(name) + self.print_children(person, person, level, exclusion=exclude) + + def print_children(self, person, partner, level=0, exclusion=[]): + for child in person.children(): + if (child in partner.children()) and (child not in exclusion): + self.print_individual(child, level + 1) diff --git a/code/jupyter/HomeworkExercises/HW5-P1.py b/code/jupyter/HomeworkExercises/Solutions/HW5-P1.py similarity index 100% rename from code/jupyter/HomeworkExercises/HW5-P1.py rename to code/jupyter/HomeworkExercises/Solutions/HW5-P1.py diff --git a/code/jupyter/HomeworkExercises/Solutions/Individual.py b/code/jupyter/HomeworkExercises/Solutions/Individual.py new file mode 100644 index 0000000..4999db4 --- /dev/null +++ b/code/jupyter/HomeworkExercises/Solutions/Individual.py @@ -0,0 +1,90 @@ +# ************************************************************************** +# +# class: Individual +# +# author: Peter Mackenzie-Helnwein +# created: pmh - 10/28/2020 +# modified: +# +# ************************************************************************** + + +from Human import * + + +class Individual(Human): + """ + Modify Individual to incorporate parent information + + variables: + self.the_partner = [] + self.the_children = [] + self.the_parents = [] + + variables (inherited): + self.firstname + self.lastname + self.gender + self.date_of_birth + + methods: + __init__(self,first='unknown',last='unknown',gender='unknown',dob='2000-01-01') + __str__(self) + partner(self) + children(self) + add_partner(self, partner) + add_child(self, child) + parents(self) + add_parent(self, parent) + + methods (inherited BUT overloaded/hidden): + __init__(self,first='unknown',last='unknown',gender='unknown',dob='2000-01-01') + __str__(self) + + methods (inherited, not overloaded): + __repr__(self) + first_name(self) + last_name(self) + full_name(self) + """ + + def __init__(self,first='unknown',last='unknown',gender='unknown',dob='2000-01-01'): + super().__init__(first=first,last=last,gender=gender,dob=dob) + self.the_partner = [] + self.the_children = [] + self.the_parents = [] + + def __str__(self): + s = super().__str__() + if self.the_parents: + s += "Parents: {}\n".format([ parent.full_name() for parent in self.the_parents ]) + if self.the_partner: + s += "Partner(s): {}\n".format([ partner.full_name() for partner in self.the_partner ]) + if self.the_children: + s += "Children: {}\n".format([ child.full_name() for child in self.the_children ]) + return s + + def partner(self): + return tuple(self.the_partner) + + def parents(self): + return tuple(self.the_parents) + + def children(self): + return tuple(self.the_children) + + def add_partner(self, partner): + if partner not in self.the_partner: + self.the_partner.append(partner) + partner.add_partner(self) + + def add_child(self, child): + if child not in self.the_children: + self.the_children.append(child) + child.add_parent(self) + + def add_parent(self, parent): + if parent not in self.the_parents: + self.the_parents.append(parent) + parent.add_child(self) +