Last active
March 26, 2018 21:51
-
-
Save earthboundkid/011f2e7d3afd3209184eb6b6f54ebe10 to your computer and use it in GitHub Desktop.
Python for making 1, 2 … 5, 6, 7 … 10, 11 pagination correctly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pages(n, max, window=1, ellipsis=None): | |
s = set(range(window + 2)) | |
s.update(range(n-window, n+window+1)) | |
s.update(range(max-window, max+1)) | |
seq = [i for i in sorted(s) if i > 0 and i <= max] | |
res = [1] | |
for i, nxt in zip(seq, seq[1:]): | |
diff = nxt - i | |
if diff == 2: | |
res.append(i+1) | |
elif diff > 2: | |
res.append(ellipsis) | |
res.append(nxt) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment