diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2024-09-16 09:41:35 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2024-09-16 09:41:35 +0200 |
| commit | 65b9d6b25e0a823ca7ab6d15744ff98eb3697471 (patch) | |
| tree | fcbcdebd50e3e146dfecf519701ec04b191053eb /challenge-057/paulo-custodio/python/ch-2.py | |
| parent | bd1fe7ae50ca42bda58c134b9edfdc287fb3f386 (diff) | |
| parent | 68e321dd32a834f54b55d5e8924f04358e41cf1f (diff) | |
| download | perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.gz perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.tar.bz2 perlweeklychallenge-club-65b9d6b25e0a823ca7ab6d15744ff98eb3697471.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-057/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-057/paulo-custodio/python/ch-2.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-057/paulo-custodio/python/ch-2.py b/challenge-057/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..5f833663b5 --- /dev/null +++ b/challenge-057/paulo-custodio/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# 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" ] + +import re +import sys + +def shortest_prefix1(word, words): + for i in range(1, len(word)): + prefix = word[:i] + found = 0 + for x in words: + if re.search(r'^'+prefix, x): + found += 1 + if found == 1: + return prefix + return word + +def shortest_prefix(words): + prefixes = [] + for word in words: + prefixes.append(shortest_prefix1(word, words)) + return prefixes + +print(" ".join(shortest_prefix(sys.argv[1:]))) |
