Answer to Question #334972 in Python for sushmanth

Question #334972

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






























1
Expert's answer
2022-04-28T13:21:17-0400
def order(sentence):
    words = [(int(l), w) for w in sentence.split() for l in w if l.isdigit()]
    words.sort(key=lambda t: t[0])
    return " ".join(t[1] for t in words)


line = "is2 Thi1s T4est 3a"

new = order(line)
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