Return 基于输入数字的整数列表范围
Return range of integer list based on input number
我在今天的考试中发现了这个问题,我一直试图找到正确的答案,但没有找到。
问题是:
假设我们的页码范围是 0、100。当我们点击页面时,假设是 15,我们只显示 UI 上的 10 页,即从第 10 页到第 20 页
更多示例输入:50 输出:returns列表
[46,47,48,49,50,51,52,53,54,55]
输入:15
输出:returns 列表
[11,12,13,14,15,16,17,18,19,20]
列表还应包括第一页和最后一页,即 0 和 50
所以第一个例子的实际输出是
[0,46,47,48,49,50,51,52,53,54,55,100]
下面是我试过的
def get_thread_page_num(num, max_page_num):
# Returns 10 numbers dynamically
new_lst =[1,50]
# default list
# defult_lst = [1,2,3,4,5,6,7,8,9,10]
num -4 > 0
num+5 <max_page_num
i = 10
m = 4
p = 5
while i != 0:
if num-1 >0 and m !=0:
new_lst.append(num-m)
i=i-1
m = m-1
elif num+1<max_page_num and p != 0:
new_lst.append(num+p)
i=i-1
p = p-1
print(sorted(new_lst))
get_thread_page_num(9, 50)
在您的代码中,m
和 p
分别以值 4 和 5 开头。在每次迭代中,它们中的任何一个都减少 1。因此,在 9 次迭代后它们都为 0,并且 new_lst
包含 9 个元素。 i
也变成 10-9 = 1。
但是 i
永远不会变成 0 并且循环变成无限。
您可以试试下面的代码。请参考评论。
def get_thread_page_num(num, max_page_num):
# low and high denotes the low and high end of the list
# where middle element is num
low = max(0, num - 4)
high = min(num + 5, max_page_num)
lst = []
if max_page_num < 9:
# 10 element list is not possible
return lst
# In case high is same as max, just make the list as
# high-9, high -8, ..., high
if high == max_page_num:
lst = list(range(max(0, high - 9), high + 1))
else:
# Just create a list starting from low like -
# low, low + 1, ..., low + 9
lst = list(range(low, low+10))
# Add 0 and max if not already present
if 0 not in lst:
lst.append(0)
if max_page_num not in lst:
lst.append(max_page_num)
# return sorted lst
return sorted(lst)
致电get_thread_page_num()
:
print(get_thread_page_num(15, 50))
print(get_thread_page_num(0, 50))
print(get_thread_page_num(2, 50))
print(get_thread_page_num(50, 50))
print(get_thread_page_num(43, 50))
输出:
[0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 50]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50]
[0, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[0, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50]
我在今天的考试中发现了这个问题,我一直试图找到正确的答案,但没有找到。
问题是:
假设我们的页码范围是 0、100。当我们点击页面时,假设是 15,我们只显示 UI 上的 10 页,即从第 10 页到第 20 页
更多示例输入:50 输出:returns列表
[46,47,48,49,50,51,52,53,54,55]
输入:15 输出:returns 列表
[11,12,13,14,15,16,17,18,19,20]
列表还应包括第一页和最后一页,即 0 和 50
所以第一个例子的实际输出是
[0,46,47,48,49,50,51,52,53,54,55,100]
下面是我试过的
def get_thread_page_num(num, max_page_num):
# Returns 10 numbers dynamically
new_lst =[1,50]
# default list
# defult_lst = [1,2,3,4,5,6,7,8,9,10]
num -4 > 0
num+5 <max_page_num
i = 10
m = 4
p = 5
while i != 0:
if num-1 >0 and m !=0:
new_lst.append(num-m)
i=i-1
m = m-1
elif num+1<max_page_num and p != 0:
new_lst.append(num+p)
i=i-1
p = p-1
print(sorted(new_lst))
get_thread_page_num(9, 50)
在您的代码中,m
和 p
分别以值 4 和 5 开头。在每次迭代中,它们中的任何一个都减少 1。因此,在 9 次迭代后它们都为 0,并且 new_lst
包含 9 个元素。 i
也变成 10-9 = 1。
但是 i
永远不会变成 0 并且循环变成无限。
您可以试试下面的代码。请参考评论。
def get_thread_page_num(num, max_page_num):
# low and high denotes the low and high end of the list
# where middle element is num
low = max(0, num - 4)
high = min(num + 5, max_page_num)
lst = []
if max_page_num < 9:
# 10 element list is not possible
return lst
# In case high is same as max, just make the list as
# high-9, high -8, ..., high
if high == max_page_num:
lst = list(range(max(0, high - 9), high + 1))
else:
# Just create a list starting from low like -
# low, low + 1, ..., low + 9
lst = list(range(low, low+10))
# Add 0 and max if not already present
if 0 not in lst:
lst.append(0)
if max_page_num not in lst:
lst.append(max_page_num)
# return sorted lst
return sorted(lst)
致电get_thread_page_num()
:
print(get_thread_page_num(15, 50))
print(get_thread_page_num(0, 50))
print(get_thread_page_num(2, 50))
print(get_thread_page_num(50, 50))
print(get_thread_page_num(43, 50))
输出:
[0, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 50]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 50]
[0, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[0, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50]