Hermit crabs adopt other creatures' castoff shells for protection. But as they grow, crabs must move into successively larger shells. This produces a curious phenomenon: when a crab finds a shell that's too big for it, it waits nearby. Other crabs may accumulate, forming a little conga line of dissatisfied shell seekers. Finally a "Goldilocks" crab arrives — a crab large enough to claim the new shell — and now each waiting crab can move into the shell abandoned by its larger neighbor. By cooperating to share a scarce resource, the whole species benefits.
The same thing happens in human societies — when one person finds a new apartment, car, or job, she leaves behind her old one, and the vacancy passes down through society until the final unit is cast away or destroyed. It's called a vacancy chain.
In this assignment we work with sequences of integers that represent shell sizes. A sequence of consecutive integers is a sequence of $$n$$ integers $$a_1, a_2, \ldots, a_n$$ such that $$a_i + 1 = a_{i + 1} (i = 1, \ldots, n - 1)$$. Your task:
Write a function consecutive that takes a sequence (a list or a tuple) of integers. The function must return a Boolean value that indicates whether or not the given sequence forms a sequence of consecutive integers if the integers are sorted in increasing order.
Write a function goldilocks that takes a sequence (a list or a tuple) of integers. In case the given sequence cannot be used to form a sequence of consecutive integers, but if a single integer can be added to the list so that a sequence of consecutive integers can be formed, the function must return this "missing" integer. Otherwise, the function must return the value None.
Write a function move1 that takes a sequence (a list or a tuple) of integers. The function must return a list that contains a copy of the given sequence of numbers, in the given order. In case the given sequence cannot be used to form a sequence of consecutive integers, but if a single integer can be added to the list so that a sequence of consecutive integers can be formed, the function must append this "missing" integer to the end of the list that is returned.
Write a function move2 that takes a list of integers. The function may not return a value (read: the function must return the value None), but in case the given integers cannot be used to form a sequence of consecutive integers, and a single integer can be added to the list so that a sequence of consecutive integers can be formed, this "missing" integer must be appened to the end of the given list.
>>> consecutive([7, 5, 4, 9, 6, 3, 8])
True
>>> consecutive((16, 13, 18, 17, 15, 14, 20))
False
>>> consecutive((3, 4, 1, 6, 8, 7))
False
>>> goldilocks([7, 5, 4, 9, 6, 3, 8])
>>> goldilocks((16, 13, 18, 17, 15, 14, 20))
19
>>> goldilocks((3, 4, 1, 6, 8, 7))
>>> move1([7, 5, 4, 9, 6, 3, 8])
[7, 5, 4, 9, 6, 3, 8]
>>> move1((16, 13, 18, 17, 15, 14, 20))
[16, 13, 18, 17, 15, 14, 20, 19]
>>> move1((3, 4, 1, 6, 8, 7))
[3, 4, 1, 6, 8, 7]
>>> shells = [7, 5, 4, 9, 6, 3, 8]
>>> move2(shells)
>>> shells
[7, 5, 4, 9, 6, 3, 8]
>>> shells = [16, 13, 18, 17, 15, 14, 20]
>>> move2(shells)
>>> shells
[16, 13, 18, 17, 15, 14, 20, 19]
>>> shells = [3, 4, 1, 6, 8, 7]
>>> move2(shells)
>>> shells
[3, 4, 1, 6, 8, 7]