Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CSCI30_Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Marc Yap
CSCI30_Project
Commits
1d86bec5
Commit
1d86bec5
authored
Nov 15, 2019
by
Temujin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added some functions to the classes
parent
f213035d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
8 deletions
+87
-8
family_tree.py
family_tree.py
+87
-8
No files found.
family_tree.py
View file @
1d86bec5
"""Family Tree project for CSCI 30."""
"""Family Tree project for CSCI 30."""
from
collections
import
defaultdict
class
FamilyTree
:
class
FamilyTree
:
"""A family tree."""
"""A family tree.
Family Tree dict format --
key = person
value = [name of child1, name of child2, name of parent]
"""
"""
TODO:
Don't initialize with root.
Find a way to get root afterwards.
Find a way to get isolated nodes.
Problems to consider --
What to do if not rooted?
Multiple roots?
What if the file describes two separate trees at once?
Handling value errors.
Incest and cycles:
> Adding someone already in the tree as a child
Problems taken care of(?) --
Adding child to someone who already has two children.
Questions:
How does a dict compare object keys?
How to deal with multiple trees?
Is there only one root?
"""
def
__init__
(
self
,
root
):
def
__init__
(
self
,
root
):
self
.
family_tree
=
defaultdict
([
None
,
None
])
"""Initialize the family tree.
self
.
family_tree
[
root
]
=
[
None
,
None
]
pass
root: the name of the first generation person.
"""
self
.
family_tree
=
dict
()
self
.
root
=
root
self
.
family_tree
[
root
]
=
[
None
,
None
,
None
]
def
insert_person
(
self
,
person
,
parent
):
"""Add a person as a child to some parent."""
try
:
parent_list
=
self
.
family_tree
[
parent
]
except
KeyError
:
raise
KeyError
(
"Error: The parent is not in the tree."
)
if
parent_list
[
1
]
is
None
:
parent_list
[
1
]
=
person
parent_list
[
person
]
=
[
None
,
None
,
parent
]
elif
parent_list
[
2
]
is
None
:
parent_list
[
2
]
=
person
parent_list
[
person
]
=
[
None
,
None
,
parent
]
else
:
raise
ValueError
(
"Error: That parent already has two children."
)
def
get_parent
(
self
,
name
):
def
get_ancestors
(
self
,
name
):
"""Return a list of all the ancestors of a person."""
ancestors
=
[]
parent
=
self
.
family_tree
[
name
][
2
]
while
parent
is
not
None
:
ancestors
+=
parent
parent
=
self
.
family_tree
[
parent
][
2
]
ancestors
.
sort
()
return
ancestors
def
get_descendants
(
self
,
name
):
"""Return a list of a person's descendants."""
class
Person
:
class
Person
:
"""A person."""
"""A person.
Attributes --
Name: a string indicating the name
Age: a integer indicating the age
Sex: a boolean, male = True, female = False
"""
def
__init__
(
self
,
name
,
age
,
sex
):
def
__init__
(
self
,
name
,
age
,
sex
):
"""Initialize the Person class."""
self
.
name
=
name
self
.
name
=
name
self
.
age
=
age
self
.
age
=
age
self
.
sex
=
sex
self
.
sex
=
sex
def
set_name
(
self
,
name
):
"""Set name."""
self
.
name
=
name
def
set_age
(
self
,
age
):
"""Set age."""
self
.
age
=
age
def
set_sex
(
self
,
sex
):
"""Set sex (bool)."""
self
.
sex
=
sex
def
get_name
(
self
):
def
get_name
(
self
):
"""Get name."""
return
self
.
name
return
self
.
name
def
get_age
(
self
):
def
get_age
(
self
):
"""Get age."""
return
self
.
age
return
self
.
age
def
get_sex
(
self
):
def
get_sex
(
self
):
"""Get sex (bool)."""
return
self
.
sex
return
self
.
sex
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment