diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
| commit | f86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch) | |
| tree | 0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-057/paulo-custodio/python/ch-2.py | |
| parent | ff8719c86653d5ad3121955e9494a0010527c2b9 (diff) | |
| parent | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff) | |
| download | perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2 perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
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:]))) |
