Last active
November 4, 2023 20:50
-
-
Save simonLeary42/bbd2b5bf1cc3d6066878805b29464125 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
def bool_array_find_clusters_and_islands(bool_array): | |
clusters = [] | |
islands = [] | |
cluster_start_index = None | |
currently_iterating_through_cluster = False | |
for i,value in enumerate(bool_array): | |
value = bool(value) | |
if currently_iterating_through_cluster and (value is True): | |
if i == len(bool_array)-1: # if last element | |
cluster_end_index = i | |
clusters.append((cluster_start_index, cluster_end_index)) | |
continue | |
if currently_iterating_through_cluster and (value is False): | |
cluster_end_index = i-1 | |
if cluster_start_index == cluster_end_index: | |
islands.append(cluster_start_index) | |
else: | |
clusters.append((cluster_start_index, cluster_end_index)) | |
cluster_start_index = None | |
currently_iterating_through_cluster = False | |
continue | |
if (not currently_iterating_through_cluster) and (value is True): | |
cluster_start_index = i | |
currently_iterating_through_cluster = True | |
continue | |
if (not currently_iterating_through_cluster) and (value is False): | |
continue | |
return clusters, islands |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment