2017年4月22日土曜日

開発環境

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 4(Conditionals and Recursion)のExercises 4-1、2、3.を取り組んでみる。

Exercises 4-1、2、3.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

say '1-1.';
sub days-hours-minutes-seconds($seconds is copy) {
    my $days = $seconds div (24 * 60 * 60);
    $seconds %= (24 * 60 * 60);
    my $hours = $seconds div (60 * 60);
    $seconds %= (60 * 60);
    my $minutes = $seconds div 60;
    $seconds %= 60;
    say "days: $days, hours: $hours, minutes: $minutes, seconds: $seconds";
}

say '1-2.';
days-hours-minutes-seconds(240000);

say '1-3.';
my $seconds = (prompt "seconds: ").Int;
days-hours-minutes-seconds($seconds);

say '2-1.';
sub check-fermat($a, $b, $c, $n) {
    if (($a ** $n + $b ** $n) == $c ** $n) and $n > 2 {
        say 'Holy smokes, Fermat was wrong!';
    } else {
        say "No, that doesn't work.";
    }
}
say '2-2.';
sub check-fermat-prompt {
    my $a = get().Int;
    my $b = Int($*IN.get);
    my $c = (prompt '').Int;
    my $n = Int(get);
    check-fermat($a, $b, $c, $n);
}

check-fermat-prompt;

say '3-1.';
sub is-triangle($a, $b, $c) {
    if $a >= $b + $c or $b >= $c + $a or $c >= $a + $b {
        say 'No';
    } else {
        say 'Yes';
    }
}
say '3-2.';
sub is-triangle-prompt {
    my $a = get().Int;
    my $b = Int($*IN.get);
    my $c = (prompt '').Int;
    is-triangle($a, $b, $c);
}
is-triangle-prompt;
is-triangle-prompt;

入出力結果(Terminal, REPL)

$ cat sample1.txt
240000
2
3
4
5
1
2
2
1
2
3
$ ./sample1.pl < sample1.txt 
1-1.
1-2.
days: 2, hours: 18, minutes: 40, seconds: 0
1-3.
seconds: days: 2, hours: 18, minutes: 40, seconds: 0
2-1.
2-2.
No, that doesn't work.
3-1.
3-2.
Yes
No
$ 

0 コメント:

コメントを投稿