Last active
March 1, 2021 18:11
-
-
Save skatenerd/34baec0857e9d7dbd31f3227e64bfb13 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
from functools import reduce | |
def test(f): | |
assert f([1,2,2,2,3]) == [[2,2,2]] | |
assert f([1,4,4,6,3,3,3,2,5,5,5,3,1]) == [[3,3,3],[5,5,5]] | |
assert f([1,2,2,2,3,2,2,2,2]) == [[2,2,2],[2,2,2,2]] | |
def get_from_array(array, index): | |
if index < 0: | |
return None | |
try: | |
return array[index] | |
except IndexError: | |
return None | |
def all_same(items): | |
return len(set(items)) == 1 | |
def get_unique_value(items): | |
if all_same(items): | |
return items[0] | |
def append(items, new): | |
return items + [new] | |
def get_dominant_number(index, to_examine): | |
surrounding_values = [get_from_array(to_examine, i) for i in range(index - 2, index + 3)] | |
left_case = get_unique_value(surrounding_values[:3]) | |
middle_case = get_unique_value(surrounding_values[1:-1]) | |
right_case = get_unique_value(surrounding_values[-3:]) | |
return left_case or middle_case or right_case | |
def using_reduce(to_trim): | |
def append_new_number(state, index): | |
built_so_far, on_a_roll = state | |
candidate = to_trim[index] | |
if get_dominant_number(index, to_trim): | |
if on_a_roll and candidate == built_so_far[-1][-1]: | |
to_edit = built_so_far[-1] | |
with_appended = append(built_so_far[:-1], append(to_edit, candidate)) | |
return (with_appended, True) | |
else: | |
return (append(built_so_far, [candidate]), True) | |
else: | |
return (built_so_far, False) | |
return reduce(append_new_number, range(len(to_trim)), ([], False))[0] | |
def cluster_by_characterization(to_cluster, characterizer): | |
item_count = len(to_cluster) | |
index_pairs = zip(range(item_count - 1), range(1, item_count)) | |
for pair in index_pairs: | |
shared_characterization = get_unique_value([characterizer(to_cluster[x][0]) for x in pair]) | |
if shared_characterization is not None: | |
with_progress = to_cluster[:pair[0]] + [to_cluster[pair[0]] + to_cluster[pair[1]]] + to_cluster[(pair[1] + 1):] | |
return cluster_by_characterization(with_progress, characterizer) | |
return to_cluster | |
def more_data_structurey_way(to_trim): | |
# for input of [1,2,2,2,3], make a list-of-lists containing the indexes: | |
# [[0], [1], [2], [3], [4]], | |
# and then CLUSTER these together based on whether the values at various indices share a 'dominant number' | |
# so we get: | |
# [[0], [1,2,3], [4]] | |
# which we then convert back into: | |
# [[1], [2,2,2], [3]] | |
indexes = cluster_by_characterization([[e] for e in range(len(to_trim))], lambda e: get_dominant_number(e, to_trim)) | |
trimmed = [e for e in indexes if len(e) > 1] | |
return [[to_trim[index] for index in e] for e in trimmed] | |
test(using_reduce) | |
test(more_data_structurey_way) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment