두 개 이상의 데이터 세트에 공통, 배타적 또는 고유한 값을 알아야 할 때 교차점, 차이점 및 합집합을 만들어야 할 필요성이 발생합니다. 이번 달에는 세 가지 함수를 만듭니다.
- 교차점 ( aList , bList ) – aList 및 bList 모두에 포함된 값 목록을 반환합니다.
- 차이 ( aList , bList ) – 두 개의 목록을 반환합니다. 첫 번째는 bList 에 없는 aList 의 값을 포함하고, 두 번째는 aList 에 없는 bList 의 값을 포함합니다.
- Union ( aList , bList ) – aList 및 bList 의 모든 값 목록을 반환하고 중복 항목을 제거합니다.
예를 들어, 다음과 같은 경우:
aList = {“a”,”b”,”c”,”d”,”e”,”e”}
bList = {"a","a","c","f","g"}
그 다음에,
교차점(aList,bList) => {“a”,”c”}
차이(aList,bList) => {{“b”,”d”,”e”},{“f”,”g”}}
Union(aList,bList) => {"a","b","c","d","e","f","g"}
추가 크레딧을 얻으려면 교차점 과 합집합을 확장하여 세 개 이상의 목록을 가져옵니다.
연관 배열과 재귀를 사용합니다. 명확하게 명명된 지원 변수를 사용하고, 재귀 및 연관 배열을 사용하고, 주석도 모두 누락했기 때문에 코드를 이해하기가 가장 쉽지 않을 수 있습니다.
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})));적어도 연관 배열을 사용할 때 중복된 값을 제거할 때 약간 느려질 수 있습니다. 적어도 요약을 사용하여 데이터 테이블에서 고유한 값을 가져올 때 어느 시점에서는 더 빨라질 수 있습니다.