2015年8月20日木曜日

開発環境

  • OS X Yosemite - Apple (OS)
  • Emacs (Text Editor)
  • JavaScript (プログラミング言語)
  • SpiderMonkey, Node.js(V8) (JavaScript engine)

Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 9(Sets)、Exercises 1.(No. 5683)を解いてみる。

Exercises 1.(No. 5683)

JavaScript(Emacs)

/*jslint         browser : true, continue : true,
  devel  : true, indent  : 4,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : false,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global print */
var Set,
    set;

Set = function () {
    this.data_store = [];
};
Set.prototype = {
    constructor : Set,
    add : function (data) {        
        if (this.data_store.indexOf(data) < 0) {
            this.data_store.push(data);
            this.data_store.sort();
            return true;
        }
        return false;
    },
    remove : function (data) {
        var pos = this.data_store.indexOf(data);
        if (pos > -1) {
            this.data_store.splice(pos, 1);
            return true;
        }
        return false;
    },
    size : function () {
        return this.data_store;
    },
    contains: function (data) {
        return this.data_store.indexOf(data) > -1 ? true : false;
    },
    union : function (set) {
        var temp_set = new Set(),
            i,
            max;

        for (i = 0, max = this.data_store.length; i < max; i += 1) {
            temp_set.add(this.data_store[i]);
        }
        for (i = 0, max = set.data_store.length; i < max; i += 1) {
            if (!temp_set.contains(set.data_store[i])) {
                temp_set.data_store.push(set.data_store[i]);
            }
        }
        return temp_set;
    },
    intersect: function (set) {
        var temp_set = new Set(),
            i,
            max;
        for (i = 0, max = this.data_store.length; i < max; i += 1) {
            if (set.contains(this.data_store[i])) {
                temp_set.add(this.data_store[i]);
            }
        }
        return temp_set;
    },
    subset: function (set) {
        if (this.size() > set.size()) {
            return false;
        }
        this.data_store.forEach(function (element) {
            if (!set.contains(element)) {
                return false;
            }
        });
        return true;
    },
    difference: function (set) {
        var temp_set = new Set(),
            i,
            max;
        
        for (i = 0, max = this.data_store.length; i < max; i += 1) {
            if (!set.contains(this.data_store[i])) {
                temp_set.add(this.data_store[i]);
            }
        }
        return temp_set;            
    },
    show: function () {
        return this.data_store;
    }
};

set = new Set();
set.add("Clayton");
set.add("Jennifer");
set.add("Danny");
set.add("Bryan");
set.add("Clayton");

print(set.show());

出力結果(Terminal, shell, SpiderMonkey)

$ jslint  sample1.js 

sample1.js is OK.
$ js sample1.js
Bryan,Clayton,Danny,Jennifer
$

0 コメント:

コメントを投稿