Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions challenge-009/andrezgz/ch2-data.csv
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions challenge-009/andrezgz/perl5/ch-1.pl
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions challenge-009/andrezgz/perl5/ch-2.pl
Original file line number Diff line number Diff line change
@@ -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;
}