# Untitled

May 1st, 2021
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def transitive_cloure(array):
2.
3.     new_list = [set(array.pop(0))]  # initialize first set with value of index `0`
4.
5.     for item in array:
6.         for i, s in enumerate(new_list):
7.             if any(x in s for x in item):
8.                 new_list[i] = new_list[i].union(item)
9.                 break
10.         else:
11.             new_list.append(set(item))
12.     return new_list
13.
14.
15. def countGroups(arr):
16.
17.     inp = [list(x) for x in arr]
18.     size = len(inp)
19.     relationships = []
20.
21.     for i in range(size):
22.         for j in range(size):
23.             if (inp[i][j] == '1'):
24.                 if ((i,j) not in relationships) and ((j,i) not in relationships):
25.                     relationships.append((i,j))
26.                 if i != j:
27.                     if (i, i) in relationships:
28.                         relationships.remove((i, i))
29.                     if (j, j) in relationships:
30.                         relationships.remove((j, j))
31.
32. #     to_remove_elts = []
33. #     for (i, j) in relationships:
34. #         if i != j: # it is already part of a couple
35. #             to_remove_elts.append((i, i))
36. #             to_remove_elts.append((j, j))
37.
38. #     for item in to_remove_elts:
39. #         if item in relationships:
40. #             relationships.remove(item)
41.
42.     transitive_relationships = transitive_cloure(relationships)
43.     return len(transitive_relationships)
RAW Paste Data