[6 kyu] Your order, please

Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).
If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers.

Examples

"is2 Thi1s T4est 3a"  -->  "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2"  -->  "Fo1r the2 g3ood 4of th5e pe6ople"
""  -->  ""

Solution :

def order(sentence):
    list_word = []
    new_list = []
    count = 0
    list_number = ["1","2","3","4","5","6","7","8","9"]

    new_sentence = ""
    list_word = sentence.split()
    for word in list_word:
        new_list.append(word)

    for word in list_word:

        for letter in word:

            if letter in list_number:

                new_list[int(letter) - 1] = word

    for new_word in new_list:
        if count <= len(new_list) - 2:
            new_sentence += new_word + " "
            count += 1

    if len(list_word) != 0:
        new_sentence = new_sentence + new_list[len(new_list)-1]
  # code here
    return new_sentence
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值