我正在编写一个递归函数,该函数将整数作为输入,它将返回整数中出现123的次数。
例如:
打印(一二三(123123999123))
将打印出 3,因为序列 123 在我输入到函数中的数字中出现 3 次。
这是我到目前为止的代码:
def onetwothree(x):
count = 0
while(x > 0):
x = x//10
count = count + 1
if (count < 3): #here i check if the digits is less than 3, it can't have the sequence 123 if it doesn't have 3 digits
return 0
if (x%10==1 and x//10%10 == 2 and x//10//10%10==3):
counter += 1
else:
return(onetwothree(x//10))
这会继续打印“0”。
慕标琳琳
拉丁的传说
相关分类