タグ

関連タグで絞り込む (1)

タグの絞り込みを解除

アルゴリズムとJavascriptに関するpaselaのブックマーク (3)

  • John Resig - Javascript Diff Algorithm

    Using an idea grabbed from a mailing list post, I implemented the diff algorithm discussed in the following paper (free registration required): P. Heckel, A technique for isolating differences between files Comm. ACM, 21, (4), 264–268 (1978). The implementation, itself, has two functions, one of which is recommended for use: diffString( String oldFile, String newFile ) This method takes two string

  • 最速インターフェース研究会 :: 実践JavaScriptで配列をシャッフルする方法リファクタリング

    JavaScriptで配列をシャッフルする話を見て、そういえばArray#shuffleは以前書いた記憶があるなーと思って調べてみたらコピペだった。 http://www.fumiononaka.com/TechNotes/Flash/FN0212002.html Fisher-Yatesというアルゴリズムだそうです。 Array.prototype.shuffle = function() { var i = this.length; while(i){ var j = Math.floor(Math.random()*i); var t = this[--i]; this[i] = this[j]; this[j] = t; } return this; } a = [1,2,3,4,5]; a.shuffle() // 3,1,5,2,4 a // 3,1,5,2,4 ごく普通に実装

  • JavaScriptで配列をシャッフル

    配列をシャッフル、つまりランダムに要素の位置を入れ替えるというのを、sortメソッドを使ってやってみたのだけど、明らかにダメダメなものになってしまった。その後、あーでもないこーでもないと考えたのだけど、算数が得意すぎて頭が痛くなった。ということを某所でぼやいたらはてのくんがコードを見つけてくれた。どうやらFisher-Yatesという有名なアルゴリズムでやると良いらしい。 最初に書いたコードは、 var a = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); a.sort( function (a, b) { return Math.ceil(Math.random() * 3) - 2; } ); というもの。sortメソッドは、パラメータに与えられた関数が負の値・0・正の値を返すことによって要素の順序を決定するので、その関数がランダムに値を返せばランダ

    JavaScriptで配列をシャッフル
  • 1