Answer to Question #194793 in Python for J NAGAMANI

Question #194793

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

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




1
Expert's answer
2021-05-19T05:04:57-0400
def concatenateStrings(words : list, L : int) :
    size = len(words)
    result = []
    for i in range(size) :
        for j in range(size) :
            if i != j :
                temporary = words[i] + words[j]
                if (len(temporary) == L) :
                    result.append(temporary)
    print(*sorted(result), sep="\n")
    
if __name__ == "__main__" :
    while True :
	    words = input('Sentense : ').split()
	    try:
		    L = int(input('L = '))
	    except ValueError:
		    print('incorect input')
		    continue
	    concatenateStrings(words, L)

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

Vivek
18.05.21, 11:46

Please, Reply as soon as possible......

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS