Python - K 大于 N 的连续范围
给定一个元素列表,任务是编写一个Python程序来获取所有 K 大于 N 的范围。
Input : [2, 6, 6, 6, 6, 5, 4, 6, 6, 8, 4, 6, 6, 6, 2, 6], K = 6, N = 3
Output : [(1, 4), (11, 13)]
Explanation : 6 is consecutive from index 1 to 4, hence 1-4 in result. 7-8 also has 6, but its less than 3 size range, hence not included in result.
Input : [2, 1, 1, 1, 1, 5, 4, 1, 1], K = 1, N = 3
Output : [(1, 4)]
Explanation : 1 is consecutive from index 1 to 4, hence 1-4 in result. 7-8 also has 1, but its less than 3 size range, hence not included in result.
方法#1:使用循环
在这种情况下,跟踪 K 的每次出现,并使用嵌套循环来获取范围的大小。如果范围大小大于 N,则将范围记录在结果中。
Python3
# Python3 code to demonstrate working of
# Consecutive Ranges of K greater than N
# Using loop
# initializing list
test_list = [2, 6, 6, 6, 6, 5, 4, 6,
6, 8, 4, 6, 6, 6, 2, 6]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# initializing N
N = 3
res = []
strt, end = 0, 0
prev = 1
for idx, ele in enumerate(test_list):
# if ele K assign end
if ele == K:
end = idx
# if prev ele not K, reassign start
if prev != K: # previous item one
strt = idx
else:
# if range is greater than N, append to result
if prev == K and end - strt + 1 >= N:
res.append((strt, end))
prev = ele
# printing result
print("The extracted ranges : " + str(res))
Python3
# Python3 code to demonstrate working of
# Consecutive Ranges of K greater than N
# Using enumerate() + zip() + list slice + list comprehension
# initializing list
test_list = [2, 6, 6, 6, 6, 5, 4, 6,
6, 8, 4, 6, 6, 6, 2, 6]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# initializing N
N = 3
# getting break pairs indices
brk_pairs = [idx for idx, (x, y) in enumerate(
zip(test_list, test_list[1:]),
1) if (x == K) != (y == K)]
# The ranges are checked for size required
res = [(idx, ele - 1) for idx, ele in zip([K] + brk_pairs,
brk_pairs + [len(test_list)])
if ele - idx >= N and test_list[idx] == K]
# printing result
print("The extracted ranges : " + str(res))
输出:
The original list is : [2, 6, 6, 6, 6, 5, 4, 6, 6, 8, 4, 6, 6, 6, 2, 6]
The extracted ranges : [(1, 4), (11, 13)]
方法#2:使用enumerate() + zip() +列表切片+列表理解
在这种情况下,K 的所有结束和开始对都分别与前一个和下一个元素一起提取。然后检查对索引是否具有所需的范围以添加到列表中的结果。
蟒蛇3
# Python3 code to demonstrate working of
# Consecutive Ranges of K greater than N
# Using enumerate() + zip() + list slice + list comprehension
# initializing list
test_list = [2, 6, 6, 6, 6, 5, 4, 6,
6, 8, 4, 6, 6, 6, 2, 6]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# initializing N
N = 3
# getting break pairs indices
brk_pairs = [idx for idx, (x, y) in enumerate(
zip(test_list, test_list[1:]),
1) if (x == K) != (y == K)]
# The ranges are checked for size required
res = [(idx, ele - 1) for idx, ele in zip([K] + brk_pairs,
brk_pairs + [len(test_list)])
if ele - idx >= N and test_list[idx] == K]
# printing result
print("The extracted ranges : " + str(res))
输出:
The original list is : [2, 6, 6, 6, 6, 5, 4, 6, 6, 8, 4, 6, 6, 6, 2, 6]
The extracted ranges : [(1, 4), (11, 13)]