aboutsummaryrefslogtreecommitdiff
path: root/challenge-057/andrezgz/perl/ch-2.pl
blob: f622980927fe6173c105510fbd4ef9196cd17e66 (plain)
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
#!/usr/bin/perl

# https://perlweeklychallenge.org/blog/perl-weekly-challenge-057/
# Task #2
#
# Shortest Unique Prefix
# Write a script to find the shortest unique prefix for each each word
# in the given list. The prefixes will not necessarily be of the same length.
#
# Sample Input
#     [ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ]
# Expected Output
#     [ "alph", "b", "car", "cadm", "cade", "alpi" ]

use strict;
use warnings;

die "$0 <word1> <word2> ...\n" if @ARGV < 2;

my $input = \@ARGV;
my $l = @$input -1;

my @output;

for my $i (0 .. $l){
    for my $j (1 .. length $input->[$i]){
        my $s = substr $input->[$i],0,$j;
        next if grep { $s eq substr $_,0,$j }
                ( @$input[0 .. $i-1] , @$input[$i+1 .. $l] );
        push @output,$s;
        last;
    }
    push @output,'N/A' if @output != $i+1; #not unique prefix
}

print join ',', @output;

__END__

./ch-2.pl alphabet book carpet cadmium cadeau alpine
alph,b,car,cadm,cade,alpi

./ch-2.pl just another perl hacker
j,a,p,h

./ch-2.pl use uses user
N/A,uses,user