Last active
November 13, 2015 19:56
-
-
Save philnguyen/c170d173fca873ca811f to your computer and use it in GitHub Desktop.
randomize line batches for project 2 in 723
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
| import sys | |
| import random | |
| # Given file name, make an iterator returning random line batches, each of which is also randomized | |
| def iterRandomLines(filename): | |
| batches = readBatches(filename) | |
| random.shuffle(batches) | |
| for lines in batches: | |
| random.shuffle(lines) #FIXME: we don't want to randomize lines within each batch | |
| for line in lines: | |
| yield line | |
| yield "" | |
| # Given file name, return (list of (listof lines)) grouped by consecutive non-empty lines | |
| def readBatches(filename): | |
| h = open(filename, 'r') | |
| batches = [] | |
| batch = [] | |
| for l in h: | |
| l = l.strip() | |
| if l == "": | |
| batches.append(batch) | |
| batch = [] | |
| else: | |
| batch.append(l) | |
| h.close() | |
| return batches |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment