Debugging task: fix a bug in a given code snippet making no more than three changes.
Asked in the Codility online coding stage, alongside two easy-to-medium coding questions.
The function receives a list of integers and must return the smallest positive integer absent from the list. Fix the snippet below without changing its function signature or using additional arrays. For example, [3, 4, -1, 1] returns 2, and [1, 2, 3] returns 4.
def first_missing_positive(nums):
for i in range(len(nums)):
while 1 <= nums[i] <= len(nums) and nums[nums[i]] != nums[i]:
nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
for i, value in enumerate(nums):
if value != i + 1:
return i + 1
return len(nums) + 1
Input is a list of integers. Return one integer.
def first_missing_positive(nums):