diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-05-25 04:15:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-25 04:15:35 +0100 |
| commit | 307c96372d8ec40fd0949a0b4417a2d0b1ac23f3 (patch) | |
| tree | 101874096674dfe5745c1afd8001c191d0833882 /challenge-009 | |
| parent | 962c46544d0d6f98733732f02a08cfe06eacf55a (diff) | |
| parent | 8c7dc14ab7442c5329fb62403875fd370eabfa13 (diff) | |
| download | perlweeklychallenge-club-307c96372d8ec40fd0949a0b4417a2d0b1ac23f3.tar.gz perlweeklychallenge-club-307c96372d8ec40fd0949a0b4417a2d0b1ac23f3.tar.bz2 perlweeklychallenge-club-307c96372d8ec40fd0949a0b4417a2d0b1ac23f3.zip | |
Merge pull request #173 from andrezgz/challenge-009
challenge-009 andrezgz solution
Diffstat (limited to 'challenge-009')
| -rw-r--r-- | challenge-009/andrezgz/ch2-data.csv | 33 | ||||
| -rw-r--r-- | challenge-009/andrezgz/perl5/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-009/andrezgz/perl5/ch-2.pl | 68 |
3 files changed, 118 insertions, 0 deletions
diff --git a/challenge-009/andrezgz/ch2-data.csv b/challenge-009/andrezgz/ch2-data.csv new file mode 100644 index 0000000000..ae2cc0cf71 --- /dev/null +++ b/challenge-009/andrezgz/ch2-data.csv @@ -0,0 +1,33 @@ +02:03:59;Haile Gebrselassie +02:05:27;Jaouad Gharib +02:05:42;Deressa Chimsa +02:07:05;William Kiplagat +02:07:05;Carlos Lopes +02:07:30;Patrick Tambwé +02:07:32;Sammy Korir +02:07:36;Kenneth Mungara +02:07:36;Peter Kamais +02:07:44;Henrick Ramaala +02:07:47;Moses Tanui +02:07:57;Abel Antón +02:07:59;Hicham Chatt +02:08:01;Francis Bowen Kipkoech +02:08:04;Lee Bong ju +02:08:06;Paul Tergat +02:08:14;Mbarak Hussein +02:08:14;Martin Fiz +02:08:21;Kamiel Maase +02:08:25;Paul Kipsambu +02:08:32;Viktor Röthlin +02:08:36;Jackson Kipngok +02:08:39;Daniel Too +02:08:42;Peter Kiplagat Chebet +02:08:55;Paul Evans +02:08:55;Luís Jesús +02:08:59;Alberto Juzdado +02:09:03;Abdelkader El Mouaziz +02:09:05;Abdellah Behar +02:09:08;Meb Keflezighi +02:09:10;Rachid Kisri +02:09:10;Charles Kibiwott +02:09:11;Joaquim Pinheiro diff --git a/challenge-009/andrezgz/perl5/ch-1.pl b/challenge-009/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..652432c053 --- /dev/null +++ b/challenge-009/andrezgz/perl5/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-009/ +# Challenge #1 +# 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; + +my $n = 100; #any less won't give a 5 digit number when squared +my @digits; +do { + @digits = (); + map { $digits[$_] = 1 } split //, ++$n**2; +} until ( 5 == grep $_, @digits); +print $n**2; #12769 diff --git a/challenge-009/andrezgz/perl5/ch-2.pl b/challenge-009/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..d9e367a106 --- /dev/null +++ b/challenge-009/andrezgz/perl5/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-009/ +# Challenge #2 +# 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. +# For more information, please refer to wiki page. +# https://en.wikipedia.org/wiki/Ranking + +use strict; +use warnings; + +my %valid_rankings = ( + 'standard' => 'Items that compare equal receive the same ranking number, + and then a gap is left in the ranking numbers.', + 'modified' => 'It is done by leaving the gaps in the ranking numbers + before the sets of equal-ranking items', + 'dense' => 'Items that compare equally receive the same ranking number, + and the next item(s) receive the immediately following ranking number.' +); + +my $ranking = $ARGV[0]; +usage() unless ($ranking && exists $valid_rankings{$ranking}); + + +my %data = load_data(); +my @sorted_data = sort keys %data; + +print ucfirst $ranking . ' ranking for marathon times'.$/; +my $n = $ranking eq 'modified' ? 0 : 1; + +foreach my $pos (@sorted_data) { + # mark positions with same ranking + my $mark = @{$data{$pos}} > 1 ? '*' : ' '; + + # not the best way, but it shows the differences between the algorithms + $n += @{$data{$pos}} if ($ranking eq 'modified'); + do { print sprintf("%3d%s => %s: %s", $n, $mark, $pos, $_).$/ } for @{$data{$pos}}; + $n += @{$data{$pos}} if ($ranking eq 'standard'); + $n++ if ($ranking eq 'dense'); +} + +sub usage { + print "Usage: $0 <ranking>".$/.$/; + print "Valid <ranking> values:".$/; + print " '$_': $valid_rankings{$_}".$/ foreach (reverse sort keys %valid_rankings); + print $/."For more information, please refer to https://en.wikipedia.org/wiki/Ranking".$/; + exit 1; +} + +sub load_data { + open(my $fh, "<", '../ch2-data.csv') or die "Could not open ch2-data.csv': $!"; + my %data; + while( my $line = <$fh> ) { + chomp $line; + my ($time,$name) = split /;/, $line; + push @{$data{$time}}, $name; + } + close $fh; + return %data; +} |
