計算機科学のブログ

イテラブル Set ヘルパー関数、和集合(union)、共通部分(intersection)、差集合(subtract)、対称差(difference)

入門JavaScriptプログラミング (JD Isaacks(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(イテラブル)、LESSON 24(Set)、24.5(練習問題)、Q24-1の解答を求めてみる。

コード

function union(set1, set2) {
    let set = new Set(set1);

    for (const elem of set2) {
        set.add(elem);
    }
    return set;
}
function intersection(set1, set2) {
    let set = new Set();

    for (const elem of set1) {
        if (set2.has(elem)) {
            set.add(elem);
        }
    }
    return set;
}
function subtract(set1, set2) {
    let set = new Set(set1);

    for (const elem of set2) {
        set.delete(elem);
    }
    return set;
}
function difference(set1, set2) {
    let set = new Set();

    for (const elem of set2) {
        if (!set1.has(elem)) {
            set.add(elem);
        }
    }
    for (const elem of set1) {
        if (!set2.has(elem)) {
            set.add(elem);
        }
    }
    return set;
}
function p(set) {
    let result = '{';

    for (const elem of set) {
        result += `${elem}, `;
    }
    result += '}';

    return result;
}
let empty = new Set(),
    one = new Set([1]),
    two = new Set([1, 2]),
    evens = new Set([2, 4, 6, 8, 10]),
    odds = new Set([1, 3, 5, 7, 9]),
    sets = [empty, one, two, evens, odds],
    funcs = [union, intersection, subtract, difference],
    funcNames = ['union', 'intersection', 'subtract', 'difference'];

for (const set1 of sets) {
    let s1 = p(set1);
    for (const set2 of sets) {
        let s2 = p(set2);

        for (let index = 0; index < funcs.length; index++) {
            let s = p(funcs[index](set1, set2));
            console.log(`${funcNames[index]}(${s1}, ${s2}): ${s}`);
        }
    }
}

入出力結果(Terminal、Zsh)

% node sample.js
union({}, {}): {}
intersection({}, {}): {}
subtract({}, {}): {}
difference({}, {}): {}
union({}, {1, }): {1, }
intersection({}, {1, }): {}
subtract({}, {1, }): {}
difference({}, {1, }): {1, }
union({}, {1, 2, }): {1, 2, }
intersection({}, {1, 2, }): {}
subtract({}, {1, 2, }): {}
difference({}, {1, 2, }): {1, 2, }
union({}, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, }
intersection({}, {2, 4, 6, 8, 10, }): {}
subtract({}, {2, 4, 6, 8, 10, }): {}
difference({}, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, }
union({}, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, }
intersection({}, {1, 3, 5, 7, 9, }): {}
subtract({}, {1, 3, 5, 7, 9, }): {}
difference({}, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, }
union({1, }, {}): {1, }
intersection({1, }, {}): {}
subtract({1, }, {}): {1, }
difference({1, }, {}): {1, }
union({1, }, {1, }): {1, }
intersection({1, }, {1, }): {1, }
subtract({1, }, {1, }): {}
difference({1, }, {1, }): {}
union({1, }, {1, 2, }): {1, 2, }
intersection({1, }, {1, 2, }): {1, }
subtract({1, }, {1, 2, }): {}
difference({1, }, {1, 2, }): {2, }
union({1, }, {2, 4, 6, 8, 10, }): {1, 2, 4, 6, 8, 10, }
intersection({1, }, {2, 4, 6, 8, 10, }): {}
subtract({1, }, {2, 4, 6, 8, 10, }): {1, }
difference({1, }, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, 1, }
union({1, }, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, }
intersection({1, }, {1, 3, 5, 7, 9, }): {1, }
subtract({1, }, {1, 3, 5, 7, 9, }): {}
difference({1, }, {1, 3, 5, 7, 9, }): {3, 5, 7, 9, }
union({1, 2, }, {}): {1, 2, }
intersection({1, 2, }, {}): {}
subtract({1, 2, }, {}): {1, 2, }
difference({1, 2, }, {}): {1, 2, }
union({1, 2, }, {1, }): {1, 2, }
intersection({1, 2, }, {1, }): {1, }
subtract({1, 2, }, {1, }): {2, }
difference({1, 2, }, {1, }): {2, }
union({1, 2, }, {1, 2, }): {1, 2, }
intersection({1, 2, }, {1, 2, }): {1, 2, }
subtract({1, 2, }, {1, 2, }): {}
difference({1, 2, }, {1, 2, }): {}
union({1, 2, }, {2, 4, 6, 8, 10, }): {1, 2, 4, 6, 8, 10, }
intersection({1, 2, }, {2, 4, 6, 8, 10, }): {2, }
subtract({1, 2, }, {2, 4, 6, 8, 10, }): {1, }
difference({1, 2, }, {2, 4, 6, 8, 10, }): {4, 6, 8, 10, 1, }
union({1, 2, }, {1, 3, 5, 7, 9, }): {1, 2, 3, 5, 7, 9, }
intersection({1, 2, }, {1, 3, 5, 7, 9, }): {1, }
subtract({1, 2, }, {1, 3, 5, 7, 9, }): {2, }
difference({1, 2, }, {1, 3, 5, 7, 9, }): {3, 5, 7, 9, 2, }
union({2, 4, 6, 8, 10, }, {}): {2, 4, 6, 8, 10, }
intersection({2, 4, 6, 8, 10, }, {}): {}
subtract({2, 4, 6, 8, 10, }, {}): {2, 4, 6, 8, 10, }
difference({2, 4, 6, 8, 10, }, {}): {2, 4, 6, 8, 10, }
union({2, 4, 6, 8, 10, }, {1, }): {2, 4, 6, 8, 10, 1, }
intersection({2, 4, 6, 8, 10, }, {1, }): {}
subtract({2, 4, 6, 8, 10, }, {1, }): {2, 4, 6, 8, 10, }
difference({2, 4, 6, 8, 10, }, {1, }): {1, 2, 4, 6, 8, 10, }
union({2, 4, 6, 8, 10, }, {1, 2, }): {2, 4, 6, 8, 10, 1, }
intersection({2, 4, 6, 8, 10, }, {1, 2, }): {2, }
subtract({2, 4, 6, 8, 10, }, {1, 2, }): {4, 6, 8, 10, }
difference({2, 4, 6, 8, 10, }, {1, 2, }): {1, 4, 6, 8, 10, }
union({2, 4, 6, 8, 10, }, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, }
intersection({2, 4, 6, 8, 10, }, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, }
subtract({2, 4, 6, 8, 10, }, {2, 4, 6, 8, 10, }): {}
difference({2, 4, 6, 8, 10, }, {2, 4, 6, 8, 10, }): {}
union({2, 4, 6, 8, 10, }, {1, 3, 5, 7, 9, }): {2, 4, 6, 8, 10, 1, 3, 5, 7, 9, }
intersection({2, 4, 6, 8, 10, }, {1, 3, 5, 7, 9, }): {}
subtract({2, 4, 6, 8, 10, }, {1, 3, 5, 7, 9, }): {2, 4, 6, 8, 10, }
difference({2, 4, 6, 8, 10, }, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, }
union({1, 3, 5, 7, 9, }, {}): {1, 3, 5, 7, 9, }
intersection({1, 3, 5, 7, 9, }, {}): {}
subtract({1, 3, 5, 7, 9, }, {}): {1, 3, 5, 7, 9, }
difference({1, 3, 5, 7, 9, }, {}): {1, 3, 5, 7, 9, }
union({1, 3, 5, 7, 9, }, {1, }): {1, 3, 5, 7, 9, }
intersection({1, 3, 5, 7, 9, }, {1, }): {1, }
subtract({1, 3, 5, 7, 9, }, {1, }): {3, 5, 7, 9, }
difference({1, 3, 5, 7, 9, }, {1, }): {3, 5, 7, 9, }
union({1, 3, 5, 7, 9, }, {1, 2, }): {1, 3, 5, 7, 9, 2, }
intersection({1, 3, 5, 7, 9, }, {1, 2, }): {1, }
subtract({1, 3, 5, 7, 9, }, {1, 2, }): {3, 5, 7, 9, }
difference({1, 3, 5, 7, 9, }, {1, 2, }): {2, 3, 5, 7, 9, }
union({1, 3, 5, 7, 9, }, {2, 4, 6, 8, 10, }): {1, 3, 5, 7, 9, 2, 4, 6, 8, 10, }
intersection({1, 3, 5, 7, 9, }, {2, 4, 6, 8, 10, }): {}
subtract({1, 3, 5, 7, 9, }, {2, 4, 6, 8, 10, }): {1, 3, 5, 7, 9, }
difference({1, 3, 5, 7, 9, }, {2, 4, 6, 8, 10, }): {2, 4, 6, 8, 10, 1, 3, 5, 7, 9, }
union({1, 3, 5, 7, 9, }, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, }
intersection({1, 3, 5, 7, 9, }, {1, 3, 5, 7, 9, }): {1, 3, 5, 7, 9, }
subtract({1, 3, 5, 7, 9, }, {1, 3, 5, 7, 9, }): {}
difference({1, 3, 5, 7, 9, }, {1, 3, 5, 7, 9, }): {}
%