開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 8(Case Study: Word Play)の Exercise 8-7、8-8、8-9.を取り組んでみる。
Exercise 8-7、8-8、8-9.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- say '7.'; my $filename = 'words.txt'; for $filename.IO.lines { say $_ if $_ ~~ /(.)$0(.)$1(.)$2/; } say '8.'; sub first_l(Str $word) { return substr $word, 0, 1; } sub last_l(Str $word) { return substr $word, *-1, 1; } sub middle_l(Str $word where $word.chars > 1) { return substr $word, 1, *-1; } sub is-palindrome(Str $word) { return True if $word.chars <= 1; return is-palindrome middle_l($word) if first_l($word) eq last_l($word); False; } for 100000..999999 { if (is-palindrome(~$_.substr(2, 4)) and is-palindrome(~($_ + 1).substr(1, 5)) and is-palindrome(~($_ + 2).substr(1, 4)) and is-palindrome(~($_ + 3))) { say $_; } } say '9,'; my $age1 = 20; my $age2 = 50; for $age1..$age2 -> $diff { my $count = 0; for 0..(100 - $diff) -> $i { my $mum = $i + $diff; if ~$i eq $mum.flip { say "ME: $i, MUM: $mum"; $count++ } } say "$count: $diff" if $count > 0; }
入出力結果(Terminal, REPL)
$ ./sample7.pl 7. bookkeeper bookkeepers bookkeeping bookkeepings 8. 198888 199999 9, ME: 14, MUM: 41 ME: 25, MUM: 52 ME: 36, MUM: 63 ME: 47, MUM: 74 ME: 58, MUM: 85 ME: 69, MUM: 96 6: 27 ME: 15, MUM: 51 ME: 26, MUM: 62 ME: 37, MUM: 73 ME: 48, MUM: 84 ME: 59, MUM: 95 5: 36 ME: 16, MUM: 61 ME: 27, MUM: 72 ME: 38, MUM: 83 ME: 49, MUM: 94 4: 45 $
0 コメント:
コメントを投稿