Script para formar parejas sin repetición de pares para pairing
#!/usr/bin/perl -s
# Perl Scrip to Pairing in Pair programming
# Example:
# La primera vez llamar con argumentos:
# perl pairing.pl "Dario" "Gaby" "Claudio" "Frula" "Ricardo" "Raul" "Esteban" "Adrian"
# A partir de la segunda vez llamar sin argumentos porque los nombres fueron guardados en un archivo.
# perl pairing.pl
use strict;
use warnings;
# Begin main
# @ARGV is a array of command line arguments
if (scalar @ARGV == 0){
# File input. Read from file
my @input_argv = @{read_array_from_file('file_example.txt')};
do_pairing_no_repeated(\@input_argv);
}elsif (scalar @ARGV > 3){
#ARG input. Read from arguments of command line
do_pairing_repeated(\@ARGV);
}else{
print "Arguments must be more than 3! \n";
}
# End main
# Run pairing with repeated
sub do_pairing_repeated{
my ($ARGV) = @_; #array reference
my @pairing_list=();
do_pairing($ARGV, \@pairing_list);
print_pairing(\@pairing_list);
save('file_example.txt', \@pairing_list);
}
# Run pairing NO repeated
sub do_pairing_no_repeated{
my ($ARGV) = @_; #array reference
my $no_repeated=1;
my @pairing_list;
do{
@pairing_list=();
do_pairing($ARGV, \@pairing_list);
$no_repeated = compare_if_no_repeated($ARGV, \@pairing_list);
} until($no_repeated);
print_pairing(\@pairing_list);
save('file_example.txt', \@pairing_list);
}
# Checks if couples are repeated in the two arrays
sub compare_if_no_repeated{
my ($origin_list, $pairing_list) = @_; #array reference
for(my $i=0; $i<=scalar @{$origin_list} - 1; $i = $i + 2){
if ($i < scalar @{$origin_list} - 1) {
if (are_repeated(@{$origin_list}[$i], @{$origin_list}[$i + 1], @{$pairing_list}[$i], @{$pairing_list}[$i + 1])){
return 0;
}
}
}
return 1;
}
# Checks if the two pairs are the same (repeated pairs)
sub are_repeated{
my ($pair_1_a, $pair_1_b, $pair_2_a, $pair_2_b) = @_;
if (($pair_1_a eq $pair_2_a) && ($pair_1_b eq $pair_2_b)){
return 1
}
if (($pair_1_a eq $pair_2_b) && ($pair_1_b eq $pair_2_a)){
return 1
}
return 0;
}
# Print pairing names from a members array
sub print_pairing{
my ($members) = @_; #array reference
for(my $index=0; $index<=scalar @{$members} - 1; $index = $index + 2){
print @{$members}[$index] . "-";
if ($index < scalar @{$members} - 1) {
print @{$members}[$index + 1] . "\n";
}else{
print "none \n";
}
}
}
# Run pairing
sub do_pairing{
my ($members, $pairing_list) = @_; #array reference
my @members_copy;
foreach my $elem (@{$members}) {
push (@members_copy, $elem);
}
my $members_count = scalar @members_copy;
do{
my $selected = get_member_and_remove_it_random(\@members_copy);
$members_count = $members_count - 1;
push (@{$pairing_list}, $selected);
} until($members_count==0);
}
# Retrieves a member and removes it from the array.
sub get_member_and_remove_it_random{
my ($members) = @_; #array reference
my $size = scalar @{$members};
my $random_number = int(rand($size));
my $removed_member = splice @{$members}, $random_number, 1;
return $removed_member;
}
sub read_array_from_file{
my ($filename) = @_;
my @input_array = ();
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
while (my $row = <$fh>) {
chomp $row;
push (@input_array, $row);
}
} else {
warn "Could not open file '$filename' $!";
}
return \@input_array;
}
sub save{
my ($filename, $array_reference) = @_;
open(my $fh, '>:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
foreach my $elem (@{$array_reference}) {
print $fh "$elem\n";
}
close $fh;
}
No hay comentarios:
Publicar un comentario