Nat*_*lor 20 algorithm code-golf rosetta-stone
挑战在于创建一种算法,用于基于该序列中的当前位置在序列中生成特定大小的数字子集.
在Stack Overflow或Digg等繁忙网站上浏览多页内容时,通常需要为用户提供一种快速跳转到第一页,最后一页或当前页面附近的特定页面的方法.观看.
totalPages
< fixedWidth1 <= currentPage <
(fixedWidth - defaultPostion)
要么(totalPages -
currentPage) < (fixedWidth - defaultPostion)如果您还无法想象,请查看问题/答案下的Stack Overflow配置文件.如果你有一个超过10个,你应该在底部看到以这种方式生成的分页链接.那,或滚动到http://digg.com的底部,并观察他们的分页控制.
所有示例都假设子集大小为5,当前页面位于第3位,但这些应该可以在您的解决方案中进行配置....表示页码之间的差距,[x]表示当前页面.
当前页:1/30
输出: [x][2][3][4][5]...[30]
当前页:2/30
输出: [1][x][3][4][5]...[30]
当前页:13/30
输出: [1]...[11][12][x][14][15]...[30]
当前页:27/30
输出: [1]...[25][26][x][28][29][30]
当前页:30/30
输出: [1]...[26][27][28][29][x]
当前页:3/6
输出: [1][2][x][4][5][6]
当前页:4/7
输出: [1][2][3][x][5][6][7]
numberOfPages,除非它们依次是一部分numberOfPages在
[1][x][3][4][5]...[30]或
[1]...[26][27][28][x][30],但不是在
[1]...[8][9][x][11][12]...[30]fixedWidth + 2为[1][2][3][x][5][6]...[15]或
[1][2][3][x][5][6][7]欢迎任何和所有语言的解决方案.
祝好运!
f=lambda c,m,n:'...'.join(''.join((' ','[%s]'%(p,'x')[p==c])[min(m-n,c-1-n/2)<p<max(n+1,c+1+n/2)or p in(1,m)]for p in range(1,m+1)).split())
Run Code Online (Sandbox Code Playgroud)
并针对OP中的示例进行测试:
for c, m, expect in (
(1, 30, "[x][2][3][4][5]...[30]"),
(2, 30, "[1][x][3][4][5]...[30]"),
(13, 30, "[1]...[11][12][x][14][15]...[30]"),
(27, 30, "[1]...[25][26][x][28][29][30]"),
(30, 30, "[1]...[26][27][28][29][x]"),
(3, 6, "[1][2][x][4][5][6]"),
(4, 7, "[1][2][3][x][5][6][7]"),
):
output = f(c, m, 5)
print "%3d %3d %-40s : %s" % (c, m, output, output == expect)
Run Code Online (Sandbox Code Playgroud)
感谢您的评论.:)
PS.大量编辑以减少字数和添加n=当前页面周围的页数(m是最大页数,c是当前页面否)
我假设您在命令行(stdin)上输入当前页和总页数:
import sys
p=sys.stdout.write
c,t=raw_input().split()
c,t=int(c),int(t)
r=range(1,t+1)
l=len(r)
p("[1]")
if c>7:
p("...")
for n in r[c-3:c+2]:
if n==1:continue
if n-t==-5 and l>7:continue
if c==n:n="X"
p("[%s]"%n)
if l<7:
for n in range(2,6):
if c==n:n="X"
p("[%s]"%n)
if r[c+2]<t and l>6:
p("...")
p("[%d]"%t)
Run Code Online (Sandbox Code Playgroud)
没有真正打高尔夫球(只是简称),所以我希望最好的解决方案至少是这个长度的一半。
python pag.py
3 30
[1][2][X][4][5]...[30]
Run Code Online (Sandbox Code Playgroud)
编辑:我意识到这对于“2 4”或“2 2”之类的东西会失败 - 它假设至少有 6 页。耸肩
| 归档时间: |
|
| 查看次数: |
1176 次 |
| 最近记录: |