Concatenate Word Pairs
Given a sentence and an integer L, write a program to concatenate word pairs so that the concatenated word has the length L.
The first line of input will be a sentence.
The second line of input will be an integer L.
The output should be containing the unique concatenated word pairs each in a line in the lexicographical order.
For example, if the given sentence and L are
Welcome to your exam
6
The words which can be paired so that they make concatenated word with length 6 are
Word1Word2toyourtoexamexamtoyourto
So the output should be printing each concatenated word in a line in the lexicographical order
examto
toexam
toyour
yourto
Sample Input 1
Welcome to your exam
6
Sample Output 1
examto
toexam
toyour
yourto
Sample Input 2
My parents and I went to a movie
9
Sample Output 2
Myparents
moviewent
parentsMy
parentsto
toparents
wentmovie
def concatenate(words:list, n:int):
l = len(words)
res = []
for i in range(l):
for j in range(l):
if i == j:
continue
else:
tmp = words[i] + words[j]
if len(tmp) == n:
res.append(tmp)
print(*sorted(res), sep='\n')
while True:
s = input('sentense : ').split()
try:
n = int(input('N = '))
except ValueError:
print('incorect input')
continue
concatenate(s, n)
Comments
Leave a comment