美文网首页
Checkio笔记

Checkio笔记

作者: 鹿呀鹿呀快开门 | 来源:发表于2020-06-02 15:42 被阅读0次

    In a given text you need to sum the numbers. Only separated numbers should be counted. If a number is part of a word it shouldn't be counted.
    The text consists from numbers, spaces and english letters
    Input: A string.
    Output: An int.
    Example:

    sum_numbers('hi') == 0
    sum_numbers('who is 1st here') == 0
    sum_numbers('my numbers is 2') == 2
    sum_numbers('This picture is an oil on canvas '
     'painting by Danish artist Anna '
     'Petersen between 1845 and 1910 year') == 3755
    sum_numbers('5 plus 6 is') == 11
    sum_numbers('') == 0
    

    我的代码如下:

    def sum_numbers(text: str) -> int:
        num = 0
        st_ls = text.split(' ')
        for i in st_ls:
            try:
                num += int(i)
            except: continue
        return num
    

    相关文章

      网友评论

          本文标题:Checkio笔记

          本文链接:https://www.haomeiwen.com/subject/mqwuzhtx.html