forked from manwar/perlweeklychallenge-club
/
ch-2.pl
executable file
·63 lines (49 loc) · 1.41 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
#!/usr/bin/env perl
use v5.38;
use List::Util qw( product );
sub persistenceSort {
my @int = @_;
my %step_count;
my $steps;
# first, calculates the steps for each number
foreach my $num ( @int ) {
$step_count{$num} = 0;
$steps .= "\n$num"; # our starting number
my $num_copy = $num; # copy the num so we can modify it
while ( length($num_copy) > 1 ) {
# split $num_copy into its individual digits
my @digits = split //, $num_copy;
# generate a new number by multiplying all the digits
$num_copy = product @digits;
# show the multiplication in the steps for this num
$steps .= ' => ' . join(' x ', @digits);
$steps .= " => $num_copy";
# add to our count of steps
$step_count{$num}++;
}
# put the step count in the steps for this num
$steps .=
sprintf " (%d step%s)", $step_count{$num},
$step_count{$num} == 1 ? '' : 's';
}
# now, sort by steps/numeric value
my @sorted = sort {
# sort by step count
$step_count{$a} <=> $step_count{$b}
||
# then sort numerically
$a <=> $b
} @int;
return \@sorted, $steps;
}
sub solution {
my @int = @_;
say 'Input: @int = (' . join(', ', @int) . ')';
my($sorted, $steps) = persistenceSort(@int);
say 'Output: (' . join(', ', @$sorted) . ')';
say $steps;
}
say "Example 1:";
solution(15, 99, 1, 34);
say "\nExample 2:";
solution(50, 25, 33, 22);