I'm learning to program in Python and for this I make challenges that I encounter on the internet in order to better fix each method, function etc. This challenge consists of creating a function that orders a string in which each word has a number of 1-9 in the middle according to the number contained without removing it from the string.
Ex: "Ess1a 4vida 3a 2é re5al" após a função "Ess1a 2é 3a 4vida re5al"
The code is as follows:
def order(sentence):
new_sent = []
for word in sentence.split():
char = list(word)
for c in char:
if c in str(range(1, 10)):
new_sent.insert(int(c)-1, word)
return " ".join(new_sent)
When testing the code it worked for " is2 Thi1s T4est 3a"
" but it did not work para 'Fo1r the2 4of g3ood pe6ople th5e'
.
I can not find the problem in the code.