aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2022-07-20 23:23:22 +0700
committerMichael Manring <michael@manring>2022-07-20 23:23:22 +0700
commit610e35db7a2992927ef1b8a474ddb4870be7a5e0 (patch)
tree2789905c6c195da57ad957c74715eaca9516f809
parent440ffedbfa07347dbf5326a97bcec4655c718d78 (diff)
downloadperlweeklychallenge-club-610e35db7a2992927ef1b8a474ddb4870be7a5e0.tar.gz
perlweeklychallenge-club-610e35db7a2992927ef1b8a474ddb4870be7a5e0.tar.bz2
perlweeklychallenge-club-610e35db7a2992927ef1b8a474ddb4870be7a5e0.zip
pwc174 solution in perl - minor updates
-rw-r--r--challenge-174/pokgopun/perl/ch-1.pl16
-rw-r--r--challenge-174/pokgopun/perl/ch-2.pl12
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