Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Suppose we have some input data describing a graph of relationships between parents and children over multiple families and generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique positive integer identifier.
- For example, in this diagram, 3 is a child of 1 and 2, and 5 is a child of 4:
- 1 2 4 30
- \ / / \ \
- 3 5 9 15 16
- \ / \ \ /
- 6 7 12
- full list = [1,2 4, 30,, 15,, ]
- sublist = [3,5,9,16.6.7.12]
- Sample input/output (pseudodata):
- pairs = [
- (5, 6), (1, 3), (2, 3), (3, 6), (15, 12),
- (5, 7), (4, 5), (4, 9), (9, 12), (30, 16)
- ]
- Write a function that takes this data as input and returns two collections: one containing all individuals with zero known parents, and one containing all individuals with exactly one known parent.
- Output may be in any order:
- findNodesWithZeroAndOneParents(pairs) => [
- [1, 2, 4, 15, 30], // Individuals with zero parents
- [5, 7, 9, 16] // Individuals with exactly one parent
- ]
- Complexity Analysis variables:
- n: number of pairs in the input
- '''
- pairs = [
- (5, 6), (1, 3), (2, 3), (3, 6), (15, 12),
- (5, 7), (4, 5), (4, 9), (9, 12), (30, 16)
- ]
- # Iterate with each child, and count the number parents for unique parent
- def my_func(pairs):
- #Check for zero parents
- items_in_all = []
- items_in_second = []
- parent_list = []
- for pair in pairs:
- items_in_second.append(pair[1])
- items_in_all.append(pair[0])
- items_in_all.append(pair[1])
- print(items_in_second, items_in_all)
- all_set = set(items_in_all)
- second_set = set(items_in_second)
- print(all_set, second_set)
- common_set = all_set.intersection(second_set)
- print(common_set)
- for x in list(all_set):
- if x not in common_set:
- parent_list.append(x)
- print(parent_list)
- parent_list2
- for pair in pairs:
- item_ = pair[1]
- for pair2 in pairs:
- # for i,pair in enumerate(pairs):
- # item_pos1 = pair[0]
- # for i2, tuple_no in pairs:
- #return (zero_list, one_parent_list)
- my_func(pairs)
Advertisement
Add Comment
Please, Sign In to add comment