Skip to content

Instantly share code, notes, and snippets.

@jcbozonier
Last active September 15, 2016 12:05
Show Gist options
  • Save jcbozonier/19d1651de30912cf18152699cd4016d5 to your computer and use it in GitHub Desktop.
Save jcbozonier/19d1651de30912cf18152699cd4016d5 to your computer and use it in GitHub Desktop.
def reverse_aggregated_partition_list(partitions):
# We're going to now merge partitions working from right
# to left to try and simplify the partitioned data
reversed_partition_list = list(reversed(partitions))
aggregated_partition_list = []
current_partition = reversed_partition_list[0]
for i in range(len(reversed_partition_list)):
if i+1 >= len(reversed_partition_list):
# If we don't have a "next" partition to compare to
# we're done. Save our lst partition and GTFO.
aggregated_partition_list.append(current_partition)
break
else:
# We do have a next partition
next_partition = reversed_partition_list[i + 1]
# If they're similar enough, we should merge them
should_merge = windows_are_similar(current_partition, next_partition)
if should_merge:
# reversed order in append because we're in backwards land
current_partition = next_partition + current_partition
else:
# Not merging so we just put the partition we're looking at in
# our new set of partitions and move on.
aggregated_partition_list.append(current_partition)
current_partition = next_partition
# Gotta reverse that list to undo the reverse we did in the beginning!
return list(reversed(aggregated_partition_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment