Answer to Question #183889 in Python for phani

Question #183889
Word Mix
Given a sentence, write a program to mix the words based on their index locations. The first word in the output should contain the first letter of each word in the given sentence and the second word should contain the second letter of each word in the given sentence and so on.

Note: The nth word in the output should contain nth letter of each word in the given sentence. The letters of the output word should be in same order as the words in the given sentence. If a word in the given sentence doesn't have n-letters, you can skip it while considering the letters of the n-th word.
Input

The input will be a single line containing a string.

Welcome	to	your	first	problem	Mix Word
W	t	y	f	p	Wtyfp
e	o	o	i	r	eooir
l		u	r	o	luro
c		r	s	b	crsb
o			t	l	otl
m				e	me
e				m	em

So the output should be
Wtyfp eooir luro crsb otl me em

Sample Input 1
Welcome to your first problem
Sample Output 1
Wtyfp eooir luro crsb otl me em
Sample Input 2
Do you eat ice cream
Sample Output 2
Dyeic ooacr utee a m
1
Expert's answer
2021-04-21T18:43:04-0400
def mix_words(s):
    words = s.split()
    n = max([len(w) for w in words])

    res = ['']*n
    for i in range(n):
        for w in words:
            if i < len(w):
                res[i] += w[i]
    return ' '.join(res)

def main():
    s = input()
    print(mix_words(s))

if __name__ == '__main__':
    main()

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