Python 如何返回包含数字和字母的字符串中的最大整数?
对于这个问题,我想返回与其他整数和字符串混合的最大整数。当我测试代码时,我得到的答案是319,而不是51。谁能帮我修一下吗 测试用例:Python 如何返回包含数字和字母的字符串中的最大整数?,python,Python,对于这个问题,我想返回与其他整数和字符串混合的最大整数。当我测试代码时,我得到的答案是319,而不是51。谁能帮我修一下吗 测试用例: >>>biggestBuried('abcd51kkk3kk19ghi') 319 def biggestBuried(s): new_string = '' biggest = 0 for num in s: if num >= '0' and num <= '9':
>>>biggestBuried('abcd51kkk3kk19ghi')
319
def biggestBuried(s):
new_string = ''
biggest = 0
for num in s:
if num >= '0' and num <= '9':
new_string += num
else:
if not(num >= '0') and not(num <= '9'):
return 0
if new_string and int(new_string) > biggest:
biggest = int(new_string)
new_string = ''
if new_string and int(new_string) > biggest:
biggest = int(new_string)
return biggest
>>最大埋深('ABCD51KK3KK19GHI')
319
def(一个或多个):
新字符串=“”
最大值=0
对于s中的num:
如果num>='0'和num='0')而不是(num最大值:
最大值=整数(新字符串)
新字符串=“”
如果新字符串和int(新字符串)>最大值:
最大值=整数(新字符串)
回报最大
循环的问题是,当你找到一个更大的数字时,你只会设置新字符串='
。当你找到一个数字的末尾时,你需要清空它,即使它不是更大的
另外,当你到达末尾时,你需要检查新的\u字符串
是否更大,以防字符串以数字结尾。我想你是想这样做的,但是缩进是错误的-循环需要在之外
def biggestBuried(s):
new_string = ''
biggest = 0
for num in s:
if num.isnumeric():
new_string += num
else: # We're at the end of a number
if new_string and int(new_string) > biggest: # Check if it's a bigger number
biggest = int(new_string)
new_string = '' # Start a new number
if new_string and int(new_string) > biggest: # Check if the final number is biggest
biggest = int(new_string)
return biggest
循环的问题是,当您找到一个更大的数字时,您只会设置new_string='
。每当您找到一个数字的末尾时,您需要清空它,即使它不是更大的
另外,当你到达末尾时,你需要检查新的\u字符串
是否更大,以防字符串以数字结尾。我想你是想这样做的,但是缩进是错误的-
循环需要在之外
def biggestBuried(s):
new_string = ''
biggest = 0
for num in s:
if num.isnumeric():
new_string += num
else: # We're at the end of a number
if new_string and int(new_string) > biggest: # Check if it's a bigger number
biggest = int(new_string)
new_string = '' # Start a new number
if new_string and int(new_string) > biggest: # Check if the final number is biggest
biggest = int(new_string)
return biggest
解决方案已关闭,但需要在第一次出现字符后重置新字符串
new_string = ''
if new_string and int(new_string) > biggest:
上述代码的这一部分永远不会运行,因为new\u string
的计算结果总是False
def biggestBuried(s):
max_val = 0
number = ''
for c in s:
if c.isnumeric():
number += c
if int(number) > max_val:
max_val = int(number)
else:
# Encountered non-numeric character. Reset number.
number = ''
return max_val
解决方案已关闭,但需要在第一次出现字符后重置新字符串
new_string = ''
if new_string and int(new_string) > biggest:
上述代码的这一部分永远不会运行,因为new\u string
的计算结果总是False
def biggestBuried(s):
max_val = 0
number = ''
for c in s:
if c.isnumeric():
number += c
if int(number) > max_val:
max_val = int(number)
else:
# Encountered non-numeric character. Reset number.
number = ''
return max_val
我添加了很多注释来解释代码,希望不会太混乱
def biggestBuried(s):
str_numbers = [str(i) for i in range(0,10)] # create a list of string numbers from 0 to 9
list_int = [] # empty list to be filled with all integers found in string
temp = str() # a temporay string needed to extract each integer
for char in s: # loop all the characters of the string
if char in str_numbers: # if the character is a number ...
temp += char # ...add the character to the temporary string
else:
if temp: # if the temporary string is not empty (i.e. integer previously found)
list_int.append(int(temp)) # add the integer found to the list...
temp = '' # ... and reset the temporary string
if temp: # this is for including the last integer found, in the case it's located right at the end of the string
list_int.append(int(temp))
return max(list_int) # maximum integer among all those found
测试:
编辑:根据注释中的请求更改答案(不尝试/除外)我添加了很多注释来解释代码,希望不会太混乱
def biggestBuried(s):
str_numbers = [str(i) for i in range(0,10)] # create a list of string numbers from 0 to 9
list_int = [] # empty list to be filled with all integers found in string
temp = str() # a temporay string needed to extract each integer
for char in s: # loop all the characters of the string
if char in str_numbers: # if the character is a number ...
temp += char # ...add the character to the temporary string
else:
if temp: # if the temporary string is not empty (i.e. integer previously found)
list_int.append(int(temp)) # add the integer found to the list...
temp = '' # ... and reset the temporary string
if temp: # this is for including the last integer found, in the case it's located right at the end of the string
list_int.append(int(temp))
return max(list_int) # maximum integer among all those found
测试:
编辑:根据评论中的请求更改答案(不带try/except)逻辑是什么?为什么不能是513?如果你能使用任何函数,我建议你使用itertools.groupBy为什么答案不是51319
?我希望答案是51,因为这是最大的整数string@Barmar他指的是用字符串分隔的整数逻辑是什么?为什么不能是513?如果你能使用我建议的任何函数为什么答案不是51319
?我希望答案是51,因为这是这个数字中最大的整数string@Barmar他指的是用stringsHow分隔的整数,我们可以不用它吗?或者只使用max((int(x)代表re.findall(r'\d+',s))中的x)
@Alexander我知道它可以是一行,我只是觉得显示所有的步骤会更清晰。我们不使用re.findall()来做吗?我仍然很困惑。当我们到达数字的末尾时,我们如何清空字符串?我们如何不使用它来做呢?或者只max((int(x)表示re.findall(r'\d+',s)中的x)
@Alexander我知道它可以是一行,我只是觉得在不使用re.findall()的情况下显示所有步骤会更清晰?我仍然很困惑。当我们到达数字的末尾时,我们如何清空字符串?如果没有尝试和例外,我们如何做?@deezy,更正并编辑答案。可能我也更喜欢这种方式。如果没有尝试和例外,我们如何做?@deezy,更正并编辑答案。可能我也更喜欢这种方式。