Common prefix and suffix¶
Save all functions into the same file named presuffix.py.
Write the function
common_prefix()that takes two strings each containing a word, and returns another string with the longest common prefix of these words.See the following examples:
>>> common_prefix ('polihedron', 'politechnic') 'poli' >>> common_prefix ('polihedron', 'poli') 'poli' >>> common_prefix ('', '') '' >>> common_prefix ('program', 'algorithm') ''
Note
More tests are provided in the
prefix.txtfile.Write the function
common_suffix()that takes two strings each containing a word, and returns another string with the longest common suffix of these words.See the following examples:
>>> common_suffix ('laicism', 'communism') 'ism' >>> common_suffix ('singing', 'going') 'ing' >>> common_suffix ('hello', 'bye') '' >>> common_suffix ('navigator', 'navigator') 'navigator' >>> common_suffix ('incredible', 'edible') 'edible'
Note
More tests are provided in the
suffix.txtfile.
Solutions
A solution of these functions is provided in the
presuffix.py file.