2013年9月17日火曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の9章(リファレンスを使った実践的なテクニック)の9.9(練習問題)3を解いてみる。

その他参考書籍

3.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
use Benchmark q(timethese);

my @files = glob "/opt/local/bin/*";

my $sort = q{ my @sorted = sort { -s $a <=> -s $b } @files;};

my $schwartz = q{
    my @sorted = map {
        $_->[0];
    } sort {
        $a->[1] <=> $b->[1];
    } map {
        [$_, -s $_];
    } @files;
};

my $count = 10 ** 6;

timethese($count, {
    sort => $sort,
    schwartz => $schwartz
});

入出力結果(Terminal)

$ ./sample.pl test.txt
ソート前
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Fred
FRED
fred
frederick
Alfred
fred flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and fred
fred and wilma
Wilma and Fred      
Fred and Wilma
wilma&fred
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
通常ソート
A
Alfred
Bamm-Bamm    
FRED
Fred
Fred and Wilma
I saw Wilma yesterday
I, Wilma!
KMI
Mississippi
Mr. Slate     
Mrs. Wilma Flintstone   
Wilma and Fred      
Z
a
barney
fred
fred and wilma
fred flintstone
frederick
http://sitekamimura.blogspot.com
http://www.mkamimura.com   
kamimura's blog
llama
wilama
wilma and fred
wilma&fred
辞書順ソート後
A
a
Alfred
Bamm-Bamm    
barney
Fred
FRED
fred
fred and wilma
Fred and Wilma
frederick
fred flintstone
http://sitekamimura.blogspot.com
http://www.mkamimura.com   
I saw Wilma yesterday
I, Wilma!
kamimura's blog
KMI
llama
Mississippi
Mr. Slate     
Mrs. Wilma Flintstone   
wilama
wilma and fred
Wilma and Fred      
wilma&fred
Z
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import sys
import re

strs = []

with open(sys.argv[1]) as f:
    strs = f.readlines()
    strs = list(map(lambda x: x.rstrip(), strs))

sorted_str = sorted(strs)

def dict_sort(x):
    x = x.lower()
    x = re.sub(r'[^a-z]', '', x)
    return x

sorted_dict = sorted(strs, key=dict_sort)

print('ソート前')
print('\n'.join(strs))

print('通常ソート後')
print('\n'.join(sorted_str))

print('辞書順ソート後')
print('\n'.join(sorted_dict))

入出力結果(Terminal)

$ ./sample.py test.txt
ソート前
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com
Fred
FRED
fred
frederick
Alfred
fred flintstone
Mr. Slate
Mississippi
Bamm-Bamm
wilama
barney
wilma and fred
fred and wilma
Wilma and Fred
Fred and Wilma
wilma&fred
Mrs. Wilma Flintstone
I saw Wilma yesterday
I, Wilma!
Z
llama
通常ソート後
A
Alfred
Bamm-Bamm
FRED
Fred
Fred and Wilma
I saw Wilma yesterday
I, Wilma!
KMI
Mississippi
Mr. Slate
Mrs. Wilma Flintstone
Wilma and Fred
Z
a
barney
fred
fred and wilma
fred flintstone
frederick
http://sitekamimura.blogspot.com
http://www.mkamimura.com
kamimura's blog
llama
wilama
wilma and fred
wilma&fred
辞書順ソート後
A
a
Alfred
Bamm-Bamm
barney
Fred
FRED
fred
fred and wilma
Fred and Wilma
frederick
fred flintstone
http://sitekamimura.blogspot.com
http://www.mkamimura.com
I saw Wilma yesterday
I, Wilma!
kamimura's blog
KMI
llama
Mississippi
Mr. Slate
Mrs. Wilma Flintstone
wilama
wilma and fred
Wilma and Fred
wilma&fred
Z
$

0 コメント:

コメントを投稿