Skip to content

Instantly share code, notes, and snippets.

@zsoldosp
Created September 16, 2013 21:25
Show Gist options
  • Save zsoldosp/6586803 to your computer and use it in GitHub Desktop.
Save zsoldosp/6586803 to your computer and use it in GitHub Desktop.
re: ayende challange
def assuming_sorted_input_with_callbacks(sorted_unique_integers, callback_to_invoke_on_each_consecutive_set):
""" context:
"""
if not sorted_unique_integers:
return # empty input
curr_lo = curr_hi = sorted_unique_integers[0]
for num in sorted_unique_integers[1:]:
if curr_hi != (num - 1):
callback_to_invoke_on_each_consecutive_set(lo=curr_lo, hi=curr_hi)
curr_lo = curr_hi = num
else:
curr_hi += 1
callback_to_invoke_on_each_consecutive_set(lo=curr_lo, hi=curr_hi)
# Testing could be performed by a little glue test code like the below
def to_expected_output_format_assumed_sorted_input_with_callbacks(unique_integers):
sorted_unique_integers = sorted(unique_integers)
result = []
assuming_sorted_input_with_callbacks(
sorted_unique_integers,
lambda lo, hi: result.append(range(lo, hi + 1))
)
return result
def verify(numbers, expected_output):
actual_output = to_expected_output_format_assumed_sorted_input_with_callbacks(numbers)
assert expected_output == actual_output, 'expected %r, got %r' % (expected_output, actual_output)
if __name__ == '__main__':
verify(numbers=[], expected_output=[])
verify(numbers=[1], expected_output=[[1]])
verify(numbers=[2, 1, 3], expected_output=[[1, 2, 3]])
verify(numbers=[1, 59, 12, 43, 4, 58, 5, 13, 46, 3, 6], expected_output=[[1], [3, 4, 5, 6], [12,13], [43], [46], [58,59]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment