Question: Problem 2 : Comprehension & File I / O This problem focuses on implementing functions with list comprehensions. The data is in a file called

Problem 2: Comprehension & File I/O
This problem focuses on implementing functions with list comprehensions. The data is in a file
called family.txt. The data is in two columns as shown in the table below, and also is given as
a visualization. It is a family tree. The names are simply characters, so no type conversion is
necessary.
Table 1: Table (left) from family.txt file, and its visualization (right) as a tree
Parent Child
01
04
05
12
13
56
67
6 A
6 g
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 of the functions required on this problem are
already completed here,
1
2 # children of name
3 def get_child(name,data):
4 return [child for parent,child in data if parent == name]
5
6 #does name have any children
7 def has_children(name,data):
8 return bool(get_child(name,data))
9
10 #get all values in data (cannot help duplicates)
11 def get_all(data):
12 return [k for k in [i for i,j in data]+[j for i,j in data]]
13
14 #data_1 has file contents...
15
16 print(has_children(0,data_1)) #true
17 print(has_children(7,data_1)) #false
18 print(get_child(6,data_1))
19 print(get_all(data_1))
has outputs:
1 True
2 False
3[7,A,g]
4[0,0,0,1,1,5,6,6,6,1,4,5,2,3,6
,7,A,g]
Programming Problem 2: Comprehension & File I/O
Complete the code to read the file. Do not change any stringthese are the names.
Complete each of the functions only using comprehension or bool on a comprehen-
sion
(Don't use AI for this python problem)
#PROBLEM 2
#list comprehension
#INPUT path, filename (remember that you need to form the correct path to read the file)
#OUTPUT list of parent,child pairs
#CONSTRAINT use csv reader
def get_data_1(path, filename):
pass
#input parent name, family data
#output children
#constraint using comprehension
def get_child(name, data):
return [child for parent,child in data if parent == name]
#input parent name, family data
#output true if has children
#constraint using comprehension
def has_children(name, data):
return bool(get_child(name,data))
#input child name, family data
#output parent of child
#constraint using comprehension
def get_parent(name, data):
pass
#input child name1, child name2, family data
#output true if children have same parent
#constraint using comprehension
def siblings(name1,name2,data):
pass
#input grandparent name1, grandchild name2, family data
#output true if name1 is grandparent to name2
#constraint using comprehension
def grandparent(name1,name2,data):
pass
#input family data
#output all names
#constraint comprehension list only
def get_all(data):
pass
#input name1, name2, family data
#output true if name1 and name 2 are cousins, i.e., have the same grandparents
def cousins(name1,name2,data):
pass

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!