Skip to content

Instantly share code, notes, and snippets.

@philnguyen
Last active November 13, 2015 19:56
Show Gist options
  • Select an option

  • Save philnguyen/c170d173fca873ca811f to your computer and use it in GitHub Desktop.

Select an option

Save philnguyen/c170d173fca873ca811f to your computer and use it in GitHub Desktop.
randomize line batches for project 2 in 723
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