開発環境
- 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 7(Strings)、Debugging の Exercise: is-reverse Subroutine.を取り組んでみる。
Exercise: is-reverse Subroutine.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub is-reverse(Str $word1, Str $word2) { return False if $word1.chars != $word2.chars; my $i = 0; my $j = $word2.chars - 1; while $j >= 0 { say '$i = ', $i, ' $j = ', $j; return False if substr($word1, $i, 1) ne substr($word2, $j, 1); $i++; $j--; } return True; } say is-reverse('', ''); say not is-reverse('', 'a'); say not is-reverse('a', ''); say is-reverse('a', 'a'); say not is-reverse('ab', 'a'); say is-reverse('ab', 'ba'); say is-reverse('abcde', 'edcba'); say not is-reverse('日本語', 'abc'); say is-reverse('日本語', '語本日');
入出力結果(Terminal, REPL)
$ ./sample_rev.pl True True True $i = 0 $j = 0 True True $i = 0 $j = 1 $i = 1 $j = 0 True $i = 0 $j = 4 $i = 1 $j = 3 $i = 2 $j = 2 $i = 3 $j = 1 $i = 4 $j = 0 True $i = 0 $j = 2 True $i = 0 $j = 2 $i = 1 $j = 1 $i = 2 $j = 0 True $
0 コメント:
コメントを投稿