La necessità di creare intersezioni, differenze e unioni emerge quando abbiamo bisogno di conoscere valori comuni, esclusivi o unici per due o più set di dati. Per questo mese, crea tre funzioni:
- intersezione ( aList , bList ) – Restituisce un elenco di valori contenuti sia in aList che in bList .
- differenza ( aLista , bLista ) – Restituisce due elenchi. Il primo contiene i valori in aList che non sono in bList , il secondo contiene i valori in bList che non sono in aList .
- union ( aList , bList ) – Restituisce un elenco di tutti i valori in aList e bList , rimuovendo eventuali duplicati.
Ad esempio, se:
aLista = {“a”,”b”,”c”,”d”,”e”,”e”}
bLista = {“a”,”a”,”c”,”f”,”g”}
Poi,
intersezione(aLista,bLista) => {“a”,”c”}
differenza(aLista,bLista) => {{“b”,”d”,”e”},{“f”,”g”}}
union(aList,bList) => {“a”,”b”,”c”,”d”,”e”,”f”,”g”}
Per ottenere crediti extra, estendi intersezione e unione per prendere un elenco di tre o più elenchi.
Utilizzo di array associativi e ricorsione. Il codice potrebbe non essere più semplice da capire perché non lo sono: utilizzo di variabili di supporto con nomi chiari, utilizzo di ricorsione e array associativi, mancando anche tutti i commenti.
Names Default To Here(1);
intersection = function({temp_list}, {Default Local},
Try(
tempAa = Associative Array(temp_list[1]);
tempAa << Intersect(AssociativeArray(temp_list[2])),
return("not enough lists provided");
);
If(N Items(temp_list) == 2,
return(tempAa << get keys);
);
If(N Items(temp_list) > 2,
recurse(Insert(EvalList({tempAa << get keys}), Remove(temp_list, 1,2)))
);
);
difference = function({firstList, secondList}, {Default Local},
tempAaFirst = Associative Array(firstList);
tempAaSecond = Associative Array(secondList);
tempAaFirst << Remove(Associative Array(secondList));
tempAaSecond << Remove(Associative Array(firstList));
return(Eval List({tempAaFirst << get keys, tempAaSecond << get keys}));
);
union = function({temp_list}, {Default Local},
Try(
Insert Into(temp_list[1], temp_list[2]),
return("not enough lists provided");
);
If(N Items(temp_list) == 2,
return(Associative Array(temp_list[1]) << get keys);
);
If(N Items(temp_list) > 2,
recurse(Insert(EvalList({Associative Array(temp_list[1]) << get keys}), Remove(temp_list, 1,2)))
);
);
//test cases
Clear log();
aList = {"a","b","c","d","e","e"};
bList = {"a","a","c","f","g"};
cList = {"a", "b", "c", "h"};
dList = {"i", "j", "k"};
Print("intersection()");
Show(intersection(Eval List({aList})));
Show(intersection(Eval List({aList, bList})));
Show(intersection(Eval List({aList, bList, cList})));
Show(intersection(Eval List({aList, bList, cList, dList})));
Print("\!Ndifference()");
Show(difference(aList, bList));
Print("\!Nunion()");
Show(union(Eval List({aList})));
Show(union(Eval List({aList, bList})));
Show(union(Eval List({aList, bList, cList})));
Show(union(Eval List({aList, bList, cList, dList})));Potrebbe essere un po' lento almeno nel rimuovere i valori duplicati quando si utilizzano array associativi, almeno quando il recupero di valori univoci dai datatable utilizzando Summarize diventa più veloce ad un certo punto.