Facebook
From kraken, 2 Weeks ago, written in Python.
Embed
Download Paste or View Raw
Hits: 121
  1. import sys
  2. from collections import defaultdict
  3. from functools import reduce
  4.  
  5. def load_txt(fname):
  6.     list2 = []
  7.     with open(fname,'r') as f:
  8.         list2 = [tuple(map(int,line.split(','))) for line in f]
  9.     return list2
  10.  
  11. def flatmap(lista, f):
  12.     return flatten(colection_map(lista,f))
  13.  
  14. def flatten(list_of_lists):
  15.     flattened_list = [item for sublist in list_of_lists for item in sublist]
  16.     return flattened_list
  17.  
  18. def colection_map(lista, f):
  19.     return list(map(f, lista))
  20.  
  21. def reduceByKey(kolekcja, f):
  22.     result_dict = {}
  23.     result = []
  24.     for key, value in kolekcja:
  25.         if key in result_dict:
  26.             result_dict[key]=f(result_dict[key], value)
  27.         else:
  28.             result_dict[key] = value
  29.     for key, value in result_dict.items():
  30.         result.append((key,value))
  31.     return result
  32.  
  33. def crossJoinByKey(m1,m2):
  34.     list1 = []
  35.     for k1 in m1:
  36.         for k2 in m2:
  37.             if k1[0] == k2[0]:
  38.                 list1.append((k1[0],(k1[1],k2[1])))
  39.     return list1
  40.  
  41. list1 = load_txt(sys.argv[1])
  42. #print(list1)
  43.  
  44. fA = lambda p: ((p[0],p[1]),(p[1],p[0]))
  45. fP1 = lambda p: (p[0],0)
  46. fR = lambda a,b: a+b
  47. fP2 = lambda p: p[0]
  48. fPe = lambda p: (p,p,True)
  49. fT = lambda t: (t[1][1],(t[0],t[1][0]))
  50. fS1 = lambda a,b: (a[0],min(a[1],b[1]))
  51. fS2 = lambda t: (t[0],min(t[1][0],t[1][1]))
  52. fS3 = lambda s: (s[0],(s[0],s[1]))
  53. fZ1 = lambda p: p[2]
  54. fZ2 = lambda p,q: p or q
  55.  
  56. fW1 = lambda w: (w[1][1],(w[0],(w[1][0],w[1][1])))
  57. fW2 = lambda w: (w[1])
  58. fW3 = lambda w: (w[0][0],w[1])
  59. fPe2 =lambda s: (s[0],min(s[1][0],s[1][1]),s[1][1] < s[1][0])
  60.  
  61. listA = flatmap(list1, fA)
  62. #print(listA)
  63. listP = colection_map(reduceByKey(colection_map(listA, fP1),fR),fP2)
  64. #print(listP)
  65. listPe = colection_map(listP,fPe)
  66. #print(listPe)
  67. listT = crossJoinByKey(listPe,listA)
  68. #print(listT)
  69. listS = colection_map(colection_map(reduceByKey(listT,fS1),fS2),fS3)
  70. #print("list S: ",listS)
  71. z = reduce(fZ2,colection_map(listPe,fZ1))
  72. #print(z)
  73.  
  74. while z:
  75.     #print("--------------------------------------------")
  76.     listW1 = colection_map(listS,fW1)
  77.     #print("list W1: ", listW1)
  78.     listW2 = colection_map(crossJoinByKey(listW1,listS),fW2)
  79.     #print("list W2: ",listW2)
  80.     listS = colection_map(listW2,fW3)
  81.     #print("list S: ",listS)
  82.     listPe = colection_map(listS, fPe2)
  83.     z = reduce(fZ2,colection_map(listPe,fZ1))
  84.     if z == False:
  85.         listZ = crossJoinByKey(listS,listA)
  86.         print(listZ)
  87.  
  88. #print("list S: ",listS)