Skip to content

Commit 25021fd

Browse files
committed
[Feature] Two methods of finding the longest common prefix in a list of words!
1 parent 54e93de commit 25021fd

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,7 @@ Different algorithmic programs. Grouped by general topic.
6262
* [Permutations](algorithms/string/permutations.py)
6363
* [Combinations](algorithms/string/combinations.py)
6464
* [Letters in Word](algorithms/string/combinations.py)
65-
* [Letters from Phone](algorithms/string/combinations.py)
65+
* [Letters from Phone](algorithms/string/combinations.py)
66+
* [Longest Common Prefix](algorithms/string/longest_common_prefix.py)
67+
* [Trie](algorithms/string/longest_common_prefix.py)
68+
* [Reduce](algorithms/string/longest_common_prefix.py)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from functools import reduce
2+
from structures.trie import Trie
3+
4+
def find_longest_common_prefix(words:list):
5+
"""
6+
Find the longest common prefix in a list of words.
7+
8+
"""
9+
trie = Trie(words)
10+
11+
head = trie.head
12+
13+
prefix = []
14+
15+
while len(head) == 1 and trie.eof not in head:
16+
key, value = head.popitem()
17+
prefix.append(key)
18+
head = value
19+
20+
return "".join(prefix)
21+
22+
def find_longest_common_prefix_reduce(words:list):
23+
"""
24+
Find the lcp in a list of words, using 'reduce' functions.
25+
26+
"""
27+
if not words:
28+
return ''
29+
30+
def common_start(w1, w2):
31+
shorter = w1 if len(w1) < len(w2) else w2
32+
for i in range(0, len(shorter)):
33+
if w1[i] != w2[i]:
34+
return shorter[:i]
35+
return shorter
36+
37+
return reduce(common_start, words)

structures/graph.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,11 @@ def add_adjacent(self, node):
88
self.adjacent_list.add(node)
99

1010
def remove_adjacent(self, node):
11-
self.adjacent_list.remove(node)
11+
self.adjacent_list.remove(node)
12+
13+
def contains_universal_sink(self):
14+
"""
15+
Check if this node is part of a graph with a universal sink.
16+
17+
"""
18+

tests/test_longest_common_prefix.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from algorithms.string.longest_common_prefix import find_longest_common_prefix, find_longest_common_prefix_reduce
3+
4+
class TestLongestCommonPrefix(unittest.TestCase):
5+
6+
def test_lcp(self):
7+
self.assertEqual('he', find_longest_common_prefix(['hello','hellieo','he']))
8+
self.assertEqual('he', find_longest_common_prefix_reduce(['hello','hellieo','he']))
9+
10+
def test_lcp_empty(self):
11+
self.assertEqual('', find_longest_common_prefix([]))
12+
self.assertEqual('', find_longest_common_prefix_reduce([]))
13+
14+
def test_lcp_no_prefix(self):
15+
self.assertEqual('', find_longest_common_prefix(['fcp','jpl','the','first']))
16+
self.assertEqual('', find_longest_common_prefix_reduce(['fcp','jpl','the','first']))
17+
18+
def test_lcp_full_prefix(self):
19+
self.assertEqual('full', find_longest_common_prefix(['full','full','full']))
20+
self.assertEqual('full', find_longest_common_prefix_reduce(['full','full','full']))

0 commit comments

Comments
 (0)