Coding Towards The Answer, Part 5
Trace, Compress …
8 min readJun 22, 2023
Assume that we want to compress a series of continuous numbers to a succinct string expression such as n to m
, and be able to convert back to a string of continuous numbers.
Given a list of sorted numbers, let’s curl out the continuous numbers. We assume the distance of 1 for now. Noe that the program can be modified to just check for array[i] < (array[i+1])
instead of array[i] ==(array[i+1]-1)
.
def detect_continuous_numbers(array):
if not array:
return []
number_of_continous_arrays = 0 # counter as key
continuous_numbers_dict = {}
continuous_detected = False
last_index_checked = 0
last_index_collected = -1
uncollected_non_continous = []
for i in range(0, len(array)-1):
#if ((array[i] != (array[i+1])-1) and (continuous_detected == False)):
# print(f"{i}: {array[i]} not collected")
# number_of_continous_arrays = number_of_continous_arrays + 1 # re-assign key as the non-continously take the key (turn this off, if only continous series are considered)
if…