Created
May 10, 2012 22:53
-
-
Save mattdeboard/2656434 to your computer and use it in GitHub Desktop.
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 take(n, seq): | |
| "Return first n items of the seq as a list" | |
| return list(islice(seq, n)) | |
| def drop(n, seq): | |
| "Return seq with the first n items removed." | |
| return list(islice(seq, n, None, 1)) | |
| def split_by(n, seq): | |
| return [take(n, seq), drop(n, seq)] | |
| def slices(seq, start=0, end=None, step=2): | |
| fun = split_by | |
| ls = len(seq) | |
| if ls < step and seq: | |
| yield (0, ls) | |
| else: | |
| if ls % step: | |
| rng = xrange(ls/step+1) | |
| else: | |
| rng = xrange(ls/step) | |
| for i in rng: | |
| segment = fun(step, seq[start:]) | |
| start += step | |
| yield segment[0][0], segment[0][-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment