Created
December 2, 2016 06:12
-
-
Save akanehara/20f9627e98e234f3bd3b45e3464ab62c to your computer and use it in GitHub Desktop.
長さと分割数から範囲のリストを生成(Perl)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| # 長さと分割数から範囲のリストを生成します | |
| # | |
| # 例: 長さ100を5区間に | |
| # $ range.pl 100 5 | |
| # 1 0 19 │ | |
| # 2 20 39 │ | |
| # 3 40 59 │ | |
| # 4 60 79 │ | |
| # 5 80 99 | |
| use strict; | |
| use warnings; | |
| use POSIX qw(floor ceil); | |
| use File::Basename qw(basename); | |
| # STDOUT,STDERRのバッファリング抑制 | |
| { $| = 1; my $old = select(STDERR); $| = 1; select($old); } | |
| my $SCRIPT = basename $0; | |
| sub usage { | |
| print STDOUT "Usage: $SCRIPT LENGTH DIVISION\n"; | |
| } | |
| unless (2 <= scalar @ARGV) { | |
| usage; | |
| exit 1; | |
| } | |
| my ($LENGTH, $DIVISION) = @ARGV; | |
| unless ($LENGTH =~ /^\d+$/ && $DIVISION =~ /^\d+$/) { | |
| usage; | |
| exit 1; | |
| } | |
| my $WIDTH = ceil($LENGTH / $DIVISION); | |
| my $END = $LENGTH - 1; | |
| my $cur = 0; | |
| my $i = 1; | |
| do { | |
| my ($a, $b) = ($cur, $cur + $WIDTH -1); | |
| $b = $END if ($END < $b); | |
| print STDOUT "$i\t$a\t$b\n"; | |
| $cur = $b + 1; | |
| $i++; | |
| } while ($cur <= $END); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment