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
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=' ')
Comments
Leave a comment