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