同じ所を2度通らない道順の数 Total number of routes that do not pass by the same place twice
同じ所を2度通らない道順の数 Total number of routes that do not pass by the same place twice
JSDeferred による非同期処理のスライドを見て、「JSDeferred っておもしろそー」って思ってたんだけど、1ヶ月放置した挙句にやっと読み始めた。 JSDeferred の解説記事って検索しても出てこないので、ソース読まずにはちゃんとした使い方がわからない。 それどころか、ソース読んでも一見何やってるのかわからない。(おまけにコメント部分の英語がちょっとアレなのでというのもあるけど…) というわけで腰を落ち着けて読みながら理解した点をメモってたら、こんな分量になってしまった。 JSDeferredのソース読んでる - JavaScriptで遊ぶよ - g:javascript JSDeferredのソース読んでる・その2 - JavaScriptで遊ぶよ - g:javascript JSDeferredのソース読んでる・その3 - JavaScriptで遊ぶよ - g:jav
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 ごく普通に実装
配列をシャッフル、つまりランダムに要素の位置を入れ替えるというのを、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・正の値を返すことによって要素の順序を決定するので、その関数がランダムに値を返せばランダ
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く