Created
March 16, 2018 18:02
-
-
Save abeforgit/06fafcb568cbbfd5fa582283cfed7f5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Dict, Set | |
def familieleden(path: str) -> Dict[str, Dict]: | |
famlist = [] | |
rslt = {} | |
with open(path) as file: | |
for line in file: | |
famlist.append(line.split()) | |
for rel in famlist: | |
mother, father, *children = rel | |
if mother in rslt.keys(): | |
rslt[mother]['kinderen'] = set(children) | |
else: | |
rslt[mother] = {'kinderen': set(children)} | |
if father in rslt.keys(): | |
rslt[father]['kinderen'] = set(children) | |
else: | |
rslt[father] = {'kinderen': set(children)} | |
for child in children: | |
if child in rslt.keys(): | |
rslt[child]['moeder'] = mother | |
rslt[child]['vader'] = father | |
else: | |
rslt[child] = {'moeder': mother, 'vader': father} | |
return rslt | |
def voorouder(naam: str, voorouder: str, reldict: Dict[str, Dict]) -> str: | |
queue = [] | |
for i in range(0, len(voorouder), 3): | |
queue.append(voorouder[i:i + 3]) | |
cur: str = naam | |
for item in queue: | |
try: | |
if item == 'far': | |
cur = reldict[cur]['vader'] | |
elif item == 'mor': | |
cur = reldict[cur]['moeder'] | |
except KeyError: | |
raise AssertionError("onbekende voorouder") | |
return cur | |
def nakomelingen(fam: str, gens: int, rel: Dict[str, Dict]) -> Dict[str, Set]: | |
return generate_desc(fam, '', gens, 1, rel, {}, '') | |
def generate_desc(fam: str, farmorstr: str, gens: int, current_depth: int, rel: Dict[str, Dict], | |
rslt: Dict[str, Set], farmornew: str) -> Dict[str, Set]: | |
try: | |
farmorstr = farormor(next(iter(rel[fam]['kinderen'])), fam, rel) + farmorstr | |
except KeyError: | |
pass | |
if current_depth == gens: | |
if farmorstr in rslt.keys(): | |
rslt[farmorstr] = rslt[farmorstr].union(rel[fam]['kinderen']) | |
else: | |
try: | |
rslt[farmorstr] = rel[fam]['kinderen'] | |
except KeyError: | |
pass | |
return rslt | |
else: | |
try: | |
for child in iter(rel[fam]['kinderen']): | |
generate_desc(child, farmorstr, gens, current_depth + 1, rel, rslt, farmornew) | |
except KeyError: | |
pass | |
return rslt | |
def farormor(name: str, parent: str, rel: Dict[str, Dict]) -> str: | |
if parent == rel[name]['vader']: | |
return 'far' | |
else: | |
return 'mor' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment