2017年3月12日日曜日

開発環境

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-1、2、3.を取り組んでみる。

Exercises 4-1-1、2、3.

コード(Emacs)

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

say '4-1, 1.';

sub days-hours-mins-secs($seconds is copy) {
    my $days = $seconds div (24 * 60 * 60);
    $seconds = $seconds % (24 * 60 * 60);
    my $hours = $seconds div (60 * 60);
    $seconds = $seconds % (60 * 60);
    my $minutes = $seconds div 60;
    $seconds = $seconds % 60;

    say "$days days, $hours hours, $minutes minutes, $seconds seconds.";
}

days-hours-mins-secs(0);
days-hours-mins-secs(60);
days-hours-mins-secs(60 * 60);
days-hours-mins-secs(24 * 60 * 60);
days-hours-mins-secs(1000000);

say '4-1, 2.';
days-hours-mins-secs(240_000);

say '4-1, 3.';

my $seconds = Int(prompt "Enter a seconds: ");
days-hours-mins-secs($seconds);

入出力結果(Terminal, REPL)

$ ./sample1.pl
4-1, 1.
0 days, 0 hours, 0 minutes, 0 seconds.
0 days, 0 hours, 1 minutes, 0 seconds.
0 days, 1 hours, 0 minutes, 0 seconds.
1 days, 0 hours, 0 minutes, 0 seconds.
11 days, 13 hours, 46 minutes, 40 seconds.
4-1, 2.
2 days, 18 hours, 40 minutes, 0 seconds.
4-1, 3.
Enter a seconds: 240000
2 days, 18 hours, 40 minutes, 0 seconds.
$

0 コメント:

コメントを投稿

Comments on Google+: