Skip to content
This repository was archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
第5回大阪 勉強会コーディングファイル
Browse files Browse the repository at this point in the history
  • Loading branch information
yabuuchi committed Jan 21, 2015
1 parent f09ae17 commit e33a9ea
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 0 deletions.
35 changes: 35 additions & 0 deletions sample-answer/calc.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

sub calc{
my ($first,$second) = @_;
if($first =~ m|^\d+$| && $second =~ m/^\d+$/){
my %result = (
add => $first + $second,
sub => $first - $second,
mol => $first * $second,
div => $first / $second,
mod => $first % $second,
);
return \%result;
}else{
return undef;
}
}

print Dumper calc(9,6);
print Dumper calc('a','b');

sub result_dump{
my $result = shift;
for my $key (keys %$result){
print $key."=>".$result->{$key}.",\n";
}
return;
}

# result_dump(calc(9,6));
39 changes: 39 additions & 0 deletions sample-answer/calc_string.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

#####################
##calc_string練習問題
###1.引数として与えられた文字列が、数値A 演算子 数値Bという文字列であれば、その式を計算して結果を返す関数calc_stringを書いてみましょう
###2.関数calc_stringとwhile文を使って、Ctrlキーとdキーを押すまでの間、標準入力から文字列を受け取り、文字列に書かれた式を計算するようなコードを書いてみましょう

sub calc_string{
my $str = shift;
return "undifined_if!" if !($str);
return "undifined_unless" unless ($str);
return "undifined_defined" if !defined($str);
my @strings = split(/ /,$str);
return 'first not num!!' unless $strings[0] =~ m/^[-]?\d+$/;
return 'not set Operator' unless $strings[1] =~ m/(\+|\-|\*|\/|%)/;
return 'second not num!!' unless $strings[2] =~ /^[-]?\d+$/;
my $result = '';
$result = $strings[0] + $strings[2] if $strings[1] eq '+';
$result = $strings[0] - $strings[2] if $strings[1] eq '-';
$result = $strings[0] * $strings[2] if $strings[1] eq '*';
$result = $strings[0] / $strings[2] if $strings[1] eq '/';
$result = $strings[0] % $strings[2] if $strings[1] eq '%';
return $result;
}

while(my $input = <STDIN>) {
chomp $input;
my $result = calc_string($input);
print "$result\n";
}
# print calc_string('')."\n";
# print calc_string('1 +1')."\n";
# print calc_string('1 + 1')."\n";
# print calc_string('3 - 1')."\n";
27 changes: 27 additions & 0 deletions sample-answer/food.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

########################
##Perl入学式 #4 復習問題
###3.$data に人物名と好きな食べ物を (スペース):(スペース) 区切りで与えています.
###この変数から食べ物が何回出現したかカウントして表示させてください.

my $data = q{
papix : sushi
moznion : soba
boolfool : sushi
macopy : sushi
};

my @lines = split "\n", $data;
my %foods_count;
for my $line (@lines){
if($line =~ m/(\w+)\s:\s(\w+)/){
$foods_count{$2}++;
}
}
print Dumper \%foods_count;
29 changes: 29 additions & 0 deletions sample-answer/lib/PerlBeginners.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# #!/usr/bin/env perl

use strict;
use warnings;

package PerlBeginners;
sub perllevel{
my $level = shift;
unless($level =~ m/^[1-9]$|10/){
return "no set level!!"
}
my % my_level = (
1 => "Perl 関係の書籍や資料を何も読んでいない。Perl がプログラミング言語だということは知っているが、それ以外のことは何も知らない。他人の書いたPerl プログラムを実行できるので、プログラムの一部を編集することでプログラムの動作の一部(出力される文字列の内容など)を変更できることを知っている。プログラムのほかの部分に変更を加えてもなぜうまくいかないのか理解していない。この言語に合うメンタルモデルを持っていないので、Perl の構文をCOBOL とC++ のような他の言語のものとは区別できていない。",
2 => "基本ブロック構造の構文を理解しているが、JavaScriptのような言語に似ているという程度の認識にとどまっている。ブロックがある種のスコープ効果を生じさせるという理解はあるものの、レキシカル変数のことは知らないし、use strict や usewarningsに出会ったこともない。条件の意味を変更することができ、基本的な算術演算子と論理演算子条件を使用できる。必要とするすべてのことを、他人の書いたプログラムに若干の変更を加えることで達成できると思っている。",
3 => "プログラムをゼロから作成したいが、ある種の教育が必要だとわかっている。Perl を学ぶのに適した書籍を推薦して欲しいと思っている。このレベルのプログラマの中には、ラクダ本[WALL00] をチュートリアルだと思って手に入れ、その内容を読破しようとして、重いトラウマに苦しむ者がいるかもしれない。",
#
# .
# .
# .
# .
# .
# .
# .
#
10 => "Perl のobfuscation コンテストや「golf」コンテスト*3 に参加する。たとえば、普通のプログラマなら1 つのプログラムを丸ごと必要とするような関数を、埋め込みコードを使って単一の正規表現で実装してしまう。Perl コアにパッチを送ったり、新しいメジャーなモジュールを寄贈したりすることで、Perl コミュニティで有名人になるかもしれない"
);
return $my_level{$level};
}
1;
16 changes: 16 additions & 0 deletions sample-answer/lib/PerlEntrance.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

package PerlEntrance{
sub tokyo{
return "Tokyo!";
}
sub osaka{
return "Osaka!";
}
}
1;
19 changes: 19 additions & 0 deletions sample-answer/lib/Yapc.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

package Yapc;
sub year{
return "2015";
}
sub month{
return '08'
}
sub day{
return '20','21','22';
}

1;
24 changes: 24 additions & 0 deletions sample-answer/map_example.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

###################
##mapを使用した例題

my @array = (1..10);
my @array2 = ();
for my $val(@array){
push @array2,$val*2;
}

my @array3 = map{$_ * 2}@array;
say Dumper @array3;

my @strs = ('aa','bb c','c c');
say @strs;

my @forma_str = map{$_ =~ s/\s//g;$_}@strs;
print "@forma_str"."\n";
24 changes: 24 additions & 0 deletions sample-answer/map_grep.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;
use feature ':5.10';

my @files = qw/foo.pl bar.pm baz.rb qux.py/;

sub map_back{
my @files = @_;
my @format = ();
@format = map{$_ =~ s/$/.bak/g;$_;}@files;
return @format;
}
say Dumper map_back(@files);

sub grep_pl_and_pm{
my @files = @_;
my @get = ();
@get = grep{$_ =~ /pm|pl/}@files;
return @get;
}
say Dumper grep_pl_and_pm(@files);
9 changes: 9 additions & 0 deletions sample-answer/packege.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env perl

use strict;
use warnings;
use lib "lib";
use PerlEntrance;

print PerlEntrance::tokyo();
print PerlEntrance::osaka();
16 changes: 16 additions & 0 deletions sample-answer/perlbeginer.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env perl

use strict;
use warnings;
use lib "lib";
use PerlBeginners;

##################
##練習問題
###PerlBeginnersというモジュールを作って、その中にperllevelというサブルーチンを作りましょう
###perllevelは1から10の整数の引数を取ります
###引数をレベルと解釈して、上記のように使います
###Perlレベルは以下のブログに掲載されています。何らかの形でモジュール内に保持しましょう

print PerlBeginners::perllevel(10);
print PerlBeginners::perllevel('a');

0 comments on commit e33a9ea

Please sign in to comment.