2010-08-01から1ヶ月間の記事一覧

itertools.combinationsを無限長イテレータに対応させる

pythonのitertools.combinationsやitertools.permutationには無限長のイテレータを渡す事はできません。内部でイテレータをタプルに変換しようとするのでフリーズしてしまいます。無限長にも対応する、アルゴリズム自体は難しくはありません。そこで、python…

jQueryでにリセットボタンをつける

<input type="reset">という手もありますが、フォーム全体がリセットされてしまうので、各<input type="file">毎にクリアボタンがあると便利でしょう。 参考:Clear upload file input field with jQuery //jQueryが必要です。 function add_clear_button(button){ $(button).wrap('<span></span>'); $(button).aft…

itertoolsをclojureで(combinations)

(defn combinations [s r] (for [indices (permutations (range (count s)) r) :when (= (sort indices) indices)] (vec (for [i indices] (nth s i))))) 前回作ったpermutaionsに依存しています。 clojure.contrib.combinatorics/combinationsがありますが…

itertoolsをclojureで(permutations)

pythonのitertools.permutationsをclojureで書いてみました。 clojure.contrib.combinatorics/permutationsが既にありますが、それはそれとして。 (ns itertools (:use clojure.contrib.seq-utils [clojure.contrib.combinatorics :only (cartesian-product)…

なぞなぞです。プログラマなら解けるはず

mixiの「自学自習自力 文系プログラマー」コミュにあった問題です。 ●◆■▲★×4=★▲■◆● 同じ数字がひとつ入って、それぞれ同じ記号は同じ数字が入る。 ●、◆、■、▲、★に適切な数字を求めよ。 まず、Pythonで。 from itertools import product, imap for nums in …

1本のクリスマスツリー

$>python -c "for i in range(10)+[2]*3:print ' '*(9-i), '#'*(i*2-1)" # ### ##### ####### ######### ########### ############# ############### ################# ### ### ###perl 6のチュートリアルにあったのでなんとなく

モテたい一心でbrainf*ckをPythonに実行させる

LLTigerで #encoding:ruby と書くだけで、Python上でRubyが動く、Ruby on Pythonが発表されました。そして、よく分かりませんが、encodingのハックがゆくゆくはモテることにつながるらしいので、何か作ってみよう!でも、Rubyはかなり大変そう。一番簡単なの…