Answer to Question #334977 in Python for sushmanth

Question #334977

Given a sentence with numbers representing a word's location embedded within each word of given sentence, return the sentence according to the number in each word.




Example:


str = "is2 thi1s T4est 3a"




the word " is2 " contains 2 - so the word "is location is 2


the word "Thi1s" contains 1 - so the word "this" location is 1


the word "T4est" contains 4 - so the word "Test" location is 4


the word "3a" contain 3 - so the word "a" location is 3




this is a Test




Constraints:


only the integers 1-9 will be there




input format :


the first line contains a string input


Input:

is3 cri1stiano 4the rona2ldo 5best


expected output:

cristiano Ronaldo is the best




1
Expert's answer
2022-04-28T13:21:14-0400
sentence = "is3 cri1stiano 4the rona2ldo 5best"

new = " ".join(t[1] for t in sorted([(int(l), w) for w in sentence.split() for l in w if l.isdigit()], key=lambda t: t[0]))
new_split = new.split(' ')
for i in range(len(new_split)):
    for digit in new_split[i]:
        if digit.isdigit():
            new_split[i] = new_split[i].replace(digit, '')
    print(new_split[i], end=' ')

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