Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. """
  2. Suppose we have some input data describing a graph of relationships between parents and children over multiple generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique integer identifier.
  3.  
  4. For example, in this diagram, 6 and 8 have a common ancestor of 4.
  5.  
  6. 14 13
  7. | |
  8. 1 2 4 12
  9. \ / / | \ /
  10. 3 5 8 9
  11. \ / \ \
  12. 6 7 11
  13.  
  14. parent_child_pairs_1 = [
  15. (1, 3), (2, 3), (3, 6), (5, 6), (5, 7),
  16. (4, 5), (4, 8),
  17. (4, 9), (9, 11), (14, 4), (13, 12), (12, 9)
  18. ]
  19.  
  20. Write a function that takes the graph, as well as two of the individuals in our dataset, as its inputs and returns true if and only if they share at least one ancestor.
  21.  
  22. Sample input and output:
  23.  
  24. has_common_ancestor(parent_child_pairs_1, 3, 8) => false
  25. has_common_ancestor(parent_child_pairs_1, 5, 8) => true
  26. has_common_ancestor(parent_child_pairs_1, 6, 8) => true
  27. has_common_ancestor(parent_child_pairs_1, 6, 9) => true
  28. has_common_ancestor(parent_child_pairs_1, 1, 3) => false
  29. has_common_ancestor(parent_child_pairs_1, 3, 1) => false
  30. has_common_ancestor(parent_child_pairs_1, 7, 11) => true
  31. has_common_ancestor(parent_child_pairs_1, 6, 5) => true
  32. has_common_ancestor(parent_child_pairs_1, 5, 6) => true
  33.  
  34. Additional example: In this diagram, 4 and 12 have a common ancestor of 11.
  35.  
  36. 11
  37. / \
  38. 10 12
  39. / \
  40. 1 2 5
  41. \ / / \
  42. 3 6 7
  43. \ \
  44. 4 8
  45.  
  46. parent_child_pairs_2 = [
  47. (11, 10), (11, 12), (2, 3), (10, 2), (10, 5),
  48. (1, 3), (3, 4), (5, 6), (5, 7), (7, 8),
  49. ]
  50.  
  51. has_common_ancestor(parent_child_pairs_2, 4, 12) => true
  52. has_common_ancestor(parent_child_pairs_2, 1, 6) => false
  53. has_common_ancestor(parent_child_pairs_2, 1, 12) => false
  54.  
  55. n: number of pairs in the input
  56.  
  57. """
  58.  
  59. parent_child_pairs_1 = [
  60. (1, 3), (2, 3), (3, 6), (5, 6), (5, 7), (4, 5),
  61. (4, 8), (4, 9), (9, 11), (14, 4), (13, 12), (12, 9)
  62. ]
  63.  
  64. parent_child_pairs_2 = [
  65. (11, 10), (11, 12), (2, 3), (10, 2), (10, 5),
  66. (1, 3), (3, 4), (5, 6), (5, 7), (7, 8)
  67. ]
  68.  
  69. # TODO --- Write your abs
  70.  
  71. def has_common_ancestor(pairs, one,two):
  72. ancestors_one = []
  73. ancestors_two = []
  74. helper([one],ancestors_one,pairs)
  75. helper([two],ancestors_two,pairs)
  76.  
  77. for item in ancestors_one:
  78. for item2 in ancestors_two:
  79. if item == item2:
  80. return True
  81. return False
  82.  
  83. print(ancestors_one)
  84. print(ancestors_two)
  85.  
  86. def helper(my_input,ancestors,parent_child_pairs_1):
  87. queue = []
  88. #base case input in my_input is not a child
  89. flag = 0
  90. for node in my_input:
  91. for pair in parent_child_pairs_1:
  92. if node == pair[1]:
  93. flag = 1 # it's has a parent
  94. if (flag == 0):
  95. return
  96. #_______
  97. for node in my_input:
  98. for pair in parent_child_pairs_1:
  99. #every child parent pair where 6 is a child
  100. if node == pair[1]:
  101. queue.append(pair[0])
  102. ancestors.append(pair[0])
  103. helper(queue,ancestors,parent_child_pairs_1)
  104.  
  105. def find(pairs):
  106. my_dict = {}
  107. my_set = set()
  108. ret1 = []
  109. ret2 = []
  110. for pair in pairs:
  111. my_set.add(pair[0])
  112. my_set.add(pair[1])
  113. if pair[1] not in my_dict:
  114. my_dict[pair[1]] = 1
  115. else:
  116. my_dict[pair[1]] += 1
  117.  
  118. for item in my_set:
  119. if item not in my_dict.keys():
  120. ret1.append(item)
  121. for item in my_dict:
  122. if my_dict[item] == 1:
  123. ret2.append(item)
  124. ret = []
  125. ret.append(ret1)
  126. ret.append(ret2)
  127. return ret
  128.  
  129. # print(ret1)
  130.  
  131. # TODO --- Call your function with the test cases from above
  132. print(has_common_ancestor(parent_child_pairs_1, 3, 8))
  133. print(has_common_ancestor(parent_child_pairs_1, 5, 8))
  134. print(has_common_ancestor(parent_child_pairs_1, 6, 8))
  135. print(has_common_ancestor(parent_child_pairs_1, 6, 9))
  136. print(has_common_ancestor(parent_child_pairs_1, 1, 3))
  137. print(has_common_ancestor(parent_child_pairs_1, 3, 1))
  138. print(has_common_ancestor(parent_child_pairs_1, 7, 11))
  139. print(has_common_ancestor(parent_child_pairs_1, 6, 5))
  140. print(has_common_ancestor(parent_child_pairs_1, 5, 6))
  141.  
  142. print(has_common_ancestor(parent_child_pairs_2, 4, 12))
  143. print(has_common_ancestor(parent_child_pairs_2, 1, 6))
  144. print(has_common_ancestor(parent_child_pairs_2, 1, 12))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement