diff options
| -rw-r--r-- | challenge-174/pokgopun/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-174/pokgopun/perl/ch-2.pl | 12 |
2 files changed, 17 insertions, 11 deletions
diff --git a/challenge-174/pokgopun/perl/ch-1.pl b/challenge-174/pokgopun/perl/ch-1.pl index 8dd9852d2e..f522f4f9f5 100644 --- a/challenge-174/pokgopun/perl/ch-1.pl +++ b/challenge-174/pokgopun/perl/ch-1.pl @@ -15,13 +15,15 @@ sub isDisarium{ } ### take 1st argument as a number and return an array of digits made from it sub digit{ - my $n = shift; - { - unshift @_, $n % 10; - $n = int($n/10); - redo if $n; - } - return @_; + ### use regex is faster + return split //, shift; +# my $n = shift; +# { +# unshift @_, $n % 10; +# $n = int($n/10); +# redo if $n; +# } +# return @_; } ### takes all arguments as numbers and return summation of them sub sum{ diff --git a/challenge-174/pokgopun/perl/ch-2.pl b/challenge-174/pokgopun/perl/ch-2.pl index c5313ff99c..e76094674d 100644 --- a/challenge-174/pokgopun/perl/ch-2.pl +++ b/challenge-174/pokgopun/perl/ch-2.pl @@ -23,8 +23,10 @@ sub rank_permutation(){ $q = int($r / $fact); # by decomposing r = q * fact + rest $r %= $fact; push @p, $digits[$q]; - $digits[$q] = undef; # remove this digit p[i]; - @digits = grep{defined} @digits; +# $digits[$q] = undef; # remove this digit p[i]; +# @digits = grep{defined} @digits; + # use splice instead + splice @digits, $q, 1; $fact /= $n - 1 - $i if $i != $n - 1; # weight of next digit } return @p; @@ -44,8 +46,10 @@ sub permutation_rank{ redo; } $r += $fact * $q; - $digits[$q] = undef; - @digits = grep{defined} @digits; # remove this digit p[i] +# $digits[$q] = undef; +# @digits = grep{defined} @digits; # remove this digit p[i] + # use splice instead + splice @digits, $q, 1; $fact /= $n - 1 - $i; # weight of next digit } return $r |
