Esempio n. 1
0
 def test_longest_common_prefix(self):
     # Test first solution
     self.assertEqual("fl", longest_common_prefix_v1(["flower","flow","flight"]))
     self.assertEqual("", longest_common_prefix_v1(["dog","racecar","car"]))
     # Test second solution
     self.assertEqual("fl", longest_common_prefix_v2(["flower","flow","flight"]))
     self.assertEqual("", longest_common_prefix_v2(["dog","racecar","car"]))
     # Test third solution
     self.assertEqual("fl", longest_common_prefix_v3(["flower","flow","flight"]))
     self.assertEqual("", longest_common_prefix_v3(["dog","racecar","car"]))
Esempio n. 2
0
 def test_longest_common_prefix(self):
     # Test first solution
     self.assertEqual(
         "fl", longest_common_prefix_v1(["flower", "flow", "flight"]))
     self.assertEqual("",
                      longest_common_prefix_v1(["dog", "racecar", "car"]))
     # Test second solution
     self.assertEqual(
         "fl", longest_common_prefix_v2(["flower", "flow", "flight"]))
     self.assertEqual("",
                      longest_common_prefix_v2(["dog", "racecar", "car"]))
     # Test third solution
     self.assertEqual(
         "fl", longest_common_prefix_v3(["flower", "flow", "flight"]))
     self.assertEqual("",
                      longest_common_prefix_v3(["dog", "racecar", "car"]))
Esempio n. 3
0
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Reference: https://leetcode.com/problems/longest-common-prefix/description/
"""
from algorithms.strings import longest_common_prefix_v1, longest_common_prefix_v2, longest_common_prefix_v3

a = ["flower", "flow", "flight"]
"""
First solution: Horizontal scanning
"""
print(longest_common_prefix_v1(a))
"""
Second solution: Vertical scanning
"""
print(longest_common_prefix_v2(a))
"""
Third solution: Divide and Conquer
"""
print(longest_common_prefix_v3(a))

b = ["dog", "racecar", "car"]
"""
First solution: Horizontal scanning
"""
print(longest_common_prefix_v1(b))
"""
Second solution: Vertical scanning
"""
print(longest_common_prefix_v2(b))
"""