sreejith2904

Final

May 1st, 2021
691
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.  
  27.     to_remove_elts = []
  28.     for (i, j) in relationships:
  29.         if i != j: # it is already part of a couple
  30.             to_remove_elts.append((i, i))
  31.             to_remove_elts.append((j, j))
  32.    
  33.     for item in to_remove_elts:
  34.         if item in relationships:
  35.             relationships.remove(item)
  36.            
  37.     transitive_relationships = transitive_cloure(relationships)
  38.     print(transitive_relationships)
  39.     return len(transitive_relationships)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×