aboutsummaryrefslogtreecommitdiff
path: root/challenge-009
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-05-26 20:40:13 +0100
committerGitHub <noreply@github.com>2019-05-26 20:40:13 +0100
commitceee1c8f5b5255c65df04bf91bb9b3995d5e5d7c (patch)
treee7850e1eb2bf89b4710c0052359ac5cc311f22fa /challenge-009
parent4e6e67ca81e2578ced75c99be63234df10749227 (diff)
parent3791d1c527c7209fcdabf5fc88afd34084f67bb3 (diff)
downloadperlweeklychallenge-club-ceee1c8f5b5255c65df04bf91bb9b3995d5e5d7c.tar.gz
perlweeklychallenge-club-ceee1c8f5b5255c65df04bf91bb9b3995d5e5d7c.tar.bz2
perlweeklychallenge-club-ceee1c8f5b5255c65df04bf91bb9b3995d5e5d7c.zip
Merge pull request #182 from dmanto/branch-for-challenge-009
my answers for challenge009
Diffstat (limited to 'challenge-009')
-rw-r--r--challenge-009/daniel-mantovani/perl5/ch-1.pl24
-rw-r--r--challenge-009/daniel-mantovani/perl5/ch-2.pl99
2 files changed, 123 insertions, 0 deletions
diff --git a/challenge-009/daniel-mantovani/perl5/ch-1.pl b/challenge-009/daniel-mantovani/perl5/ch-1.pl
new file mode 100644
index 0000000000..c5888123dd
--- /dev/null
+++ b/challenge-009/daniel-mantovani/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+# Write a script that finds the first square number that has at least 5 distinct digits.
+# This was proposed by Laurent Rosenfeld.
+use strict;
+use warnings;
+use v5.10;
+
+# we start by defining a sub that checks how many different digits a number has:
+
+my $target = 5;
+
+sub different_digits {
+
+ # this will build a hash in wich each present digit is a key
+ my %diff = map { $_ => undef } split '', shift;
+
+ # now we just return the amount of keys
+ return scalar keys %diff;
+}
+
+my $num = 1;
+
+$num++ while different_digits( $num * $num ) < $target;
+
+say "First square with $target distinct digits is: ", $num * $num;
diff --git a/challenge-009/daniel-mantovani/perl5/ch-2.pl b/challenge-009/daniel-mantovani/perl5/ch-2.pl
new file mode 100644
index 0000000000..5cc1c5bc52
--- /dev/null
+++ b/challenge-009/daniel-mantovani/perl5/ch-2.pl
@@ -0,0 +1,99 @@
+# Write a script to perform different types of ranking as described below:
+
+# 1. Standard Ranking (1224): Items that compare equal receive the same ranking number, and then a gap is left in the ranking numbers.
+# 2. Modified Ranking (1334): It is done by leaving the gaps in the ranking numbers before the sets of equal-ranking items.
+# 3. Dense Ranking (1223): Items that compare equally receive the same ranking number, and the next item(s) receive the immediately following ranking number.
+
+use strict;
+use warnings;
+use v5.10;
+
+# proposed solution is a function "rank" that will take as arguments:
+# 1st argument is an "order" value, with values -1 for descending order, or 1 for ascending order
+# 2nd argument is a "mode" argumente, with values 1, 2 or 3 according to the type or ranking proposed
+# in the challenge.
+# Starting from 3rd argument on, is an array of hashes. Each hash has a "score" key,
+# that should be numeric (integer or float), and is used to sort the array.
+# The hashes can have other keys, not affected by the function
+# Example input:
+#
+# my @result = rank(
+# -1, 2,
+# { score => 8, name => 'foo' },
+# { score => 7, name => 'bar' },
+# { score => 8, name => 'baz' },
+# { score => 10, name => 'best' }
+# );
+#
+# result will be an array of the same hashes, with each one with an additional key "rank",
+# ordered according to the first two arguments
+#
+
+sub rank {
+ my ( $order, $mode, @inp ) = @_;
+
+ # first we order @inp by "score" key, taking into account the "order" argument
+ @inp = sort { $a->{score} * $order <=> $b->{score} * $order } @inp;
+
+ # now we add the key "rank", according to the $mode argument
+ if ( $mode == 1 ) {
+
+# standard ranking, we just copy index of the array (+1) for scores different from former score
+#
+ $inp[0]{rank} = 1;
+ for my $i ( 1 .. $#inp ) {
+ if ( $inp[$i]{score} == $inp[ $i - 1 ]{score} ) {
+ $inp[$i]{rank} = $inp[ $i - 1 ]{rank};
+ }
+ else { $inp[$i]{rank} = $i + 1 }
+ }
+ }
+ elsif ( $mode == 2 ) {
+
+ # modified ranking, the opossite of the standard for equally ranked entries
+ #
+ $inp[-1]{rank} = @inp;
+ for my $i ( reverse 0 .. $#inp - 1 ) {
+ if ( $inp[$i]{score} == $inp[ $i + 1 ]{score} ) {
+ $inp[$i]{rank} = $inp[ $i + 1 ]{rank};
+ }
+ else { $inp[$i]{rank} = $i + 1 }
+ }
+ }
+ elsif ( $mode == 3 ) {
+
+ # dense ranking, similar to standard
+ #
+ $inp[0]{rank} = 1;
+ for my $i ( 1 .. $#inp ) {
+ if ( $inp[$i]{score} == $inp[ $i - 1 ]{score} ) {
+ $inp[$i]{rank} = $inp[ $i - 1 ]{rank};
+ }
+ else { $inp[$i]{rank} = $inp[ $i - 1 ]{rank} + 1 }
+ }
+ }
+ return @inp;
+}
+
+sub print_ranking {
+ say "Rank\tScore\tName";
+ say sprintf( "%s\t%s\t%s", $_->{rank} // 'N/A', $_->{score}, $_->{name} )
+ for @_;
+ say "";
+}
+
+# now prints 3 examples
+
+my @input = (
+ { score => 8, name => 'foo' },
+ { score => 7, name => 'bar' },
+ { score => 8, name => 'baz' },
+ { score => 10, name => 'best' }
+);
+
+say "First example, standard ranking";
+print_ranking(rank(-1,1, @input));
+say "Second example, modified ranking";
+print_ranking(rank(-1,2, @input));
+say "Second example, dense ranking";
+print_ranking(rank(-1,3, @input));