開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の4章(リファレンス入門)の4.9(練習問題)2を解いてみる。
その他参考書籍
2.
コード(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';
sub check_required_items {
my $who = shift;
my $items = shift;
my @required = qw(preserver sunscreen water_bottle jacket);
my @missing = ();
for my $item (@required) {
unless (grep $item eq $_, @$items) {
print "$who is missing $item.\n";
push @missing, $item;
}
}
if (@missing) {
print "Adding @missing to @$items for $who.\n";
push @$items, @missing;
}
}
sub check_items_for_all {
my $all = shift;
for my $who (sort keys %$all) {
check_required_items($who, $all-&^gt;{$who});
}
}
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
my %all = (
Gilligan =&^gt; \@gilligan,
Skipper =&^gt; \@skipper,
Professor =&^gt; \@professor,
);
check_items_for_all(\%all);
入出力結果(Terminal)
$ ./sample.pl Gilligan is missing preserver. Gilligan is missing sunscreen. Gilligan is missing jacket. Adding preserver sunscreen jacket to red_shirt hat lucky_socks water_bottle for Gilligan. Professor is missing preserver. Professor is missing jacket. Adding preserver jacket to sunscreen water_bottle slide_rule batteries radio for Professor. Skipper is missing water_bottle. Adding water_bottle to blue_shirt hat jacket preserver sunscreen for Skipper. $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
def checkRequiredItems(who, items):
required = ['preserver', 'sunscreen', 'water_bottle', 'jacket']
missing = []
for item in required:
if not item in items:
print("{0} is missing {1}".format(who, item))
missing.append(item)
if missing:
print("Adding {0} to {1} for {2}".format(
" ".join(missing), " ".join(items), who))
for item in missing:
items.append(item)
def checkItemsForAll(all):
for who in sorted(all):
checkRequiredItems(who, all[who])
gilligan = ['red_shirt', 'hat', 'lucky_socks', 'water_bottle']
skipper = ['blue_shirt', 'hat', 'jacket', 'preserver', 'sunscreen']
professor = ['sunscreen', 'water_bottle', 'slide_rule', 'batteries', 'radio']
all = {
'Gilligan': gilligan,
'Skipper': skipper,
'Professor': professor
}
checkItemsForAll(all);
入出力結果(Terminal)
$ ./sample.py Gilligan is missing preserver Gilligan is missing sunscreen Gilligan is missing jacket Adding preserver sunscreen jacket to red_shirt hat lucky_socks water_bottle for Gilligan Professor is missing preserver Professor is missing jacket Adding preserver jacket to sunscreen water_bottle slide_rule batteries radio for Professor Skipper is missing water_bottle Adding water_bottle to blue_shirt hat jacket preserver sunscreen for Skipper $
0 コメント:
コメントを投稿