Skip to content

Instantly share code, notes, and snippets.

@dexterous
Last active March 24, 2016 22:48
Show Gist options
  • Save dexterous/617bd9ff489322386f69 to your computer and use it in GitHub Desktop.
Save dexterous/617bd9ff489322386f69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def slicing(lang_idx, lang_cnt, lines):
'''Using islice; short, but requires knowledge of total line count'''
from itertools import islice
return islice(lines, lang_idx, 12, lang_cnt)
def counting(lang_idx, lang_cnt, lines):
'''Using count in conjunction with dropwhile; on and on and on and on...'''
from itertools import count, dropwhile
f = enumerate(lines)
for c in count(lang_idx, lang_cnt):
try:
yield dropwhile(lambda e: e[0] < c, f).next()[1]
except StopIteration:
break
def rotate(l, n):
return l[-n:] + l[:-n]
def compressing(lang_idx, lang_cnt, lines):
'''Using compress in conjunction with cycle; also works on infinite iters'''
from itertools import compress, cycle
selector = rotate([True] + [False] * (lang_cnt - 1), lang_idx)
return compress(lines, cycle(selector))
def main(strategy):
print strategy.__doc__
lang_cnt = 4
for i in range(0, lang_cnt):
print "Language", i
for l in strategy(i, lang_cnt, open('input')):
print l.strip()
print ''
if __name__ == '__main__':
main(slicing)
main(counting)
main(compressing)
#!/bin/bash -e
#set -o xtrace
for i in {1..4}; do echo "Language $i"; sed -nre "${i}~4 p" input; echo ''; done
English
Spanish
German
French
English
Spanish
German
French
English
Spanish
German
French
@dexterous
Copy link
Author

Cooked up in response to https://groups.google.com/d/msg/mumpy/jXGcvMkLWIk/2apgkf48EwAJ

Like I said in the forum, the batteries are included 🔋🔋 - ⚡ Power Up! ⚡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment