Advertisement
STANAANDREY

tranform graph elems

Jan 17th, 2023
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. from functools import reduce
  2.  
  3. g = {1:{2,3,4}, 2:{1,3}, 3:{1,4}, 4:{1,3}}
  4.  
  5. def transf(graph, f):
  6.     def reducer(acc, elem):
  7.         aux = set(map(f, elem))
  8.         elem.clear()
  9.         elem.update(aux)
  10.  
  11.     reduce(reducer, graph.values(), None)
  12.     return graph
  13.  
  14. print(transf(g, lambda x: x ** 2))
  15.  
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement