/
ch-2.pl
158 lines (120 loc) · 4.01 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!perl
################################################################################
=comment
Perl Weekly Challenge 241
=========================
TASK #2
-------
*Prime Order*
Submitted by: Mohammad S Anwar
You are given an array of unique positive integers greater than 2.
Write a script to sort them in ascending order of the count of their prime
factors, tie-breaking by ascending value.
Example 1
Input: @int = (11, 8, 27, 4)
Output: (11, 4, 8, 27)
Prime factors of 11 => 11
Prime factors of 4 => 2, 2
Prime factors of 8 => 2, 2, 2
Prime factors of 27 => 3, 3, 3
=cut
################################################################################
#--------------------------------------#
# Copyright © 2023 PerlMonk Athanasius #
#--------------------------------------#
#===============================================================================
=comment
Interface
---------
If no command-line arguments are given, the test suite is run.
=cut
#===============================================================================
use v5.32.1; # Enables strictures
use warnings;
use Const::Fast;
use List::Util qw( uniqnum );
use Math::Prime::Util qw( factor );
use Regexp::Common qw( number );
use Test::More;
const my $USAGE =>
"Usage:
perl $0 [<int> ...]
perl $0
[<int> ...] A non-empty list of unique positive integers greater than 2\n";
#-------------------------------------------------------------------------------
BEGIN
#-------------------------------------------------------------------------------
{
$| = 1;
print "\nChallenge 241, Task #2: Prime Order (Perl)\n\n";
}
#===============================================================================
MAIN:
#===============================================================================
{
if (scalar @ARGV == 0)
{
run_tests();
}
else
{
my $int = parse_command_line();
printf "Input: \@int = (%s)\n", join ', ', @$int;
my $sorted = prime_sort( $int );
printf "Output: (%s)\n", join ', ', @$sorted;
}
}
#-------------------------------------------------------------------------------
sub prime_sort
#-------------------------------------------------------------------------------
{
my ($int) = @_;
my %count;
$count{ $_ } = scalar factor( $_ ) for @$int;
return [ sort { $count{ $a } <=> $count{ $b } || $a <=> $b } @$int ];
}
#-------------------------------------------------------------------------------
sub parse_command_line
#-------------------------------------------------------------------------------
{
for (@ARGV)
{
/ ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
$_ > 2 or error( qq["$_" is not greater than 2] );
}
scalar @ARGV == scalar uniqnum( @ARGV )
or error( 'Integers in the input list are not unique' );
return \@ARGV;
}
#-------------------------------------------------------------------------------
sub run_tests
#-------------------------------------------------------------------------------
{
print "Running the test suite\n";
while (my $line = <DATA>)
{
chomp $line;
my ($test_name, $int_str, $exp_str) = split / \| /x, $line;
for ($test_name, $int_str, $exp_str)
{
s/ ^ \s+ //x;
s/ \s+ $ //x;
}
my @integers = split / \s+ /x, $int_str;
my @expected = split / \s+ /x, $exp_str;
my $sorted = prime_sort( \@integers );
is_deeply $sorted, \@expected, $test_name;
}
done_testing;
}
#-------------------------------------------------------------------------------
sub error
#-------------------------------------------------------------------------------
{
my ($message) = @_;
die "ERROR: $message\n$USAGE";
}
################################################################################
__DATA__
Example 1 | 11 8 27 4 | 11 4 8 27
Seq of 200s|207 208 209 210 211 212 213|211 209 213 207 212 210 208