codewars第一天

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”
“” --> “”

每个单词都有一个数字,再按照数字排序,我感觉可以用字典的特性

思路

  1. python的sort方法很不错,应该可以完成这个问题,我以{num:word}的方式构建字典
    毕竟,我听说python的字典现在是有序的了。接下来用sort排序
  2. 关于提取num,我的想法是先按空格分开构成单个单词的数组,接下来利用ASCII码的特性。把数字拼接出来转化为数字类型,不然不好排序。
  3. ord函数可转化asc码,chr函数转化字符串
    ord(‘0’)=48
    rod(‘9’)=57
  4. 开始干活
def order(sentence):
    if sentence == '':
        return ''
    string_list = sentence.split(' ')
    result = []
    for word in string_list:
        num = ''
        for letter in word:
            if 48 <= ord(letter) <= 57:
                num += letter
        result.append((int(num), word))
    result.sort(key=lambda x: x[0], reverse=False)
    strings = []
    for i in result:
        strings.append(i[1])
    return ' '.join(strings)


if __name__ == "__main__":
    print(order(strings))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值