aboutsummaryrefslogtreecommitdiff
path: root/challenge-009
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-05-26 17:34:56 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-05-26 17:34:56 +0100
commit0ec19e0806858fdd89988d755742ee3f6282c517 (patch)
treed3e18bf9378f16123af13f3611c3bd92d58791b3 /challenge-009
parent824b4eafb6e3b5a654fbd452a403c028b72c1062 (diff)
downloadperlweeklychallenge-club-0ec19e0806858fdd89988d755742ee3f6282c517.tar.gz
perlweeklychallenge-club-0ec19e0806858fdd89988d755742ee3f6282c517.tar.bz2
perlweeklychallenge-club-0ec19e0806858fdd89988d755742ee3f6282c517.zip
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-009')
-rw-r--r--challenge-009/guillermo-ramos/perl5/ch-1.pl17
-rw-r--r--challenge-009/guillermo-ramos/perl5/ch-2.pl48
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-009/guillermo-ramos/perl5/ch-1.pl b/challenge-009/guillermo-ramos/perl5/ch-1.pl
new file mode 100644
index 0000000000..e98223d10b
--- /dev/null
+++ b/challenge-009/guillermo-ramos/perl5/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+# Compute distinct digits of the given number
+sub distinct {
+ my $n = shift;
+ my %uniq; # Poor man's set
+ $uniq{$_} = 1 foreach split(//, $n);
+ return keys %uniq;
+}
+
+my $n = 0;
+$n++ while distinct($n**2) != 5;
+
+print "$n ($n**2 = ", $n**2, ")\n";
diff --git a/challenge-009/guillermo-ramos/perl5/ch-2.pl b/challenge-009/guillermo-ramos/perl5/ch-2.pl
new file mode 100644
index 0000000000..336dda0a6c
--- /dev/null
+++ b/challenge-009/guillermo-ramos/perl5/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+sub rank {
+ my $rank_type = shift;
+ my @points = sort @_;
+
+ my %ranks;
+ my $rank = $rank_type eq "modified" ? 0 : 1;
+
+ my $interval_begin = 0;
+ my $i;
+ for ($i = 0; $i < @points; $i++) {
+ my $point = $points[$i];
+ if ($point != $points[$interval_begin]) {
+ my @points_to_add = @points[$interval_begin .. ($i-1)];
+ $rank += @points_to_add if $rank_type eq "modified";
+ $ranks{$rank} = \@points_to_add;
+ if ($rank_type eq "standard") {
+ $rank += @points_to_add;
+ } elsif ($rank_type eq "dense") {
+ $rank = $rank + 1;
+ }
+ $interval_begin = $i;
+ }
+ }
+ my @points_to_add = @points[$interval_begin .. ($i-1)];
+ $rank += @points_to_add if $rank_type eq "modified";
+ $ranks{$rank} = \@points_to_add;
+ return \%ranks;
+}
+
+# Get rank type and points as CLI arguments
+my $rank_type = shift || "";
+grep /^$rank_type$/, qw(modified standard dense)
+ or die "Usage: $0 {modified, standard, dense} rank1 rank2 ...";
+my @points = @ARGV;
+
+# Compute rankings
+my $ranks = rank($rank_type, @points);
+
+# Display rankings
+for my $rank (sort (keys %$ranks)) {
+ my @positions = @{$ranks->{$rank}};
+ print "$rank. @positions\n";
+}