mukherjee

amoun

Apr 26th, 2022
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. '''
  2. 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.
  3.  
  4. For example, in this diagram, 3 is a child of 1 and 2, and 5 is a child of 4:
  5.  
  6. 1   2    4           30
  7. \ /   /  \          \
  8.  3   5    9  15      16
  9.   \ / \   \ /
  10.    6   7   12  
  11.  
  12.  
  13. full list = [1,2 4, 30,, 15,, ]
  14. sublist = [3,5,9,16.6.7.12]
  15.  
  16. Sample input/output (pseudodata):
  17.  
  18. pairs = [
  19.    (5, 6), (1, 3), (2, 3), (3, 6), (15, 12),
  20.    (5, 7), (4, 5), (4, 9), (9, 12), (30, 16)
  21. ]
  22.  
  23.  
  24. 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.
  25.  
  26.  
  27. Output may be in any order:
  28.  
  29. findNodesWithZeroAndOneParents(pairs) => [
  30.  [1, 2, 4, 15, 30],   // Individuals with zero parents
  31.  [5, 7, 9, 16]        // Individuals with exactly one parent
  32. ]
  33.  
  34. Complexity Analysis variables:
  35.  
  36. n: number of pairs in the input
  37. '''
  38.  
  39. pairs = [
  40.     (5, 6), (1, 3), (2, 3), (3, 6), (15, 12),
  41.     (5, 7), (4, 5), (4, 9), (9, 12), (30, 16)
  42. ]
  43.  
  44. # Iterate with each child, and count the number parents for unique parent
  45. def my_func(pairs):
  46.     #Check for zero parents
  47.     items_in_all = []
  48.     items_in_second = []
  49.     parent_list = []
  50.     for pair in pairs:
  51.         items_in_second.append(pair[1])
  52.         items_in_all.append(pair[0])
  53.         items_in_all.append(pair[1])
  54.     print(items_in_second, items_in_all)
  55.     all_set = set(items_in_all)
  56.     second_set = set(items_in_second)
  57.     print(all_set, second_set)
  58.     common_set = all_set.intersection(second_set)
  59.     print(common_set)
  60.     for x in list(all_set):
  61.         if x not in common_set:
  62.             parent_list.append(x)
  63.     print(parent_list)
  64.     parent_list2
  65.     for pair in pairs:
  66.         item_ = pair[1]
  67.         for pair2 in pairs:
  68.            
  69. #    for i,pair in enumerate(pairs):
  70. #        item_pos1 = pair[0]
  71.  
  72. #        for i2, tuple_no in pairs:
  73.            
  74.     #return (zero_list, one_parent_list)
  75. my_func(pairs)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment