From bc069894f3a3cad374447d1d15fb296b67030757 Mon Sep 17 00:00:00 2001 From: "Gustavo L. de M. Chaves" Date: Tue, 21 May 2019 15:17:00 -0300 Subject: Gustavo Chaves Perl 5 solutions to Challenge 009 --- challenge-009/gustavo-chaves/perl5/README.pod | 45 +++++++++++++++++++++++++++ challenge-009/gustavo-chaves/perl5/ch-1.pl | 15 +++++++++ challenge-009/gustavo-chaves/perl5/ch-2.pl | 41 ++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 challenge-009/gustavo-chaves/perl5/README.pod create mode 100755 challenge-009/gustavo-chaves/perl5/ch-1.pl create mode 100755 challenge-009/gustavo-chaves/perl5/ch-2.pl diff --git a/challenge-009/gustavo-chaves/perl5/README.pod b/challenge-009/gustavo-chaves/perl5/README.pod new file mode 100644 index 0000000000..cba7c00185 --- /dev/null +++ b/challenge-009/gustavo-chaves/perl5/README.pod @@ -0,0 +1,45 @@ +=pod + +=encoding utf8 + +=head1 #1 First square with five distinct digits + +=over 4 + +Write a script that finds the first square number that has at least 5 distinct +digits. This was proposed by Laurent Rosenfeld. + +=back + +This is easily solvable by splitting the square digits and tucking them in a +hash to drop duplicates. Then we only have to see if there are five keys in the +hash. + +=head1 #2 Ranking + +=over 4 + +Write a script to perform different types of ranking as described below: + +=over 4 + +=item 1. Standard Ranking (1224): Items that compare equal receive the same ranking number, and then a gap is left in the ranking numbers. +=item 2. Modified Ranking (1334): It is done by leaving the gaps in the ranking numbers before the sets of equal-ranking items. +=item 3. Dense Ranking (1223): Items that compare equally receive the same ranking number, and the next item(s) receive the immediately following ranking number. + +=back + +For more information, please refer to L. + +=back + +This problem isn't very well defined because one doesn't know which type of +items should being ranked. Numbers, strings, generic objects? + +I'm assuming that we're ranking numbers which are passed as command line +arguments to the script. Like this: + + $ ./ch-2.pl 8 4 2 4 4 3 3 4 5 7 8 6 5 3 2 1 3 + Standard Ranking: 1 2 2 4 4 4 4 8 8 8 8 12 12 14 15 16 16 + Modified Ranking: 1 3 3 7 7 7 7 11 11 11 11 13 13 14 15 17 17 + Dense Ranking: 1 2 2 3 3 3 3 4 4 4 4 5 5 6 7 8 8 diff --git a/challenge-009/gustavo-chaves/perl5/ch-1.pl b/challenge-009/gustavo-chaves/perl5/ch-1.pl new file mode 100755 index 0000000000..9decc20082 --- /dev/null +++ b/challenge-009/gustavo-chaves/perl5/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use autodie; +use warnings; + +my ($i, $square, %digits); + +for ($i = 0; 5 != scalar keys %digits; ++$i) { + $square = $i * $i; + %digits = map {$_ => undef} split //, $square; +} + +say "$i^2 == $square"; diff --git a/challenge-009/gustavo-chaves/perl5/ch-2.pl b/challenge-009/gustavo-chaves/perl5/ch-2.pl new file mode 100755 index 0000000000..f03817963a --- /dev/null +++ b/challenge-009/gustavo-chaves/perl5/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use 5.026; +use strict; +use autodie; +use warnings; + +# @ARGV should contain a list of numbers which I'll rank by magnitude. + +# First I'll use a hash to count how many times each numbers occurs in @ARGV. + +my %item_count; +++$item_count{$_} foreach @ARGV; + +my @sorted_keys = sort {$a <=> $b} keys %item_count; + +my $i; + +print "Standard Ranking:"; +$i = 1; +foreach my $key (@sorted_keys) { + print " $i" x $item_count{$key}; + $i += $item_count{$key}; +} +print "\n"; + +print "Modified Ranking:"; +$i = 0; +foreach my $key (@sorted_keys) { + $i += $item_count{$key}; + print " $i" x $item_count{$key}; +} +print "\n"; + +print " Dense Ranking:"; +$i = 1; +foreach my $key (@sorted_keys) { + print " $i" x $item_count{$key}; + $i += 1; +} +print "\n"; -- cgit