| 1 | package String::CommonPrefix; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||
| 10 | 2 | our $DATE = '2025-08-02'; # DATE | |||||||||||||
| 11 | 2 | our $DIST = 'String-CommonPrefix'; # DIST | |||||||||||||
| 12 | 2 | our $VERSION = '0.021'; # VERSION | |||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | our @EXPORT_OK = qw( | |||||||||||||
| 15 | common_prefix | ||||||||||||||
| 16 | majority_prefix | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 6 | ▶ | sub common_prefix { | ||||||||||||
|
Subroutine
common_prefix: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||
| 20 | 6 | ▶ | return undef unless @_; ## no critic: Subroutines::ProhibitExplicitReturnUndef | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | 6 | my $i; | |||||||||||||
| 22 | L1: | ||||||||||||||
| 23 | 6 | ▶ | for ($i=0; $i < length($_[0]); $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | 6 | for (@_[1..$#_]) { | |||||||||||||
| 25 | 9 | ▶ | if (length($_) < $i) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | $i--; last L1; | |||||||||||||
| 27 | } else { | ||||||||||||||
| 28 | 9 | ▶ | last L1 if substr($_, $i, 1) ne substr($_[0], $i, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | } | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | } | ||||||||||||||
| 32 | 6 | substr($_[0], 0, $i); | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | ▶ | sub majority_prefix { | ||||||||||||
|
Subroutine
majority_prefix: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||
| 36 | #my $opts = ref($_[0]) eq 'HASH' ? {%{shift}} : {}; | ||||||||||||||
| 37 | 0 | ▶ | return undef unless @_; ## no critic: Subroutines::ProhibitExplicitReturnUndef | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | my @excluded = map {0} @_; | |||||||||||||
| 40 | 0 | my $min_items_for_majority = int(@_/2) + 1; | |||||||||||||
| 41 | 0 | my $prefix_len = 0; | |||||||||||||
| 42 | 0 | while (1) { | |||||||||||||
| 43 | 0 | my @items; | |||||||||||||
| 44 | # take the next character from items that are still eligible | ||||||||||||||
| 45 | 0 | for my $i (0..$#_) { | |||||||||||||
| 46 | 0 | my $char; | |||||||||||||
| 47 | 0 | ▶ | if ($excluded[$i]) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 0 | $char = undef; | |||||||||||||
| 49 | } elsif (length($_[$i]) < $prefix_len) { | ||||||||||||||
| 50 | 0 | $excluded[$i]++; | |||||||||||||
| 51 | 0 | $char = undef; | |||||||||||||
| 52 | } else { | ||||||||||||||
| 53 | 0 | $char = substr($_[$i], $prefix_len, 1); | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | 0 | push @items, $char; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | #use DD; dd \@excluded; | ||||||||||||||
| 58 | # determine whether there's still a character that belong to the | ||||||||||||||
| 59 | # majority | ||||||||||||||
| 60 | 0 | my %freqs; | |||||||||||||
| 61 | 0 | ▶ | for (@items) { next unless defined; $freqs{$_}++ } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 0 | ▶ | last unless keys %freqs; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | #use DD; dd \%freqs; | ||||||||||||||
| 64 | 0 | my @freqs = sort { $freqs{$b} <=> $freqs{$a} } keys %freqs; | |||||||||||||
| 65 | 0 | my $majority_char = $freqs[0]; | |||||||||||||
| 66 | 0 | ▶ | last if $freqs{$majority_char} < $min_items_for_majority; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 0 | ▶ | for (0 .. $#items) { next unless defined $items[$_]; unless ($items[$_] eq $majority_char) { $excluded[$_]++ } } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 68 | 0 | $prefix_len++; | |||||||||||||
| 69 | #say "D: prefix_len=$prefix_len"; | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | my $first_included; | |||||||||||||
| 73 | 0 | ▶ | for (0 .. $#_) { unless ($excluded[$_]) { $first_included = $_; last } } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | ▶ | return undef unless defined $first_included; ## no critic: Subroutines::ProhibitExplicitReturnUndef | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | #say "D: first_included=$first_included"; | ||||||||||||||
| 76 | 0 | substr($_[$first_included], 0, $prefix_len); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 2 | 1; | |||||||||||||
| 80 | # ABSTRACT: Find prefix common to all strings | ||||||||||||||
| 81 | |||||||||||||||
| 82 | __END__ | ||||||||||||||