【每日一题1104】提取数字

:woman_mage: 给定一个不小心混进了字母的数字串,请编写一个函数,提取出其中的数字。

示例:
输入:"a6b6c6",输出:666
输入:"aa 112 3dd",输出:1123

题目难度:简单
题目来源:codewars: Filter the number

def get_int(words: str) -> int:
    pass

assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666
def get_int(words: str) -> int:
    return int(''.join([w for w in words if w.isdigit()]))


assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666
def get_int(words:str) -> int:
    result = 0
    for i in words:
        if i.isdigit():
            result = result*10 + int(i)
    return result

这个算法还存在一些问题,如果要将所有的数字提取,那么对前置0的处理不对
但是返回int类型,前置0是可以去掉的

public static String get_int(String text){
String text_int = “”;
for(char ch : text.toCharArray()){
if(Character.isDigit(ch)){
text_int = text_int + ch;}
}
return text_int;
}

    public String getInt(String str){
        String res ="";
        char[] chars = str.toCharArray();
        for (Character c:chars) {
            if (Character.isDigit(c)){
                res+=c;
            }
        }
        return res;
    }
def get_int(self, words: str) -> int:
    '''
    给定一个不小心混进了字母的数字串,请编写一个函数,提取出其中的数字。

    示例:
    输入:"a6b6c6",输出:666。
    输入:"aa 112 3dd",输出:1123
    :return:
    '''
    num:int = 0
    for word in words:
        if word >= "0" and word <= "9":
            word = int(word)
            num = num *10 + word
        else:
            continue
    return num

有点简单

def get_int(words:str):
    res=''
    for i in words:
        if ord(i)>=48 and ord(i)<=57:
            res=res+i
    return int(res)
def get_int(words: str) -> int:
    li=[]
    for i in words:
        if i.isdigit():
            li.append(i)
            
    result=int("".join(li))
    return result

assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666
def get_int(word:str)->int:
    str_list = re.split(r'\D',word)
    num_list =[]
    for i in str_list:
        if i != '':
            num_list.append(i)
    num = int(''.join(num_list))
    return num

import re
def get_int(words:str):

c =
for i in words:
if re.search("\d", i):
c.append(re.search("\d", i).group())

return int("".join(c))

assert get_int(“a1b2c3”) == 123
assert get_int(“aa1bb2cc3dd”) == 123
assert get_int(“hogwarts666”) == 666

Python参考题解

def get_int(words: str) -> int:
    return int(''.join(filter(str.isdigit, words)))

assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666

思路:使用内置函数filter()过滤出由数字组成的列表,然后拼接成数字字符串,最后使用int()函数转成数字类型并返回。

Java参考题解:

public class Kata {
    public static long filterString(final String value) {
        return Long.valueOf(value.replaceAll("\\D", ""));
    }
}
def get_int(words: str) -> int:
    return int(''.join([i for i in words if i.isdigit()]))

assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666
def get_int(words: str) -> int:
    return int(''.join([item for item in list(words) if ord(item)<97]))

assert get_int("a1b2c3") == 123
assert get_int("aa1bb2cc3dd") == 123
assert get_int("hogwarts666") == 666
关闭