-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.py
40 lines (33 loc) · 979 Bytes
/
LCS.py
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
#!/usr/bin/env python
from __future__ import division
from collections import Counter
import sys
def moving_window(string, n):
for i in range(0, len(string) - n):
yield string[i:i+n]
def get_common_substring(input):
strings = map(lambda x: x.strip(), input.strip().split("\n"))
smallest_string = min(strings, key = lambda x: len(x))
for n in range(len(smallest_string), 0, -1):
for substring in moving_window(smallest_string, n):
if all(substring in string for string in strings):
return substring
return None
def test():
test_string = '''\
GATTACA
TAGACCA
ATACA'''
result = get_common_substring(test_string)
print(result)
assert result == 'AC'
print('Test passed!')
def main():
if len(sys.argv) > 1:
filename = sys.argv[1]
with open(filename) as file:
print(get_common_substring(file.read()))
else:
test()
if __name__ == '__main__':
main()