Rationalise (Canonise) the Fractions before Sorting
Before fractions are sorted, they are rationalised;
i.e., they are put in a form where their denominators become
the same. This same denominator is the LCM of the
denominators of all the separate fractions.
After this is done, the new numerators can easily be sorted
in a preferred order.
Steps for Sorting of Fractions Perl code
The following steps will guide us in writing our code.
Let's illustrate with 5/9,
3/7, 1/2
Step 1:
Find the LCM of the denominators.
⇒ LCM of 9, 7 & 2 = 126
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((5 x 14), (3 x 18), (1 x 63))/126
= (70, 54, 63)/126
Step 3:
Go ahead and sort the numerators in our order of choice.
⇒ In ascending order:
54/126, 63/126,
70/126
=
3/7, 1/2,
5/9
Create a new module file;
call it SortFraction.pm.
Type out the adjoining Perl code for sorting fractions in ascending and descending orders.
Note: You can comment out the SubtractFraction Perl object
code in the main class from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of numerators and denominators for fractional sorting.
Perl code for SortFraction Module File
package SORTFRACTION;
BEGIN {
require Exporter;
our $VERSION = 2016.12;
our @EXPORT_OK = qw(sortAscending sortDescending);
}
use warnings;
use strict;
use parent "ADDFRACTION";
my $answer;
my (@numerators, @denominators, @new_numerators);
my (@final_numerators, @final_denominators);
my %fractions;
sub new {
no warnings "all";
my $this = shift;
my $parameters = shift;
bless $parameters, $this;
$this->_init($parameters);
return $this;
}
sub _init {
my $self = shift;
my $aux = shift;
$fractions{numerators} = $aux->{numerators};
$fractions{denominators} = $aux->{denominators};
@numerators = @{$fractions{numerators}};
@denominators = @{$fractions{denominators}};
$answer = 0;
}
sub sortAscending {
my $call = ADDFRACTION->new(\%fractions);
my @help = $call->canoniseFraction();
@new_numerators = @{$help[0]};
my @copy_numerators = @new_numerators;
@copy_numerators = sort {$a <=> $b} @copy_numerators;
for my $sorted (@copy_numerators) {
my ($index) = grep { $new_numerators[$_] eq $sorted } 0 .. $
push @final_numerators, $numerators[$index];
push @final_denominators, $denominators[$index];
}
return (\@final_numerators, \@final_denominators);
}
sub sortDescending {
my $call = ADDFRACTION->new(\%fractions);
my @help = $call->canoniseFraction();
@new_numerators = @{$help[0]};
my @copy_numerators = @new_numerators;
@copy_numerators = sort {$b <=> $a} @copy_numerators;
for my $sorted (@copy_numerators) {
my ($index) = grep { $new_numerators[$_] eq $sorted } 0 .. $
push @final_numerators, $numerators[$index];
push @final_denominators, $denominators[$index];
}
return (\@final_numerators, \@final_denominators);
}
1;
main class
use strict;
use warnings;
use SORTFRACTION;
my (@numerators, @denominators, @solutions);
my %fractions;
@numerators = (1, 3, 5, 9);
@denominators = (2, 4, 2, 10);
%fractions = (
numerators => \@numerators,
denominators => \@denominators
);
print "\n Sorting in ascending order the fractions:\n";
printf("%35s", " ");
printf("%9u", $_) for @numerators;
printf("\n%43s", " ");
print "- , " for 1 .. $
printf("%2s", "-");
printf("\n%35s", " ");
printf("%9d", $_) for @denominators;
print "\n";
my $sort_fract = SORTFRACTION->new(\%fractions);
@solutions = $sort_fract->sortAscending();
@numerators = @{$solutions[0]};
@denominators = @{$solutions[1]};
printf("\n%35s", " ");
printf("%9u", $_) for @numerators;
printf("\n%43s", "Answer = ");
print "- , " for 1 .. $
printf("%2s", "-");
printf("\n%35s", " ");
printf("%9u", $_) for @denominators;
print "\n\n";