Problem 1: Comprehension & File I/O
In this problem, you'll implement functions as comprehensions. The data is in a file called
family.txt, it is shown in the table, and also given as a visualization. It is a family tree. The
names are simply characters, so no type conversion is necessary.
You will implement a series of queries based on the data that you read from the file. The
queries are formed as Python functions. A few are done here:
# children of name
def get_child(name):
return [child for parent,child in data if parent == name]
#does name have any children
def has_children(name):
return bool(get_child(name))
#get all values in data (cannot help duplicates)
def get_all():
return [k for k in [i for i,j in data] + [j for i,j in data]]
print(has_children('0')) #true
print(has_children('7')) #false
print(get_child('6'))
print(get_all())
has outputs:
True
False
['7', 'A', 'g']
['0', '0', '0', '1', '1', '5', '6', '6', '6', '1', '4', '5', '2', '3', '6'
Assignment ?6 file I/O, recursion, regression, simulation
Page 2
,'7', 'A', 'g']
Programming Problem 1: Comprehension & File I/O
Complete the code to read the file. Do not change any string-these are the names
Complete each of the functions only using comprehension or bool on a comprehen-
sion