開発環境
- 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)の Exercises 7-3.を取り組んでみる。
Exercises 7-3.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub rotate-word($word, $n) { my $result = ''; my $m = $n % 26; for $word.comb -> $letter { my $k = $letter.ord + $m; $k -= 26 if 'Z'.ord < $k; $result = $result ~ $k.chr; } $result; } say rotate-word('IBM', -1); say rotate-word('Z', 1); my $word = 'PRRL'; say $word; $word = rotate-word($word, 13); say $word; $word = rotate-word($word, 13); say $word;
入出力結果(Terminal, REPL)
$ ./sample3.pl HAL A PRRL CEEY PRRL $
0 コメント:
コメントを投稿