Answer to Question #217018 in Python for srikanth

Question #217018
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.
Input
The first line of input will be a sentence.
The second line of input will be an integer L.
Output
The output should be containing the unique concatenated word pairs each in a line in the lexicographical order.
Explanation
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
Word1	Word2
to	your
to	exam
exam	to
your	to
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
1
Expert's answer
2021-07-14T01:31:45-0400
l = input().strip().split(" ")
n = int(input())
foo = []
ans = []
for i in l:
    if len(i)<n: foo.append(i)
    
for i in range(len(foo)):
    for j in range(len(foo)):
        if (i!=j and len(foo[i]+foo[j])==n):
            ans.append(foo[i]+foo[j])
            ans.append(foo[j]+foo[i])


ans = list(set(ans))
ans.sort()
for i in ans:
    print(i)

Sample input:
My parents and I went to a movie
9

Sample output:
Myparents
moviewent
parentsMy
parentsto
toparents
wentmovie

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS