Question: Problem 2 : Comprehension & File I / O This problem focuses on implementing functions with list comprehensions. The data is in a file called
Problem : Comprehension & File IO
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 : Table left from family.txt file, and its visualization right as a tree
Parent Child
A
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,
# children of name
def getchildnamedata:
return child for parent,child in data if parent name
#does name have any children
def haschildrennamedata:
return boolgetchildnamedata
#get all values in data cannot help duplicates
def getalldata:
return k for k in i for ij in dataj for ij in data
#data has file contents...
printhaschildrendata #true
printhaschildrendata #false
printgetchilddata
printgetalldata
has outputs:
True
False
Ag
Ag
Programming Problem : Comprehension & File IO
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
Dont use AI for this python problem
#PROBLEM
#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 getdatapath filename:
pass
#input parent name, family data
#output children
#constraint using comprehension
def getchildname 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 haschildrenname data:
return boolgetchildnamedata
#input child name, family data
#output parent of child
#constraint using comprehension
def getparentname data:
pass
#input child name child name family data
#output true if children have same parent
#constraint using comprehension
def siblingsnamenamedata:
pass
#input grandparent name grandchild name family data
#output true if name is grandparent to name
#constraint using comprehension
def grandparentnamenamedata:
pass
#input family data
#output all names
#constraint comprehension list only
def getalldata:
pass
#input name name family data
#output true if name and name are cousins, ie have the same grandparents
def cousinsnamenamedata:
pass
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
