Code Golf - 根据当前页面生成附近的页码
挑战在于创建一种算法,用于根据序列中的当前位置生成序列中特定大小的数字子集。
在 Stack Overflow 或 Digg 等繁忙网站上的许多内容页面中导航时,通常希望为用户提供一种快速跳转到第一页、最后一页或附近的特定页面的方法。 em> 他们正在查看的当前页面。
要求
- 始终显示第一个和最后一个页码
- 页码的子集将 包含当前页码 以及之前和/或之前的页码 在它之后(取决于当前页面)
- 页码的子集将 始终是固定数量的页面并且 永远不能超过或低于 该固定数字除非:
总页数 <固定宽度
- 当前页面的位置 子集中的个数是固定的 除非:
1 <= 当前页面 < (固定宽度 - 默认位置)
或- <代码>(总页数 - 当前页)< (固定宽度 - 默认位置)
- 输出应指示何时存在 第一个之间的差异大于0 数据页和第一页 子集以及之间 子集的最后一页和最后一页 的数据。该指示器应该出现 在任一位置最多一次。
如果您还无法想象这一点,请在问题/答案下查看您的 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]< /code>
当前页: 30 of 30
输出: [1]...[26][27][28][29][x]
当前页: 3 of 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]
- 如果距离 在子集的任一端和第一个之间 或者最后一页小于 1。因此,有可能 具有不间断的页面序列
fixedWidth + 2
如[1][2][3][x][5][6]...[15]
或[1][2][3][x][5][6][7]
欢迎提供任何及所有语言的解决方案。
祝你好运!
The challenge is to create an algorithm for generating a specifically-sized subset of numbers in a sequence based on the current position in that sequence.
While navigating through the many pages of content on a busy site like Stack Overflow or Digg it is often desirable to give the user a way to quickly jump to the first page, the last page or a specific page which is near the current page they are viewing.
Requirements
- First and last page numbers are always displayed
- The subset of page numbers will
contain the current page number
as well as page numbers before and/or
after it (depending on current page) - The subset of page numbers will
always be a fixed number of pages and
can never exceed or fall short of
that fixed number unless:totalPages
< fixedWidth
- The position of the current page
number in the subset is fixed
unless:1 <= currentPage <
(fixedWidth - defaultPostion)
or(totalPages -
currentPage) < (fixedWidth - defaultPostion)
- Output should indicate when there is a
difference greater than 0 between the first
page of data and the first page of
the subset as well as between the
last page of the subset and the last page
of data. This indicator should appear
at most once in either position.
If you can't picture this yet, take a look at your Stack Overflow profile under questions/answers. If you have more than 10 of either one, you should see paging links at the bottom which are generated in exactly this fashion. That, or scroll to the bottom of http://digg.com and observe their paging control.
Examples
All examples assume a subset size of 5 and the current page in position 3, but these
should be configurable in your solution. ...
indicates the gap between page numbers, [x]
indicates the current page.
Current Page: 1 of 30
Output: [x][2][3][4][5]...[30]
Current Page: 2 of 30
Output: [1][x][3][4][5]...[30]
Current Page: 13 of 30
Output: [1]...[11][12][x][14][15]...[30]
Current Page: 27 of 30
Output: [1]...[25][26][x][28][29][30]
Current Page: 30 of 30
Output: [1]...[26][27][28][29][x]
Current Page: 3 of 6
Output: [1][2][x][4][5][6]
Current Page: 4 of 7
Output: [1][2][3][x][5][6][7]
Additional Clarifications
- First and last pages do not count toward
numberOfPages
unless they are sequentially
part ofnumberOfPages
as in[1][x][3][4][5]...[30]
or[1]...[26][27][28][x][30]
, but not in[1]...[8][9][x][11][12]...[30]
- No gap indicator should be included if the distance
between either end of the subset and the first
or last page is less than 1. Thus, it is possible
to have a non-breaking sequence of pages up tofixedWidth + 2
as in[1][2][3][x][5][6]...[15]
or[1][2][3][x][5][6][7]
Solutions in any and all languages are welcome.
Good luck!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Python -
156182140 个字符并针对 OP 中的示例进行测试:
感谢您的评论。 :)
PS。进行了大量编辑,以减少字符数并添加
n
=当前页周围的页数(m
是最大页数,c
是当前页号)Python -
156182140 charactersAnd testing against examples in OP:
Thanks for the comments. :)
PS. heavily edited to decrease char count and to add
n
=number of pages around the current one (m
is max number of pages andc
is the current page no)GolfScript -
898078 个字符示例 I/O:
所有页码的输出需要 83 个字符(对主体进行少量修改)。
输入/输出示例:
GolfScript -
898078 charsSample I/O:
Output for all page numbers takes 83 chars (minor modifications to the main body).
Sample I/O:
F#! - 233 个重要字符。
所有选项均受支持且符合规格。
程序:
测试:
输出:
F# ! - 233 significant characters.
All options supported and within specs.
Program:
Test:
Output:
Common Lisp:262 个有效字符
未压缩:
测试:
子集大小和当前页面在该子集中的位置可以在可选参数中给出 (
:current-position
在子集中是从零开始的,自然):编辑:压缩版本中的调用将是:
Common Lisp: 262 significant characters
Uncompressed:
Testing:
The subset size and the position of the current page in that subset can be given in optional parameters (
:current-position
is zero-based within the subset, naturally):EDIT: The call in the compressed version would be:
PHP,234 个字符
(排序)未缩小:
这不完全遵循规范(
[1][x][3][4]...[30]
而不是[ 1][x][3][4][5]...[30]
),但这样做会变得不太优雅。PHP, 234 chars
(Sort of) unminified:
This doesn't follow the spec exactly (
[1][x][3][4]...[30]
instead of[1][x][3][4][5]...[30]
), but it would become a lot less elegant accounting for that.C#,240/
195184 个字符与其他 C# 答案类似,但有一些令人讨厌的副作用填充 LINQ。我想这可能会更短一些。
编辑:
事实证明命令式版本要短很多(
195184 个字符):C#, 240/
195184 charsSimilar to the other C# answer but with some nasty side-effect filled LINQ. I would imagine this could be somewhat shorter.
Edit:
Turns out the imperative version is shorter by a good margin (
195184 characters):Perl,92 个字符
完整测试:
Perl, 92 chars
Full test:
Python - 321 个字符
我假设您在命令行(stdin)上输入当前页和总页数:
没有真正打高尔夫球(只是短名称),所以我预计最好的解决方案至少是这个长度的一半。
示例
编辑:我意识到这对于“2 4”或“2 2”之类的内容会失败 - 它假设至少有 6 页。 耸耸肩
Python - 321 characters
I'm assuming you're typing in the current page and total pages on the command line (stdin):
Not really golfed (just short names) so I expect the best solution to be at least half this length.
Example
Edit: I realize this fails for thing like "2 4" or "2 2" - it assumes that are at least 6 pages. shrug
Groovy:
242232个字符,支持可配置的组长度调用语法:
Paging(currentOffset,totalWidth,groupSize)
可读版本:
这样调用:
产生以下结果:
Groovy:
242232 chars, supports configurable group lengthCall syntax:
Paging(currentOffset, totalWidth, groupSize)
Readable version:
Calling it like this:
produces these results:
C# 278 字符
程序
测试
输出
C# 278 Characters
Program
Test
Output
Ruby 1.9 - 197 个字符
用法:
ruby pager.rb [position] [sampleSize] [totalWidth]
Ruby 1.9 - 197 characters
Usage:
ruby pager.rb [position] [sampleSize] [totalWidth]
Python - 334 个字符 - 完整的功能
我意识到已经发布了一个较短的答案,但该答案不支持页面子集中的可配置宽度和位置。我的就是这样。
以下是全部通过的测试
这是我的第一次代码高尔夫,很棒的东西,从现在开始要做更多事情:P
Python - 334 characters - complete functionality
I realize a shorter answer has already been posted, but that one doesn't support configurable width and position in the subset of pages. Mine does.
And here are the tests that all pass
This is my first code-golf, awesome stuff, going to do a lot more from now on :P
Javascript -
398393 个字符工作函数
v(j, o, l)
,其中:j
是页码o
是总页数l
是要显示的页数(子集大小)v(10, 30, 5)
返回:[1]...[8][9][x][11][12]...[30]
未压缩版本
一个简单的测试函数
Javascript -
398393 charactersWorking Function
v(j, o, l)
, where:j
is the page numbero
is the total number of pagesl
is the number of pages to display (subset size)v(10, 30, 5)
returns:[1]...[8][9][x][11][12]…[30]
Uncompressed version
A simple test function
红宝石 1.9.1 — 114
Ruby 1.9.1 — 114