Guest User

Untitled

a guest
Dec 13th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. removeFrom[b_List, a_List] := Module[{f},
  2. f[_] = 0;
  3. (f[#] = -#2) & @@@ Tally[a];
  4. Pick[b, UnitStep[f[#]++ & /@ b], 1]
  5. ]
  6.  
  7. removeFrom[{a, b, c, a, d, a, e}, {a, c, a}]
  8.  
  9. {b, d, a, e}
  10.  
  11. removeFrom2[b_List, a_List] := Module[{f, g},
  12. (f[#] = -#2) & @@@ Tally[a];
  13. g[x_] /; f[x] < 0 := f[x]++;
  14. g[_] = True;
  15. Select[b, g]
  16. ]
  17.  
  18. short = RandomInteger[1*^5, 2*^4];
  19. long = RandomInteger[1*^5, 2*^5];
  20.  
  21. unsortedComplement[long, short] // Short // Timing
  22. removeFrom2[long, short] // Short // Timing
  23.  
  24. fastRF[a_List, b_List] :=
  25. Module[{c, o, x},
  26. c = Join[b, a];
  27. o = Ordering[c];
  28. x = 1 - 2 UnitStep[-1 - Length[b] + o];
  29. x = FoldList[Max[#, 0] + #2 &, x];
  30. x[[o]] = x;
  31. Pick[c, x, -1]
  32. ]
  33.  
  34. fastRF[{a, b, c, a, d, a, e} , {a, c, a}]
  35.  
  36. {b, d, a, e}
  37.  
  38. Needs["GeneralUtilities`"]
  39.  
  40. one = RandomInteger[1*^5, 1*^6];
  41.  
  42. BenchmarkPlot[
  43. {
  44. removeFrom2[one, #] &,
  45. unsortedComplement[one, #] &,
  46. uc[one, #] &,
  47. fastRF[one, #] &
  48. },
  49. RandomSample[one, #] &,
  50. 5^Range[8],
  51. TimeConstraint -> 30
  52. ]
  53.  
  54. com = {a, c, a, a, b, c, a, d, a, e}
  55. ord = Ordering[com]
  56. com[[ord]]
  57.  
  58. {a, c, a, a, b, c, a, d, a, e}
  59.  
  60. {1, 3, 4, 7, 9, 5, 2, 6, 8, 10}
  61.  
  62. {a, a, a, a, a, b, c, c, d, e}
  63.  
  64. 1 - 2 UnitStep[-1 - 3 + ord]
  65. x = FoldList[Max[#, 0] + #2 &, %]
  66.  
  67. {1, 1, -1, -1, -1, -1, 1, -1, -1, -1}
  68.  
  69. {1, 2, 1, 0, -1, -1, 1, 0, -1, -1}
  70.  
  71. x[[ord]] = x; x
  72.  
  73. Pick[com, x, -1]
  74.  
  75. {1, 1, 2, 1, -1, 0, 0, -1, -1, -1}
  76.  
  77. {b, d, a, e}
  78.  
  79. Clear[unsortedComplement];
  80. unsortedComplement[x_, y_] :=
  81. Module[{order, xsorted, distinct, freqs, posintervals, freqrules},
  82. xsorted = x[[order = Ordering[x]]];
  83. {distinct, freqs} = Transpose[Tally[xsorted]];
  84. freqrules = Dispatch[Append[Rule @@@ Tally[y], _ -> 0]];
  85. posintervals =
  86. Transpose[{
  87. Most[#] + Replace[distinct, freqrules, {1}],
  88. Rest[#] - 1
  89. }] &[Prepend[Accumulate[freqs], 0] + 1];
  90. x[[Sort@order[[Flatten[Range @@@ posintervals]]]]]]
  91.  
  92. unsortedComplement[{a,b,c,a,d,a,e},sub = {a,c,a}]
  93.  
  94. (* {b,d,a,e} *)
  95.  
  96. large1 = RandomInteger[1000,10^5];
  97. large2 = RandomInteger[1000,10^4];
  98.  
  99. (res1=unsortedComplement[large1,large2])//Short//Timing
  100.  
  101. (* {0.078,{951,956,345,459,345,951,956,<<89986>>,443,977,568,340,496,887,946}} *)
  102.  
  103. (res2=Fold[Delete[#1,Position[#1,#2,1,1]]&,large1,large2])//Short//Timing
  104.  
  105. (* {35.,{951,956,345,459,345,951,956,<<89986>>,443,977,568,340,496,887,946}} *)
  106.  
  107. res1==res2
  108.  
  109. (* True *)
  110.  
  111. uc[list_, eles_] := Module[{dt = Tally[eles], fm},
  112. fm = findMultiPosXX[list, dt[[All, 1]]];
  113. list[[Delete[Range@Length@list,
  114. Transpose[{Flatten[MapThread[Take, {fm, dt[[All, 2]]}]]}]]]]]
  115.  
  116. list1 = {a, b, c, a, d, a, e}; list2 = {a, c, a};
  117. Fold[Delete[#1, Position[#1, #2, 1, 1]] &, list1, list2]
  118. (* {b, d, a, e} *)
  119.  
  120. With[{patt = Table[Unique[], {Length[list2] + 1}]},
  121. ReplaceAll[list1, Riffle[Pattern[#, BlankNullSequence[]] & /@ patt, list2] :> patt]]
  122. (* {b, d, a, e} *)
  123.  
  124. {list1, list2} = {{a, c, a}, {a, b, c, a, d, a, e}};
  125. Fold[# /. #2 &, list2, {h___, #, t___} :> {h, t} & /@ list1]
  126. (* {b, d, a, e} *)
  127.  
  128. remover[long_List, short_List] := Module[{hmap, lookupresult},
  129. hmap = Language`HashMap@@Apply[Rule, Tally[short], {1}];
  130. Select[long, (lookupresult = Language`HashMapLookup[hmap, #];
  131. Or[lookupresult === $Failed, lookupresult === 0,
  132. (hmap = Language`HashMapAssociate[hmap, #, lookupresult - 1]; False)]) &]
  133. ]
  134.  
  135. simple[h_, p_] :=
  136. Join @@ (Table[#[[1]], {#[[2]]}] & /@
  137. Cases[Last@
  138. Reap[Sow[1, #] & /@ h; Sow[-1, #] & /@ p, _, {#1, Total@#2} &],
  139. Except[{_, 0}]])
  140.  
  141. {a, b, d, e}
  142.  
  143. comp[x_, y_] := Module[{},
  144. fun[q_] :=
  145. If[Length[q] == 1, q,
  146. Drop[#[[1]], Length[#[[2]]]] &@GatherBy[q, Sign]];
  147. ls[u_, v_] :=
  148. Cases[Last@
  149. Reap[Join[Table[Sow[j, u[[j]]], {j, Length[u]}],
  150. Table[Sow[-j, v[[j]]], {j, Length[v]}]], _, {#1, fun[#2]} &],
  151. Except[{_, {}}]];
  152. ord[u_] := Module[{pos, tab, or},
  153. pos = Flatten@u[[All, 2]];
  154. or = Thread[Sort[pos] -> Range[Length[pos]]];
  155. tab = Table[1, {Length[pos]}];
  156. ReplacePart[tab,
  157. Flatten@(Thread[#[[2]] -> #[[1]]] & /@ (u /. or))]];
  158. ord[ls[x, y]]]
  159.  
  160. {b, d, a, e}
  161.  
  162. v1 = {a, b, c, a, d, a, e};
  163. v2 = {a, c, a};
  164.  
  165. Flatten[Table @@@
  166. Normal[Fold[
  167. Function[{ass, ele}, <|
  168. KeyValueMap[
  169. If[#1 === ele, #1 -> If[#2 - 1 >= 0, #2 - 1, 0], #1 -> #2] &,
  170. ass]|>], Length /@ PositionIndex[v1], v2]]]
Add Comment
Please, Sign In to add comment