计算数组中存在多少 "countdown" 个序列
Counting how many "countdown" sequences exists in array
我知道这听起来很简单,但我正在尝试获取数组中存在的“倒计时”序列的计数。例子:
[1,2,3,2,5,4,3,0] - > 2 ([3,2] 和 [5,4,3])
我只需要一点推动,拜托!
只要在每次倒计时结束时遍历列表就可以增加计数器
Python,又称为伪代码:
def count_finder(l):
prev = l[0]
counter = 0
inCount = False
for num in l[1:]:
if num == prev-1: #Checks if the previous was 1 greater than this one
inCount = True # if it is then "inCount" is True
elif num+1 != prev and inCount: #Checks if your exiting a countdown
inCount = False
counter += 1 #Increment Counter
prev = num #Change previous number to current number for next loop
if inCount: counter+=1 #If the loop ends while in a count down increment counter
return counter
print(count_finder([9, 8, 7, 6, 5, 4]))
我知道这听起来很简单,但我正在尝试获取数组中存在的“倒计时”序列的计数。例子: [1,2,3,2,5,4,3,0] - > 2 ([3,2] 和 [5,4,3]) 我只需要一点推动,拜托!
只要在每次倒计时结束时遍历列表就可以增加计数器
Python,又称为伪代码:
def count_finder(l):
prev = l[0]
counter = 0
inCount = False
for num in l[1:]:
if num == prev-1: #Checks if the previous was 1 greater than this one
inCount = True # if it is then "inCount" is True
elif num+1 != prev and inCount: #Checks if your exiting a countdown
inCount = False
counter += 1 #Increment Counter
prev = num #Change previous number to current number for next loop
if inCount: counter+=1 #If the loop ends while in a count down increment counter
return counter
print(count_finder([9, 8, 7, 6, 5, 4]))