2012-01-01から1年間の記事一覧

if文の中でlastを使う

if (1) { print "hoge\n"; last; print "foo\n"; print "bar\n"; } # Can't "last" outside a loop block at 〜 括弧を二重にしてやると動く。知らなかった。 if (1) {{ print "hoge\n"; last; print "foo\n"; print "bar\n"; }} perlsynを参照

難読化されたJavaScriptコードを展開するスクリプトを書いた

難読化されたJavaScriptコードをコマンドラインで展開してくれるツールを 探したけどパッと見つからなかったのでOnline JavaScript beautifierで使われている js_beautify.jsというのをPerlに翻訳してみた。https://github.com/hironorism/p5-js-beautify 使…

1台サーバでFastCGIアプリをダウンタイム無く更新する方法について

なんか難しく考えていたけど実に単純な方法で出来るような気がしたのでメモ。 アプリ(fcgi_app.psgi) #!/usr/bin/env plackup -s FCGI use Plack::Handler::FCGI; my $app = sub { return [ 200, [ 'Content-Type' => 'text/html' ], [ 'BEFORE' ] ]; }; my …

何度目かのmergesortのお勉強

今度は同マージソートPerl my @b; sub merge_sort { my ($a, $low, $high) = @_; $low //= 0; $high //= @$a -1; my ($i, $j, $k); return if $low >= $high; my $mid = int( ($low + $high) / 2 ); merge_sort($a, $low, $mid); merge_sort($a, $mid+1, $hi…

SRM 148 DIV2 250

与えられた数字の各桁の数字の中で与えられたその数字自体を割り切れるものをカウントして返す #include <string> #include <vector> #include <sstream> using namespace std; class DivisorDigits { public: int howMany(int number) { int count = 0; string s; stringstream ss; s</sstream></vector></string>…

何度目かのquicksortのお勉強

定本 Cプログラマのためのアルゴリズムとデータ構造 (SOFTBANK BOOKS)作者: 近藤嘉雪出版社/メーカー: ソフトバンククリエイティブ発売日: 1998/03メディア: 単行本購入: 11人 クリック: 169回この商品を含むブログ (77件) を見るtopcoderの問題を解く時に「…

SRM 147 DIV2 250

A-Zで構成され、アルファベット順でN文字横にずらした文字列(ex.N=2:A=>C,B=>D)が与えられるので元の文字列を求めよという問題 #include <string> #include <vector> #include <sstream> using namespace std; class CCipher { public: string decode(string cipherText, int shift) {</sstream></vector></string>…

SRM 144 DIV2 500

Pは0または1で構成される文字列でQ[i] = P[i-1] + P[i] + P[i+1] となるような文字列Qが与えられるのでPを求めろという問題多分相当いけてないけれどこれを書くのに7〜8時間かかった。 自分への戒めも含めてここに記録しておく。 #include <string> #include <vector> #inclu</vector></string>…

SRM146 DIV2 250

1〜6の数字が5つ入った配列から同じ数字の場合は合算し、最大の数字を取り出す #include <string> #include <vector> #include <map> using namespace std; class YahtzeeScore { public: int maxPoints(vector <int> toss) { map<int, int> score; int i, max; max = 0; for (i=0; i<5; i++) { s</int,></int></map></vector></string>…

SRM145 DIV2 250

文字列ditheredに含まれる文字が文字列の配列screenにいくつあるかを数える問題 (英文ではよく意味が分からずぐぐった)普通にぐるぐる回しただけ #include <string> #include <vector> using namespace std; class ImageDithering { public: int count(string dithered, vect</vector></string>…