Thursday, September 21, 2023
HomeSoftware EngineeringFixing Quantity Zoo Patrol in Python

# Fixing Quantity Zoo Patrol in Python

## The problem#

Write a operate that takes a shuffled listing of distinctive numbers from `1` to `n` with one component lacking (which may be any quantity together with `n`). Return this lacking quantity.

Word: big lists will likely be examined.

Examples:

``````[1, 3, 4]  =>  2
[1, 2, 3]  =>  4
[4, 2, 3]  =>  1
``````

## The answer in Python code#

Possibility 1:

``````def find_missing_number(a):
n = len(a) + 1
return n * (n + 1) // 2 - sum(a)
``````

Possibility 2:

``````def find_missing_number(nums):
return sum(vary(1,len(nums)+2))-sum(nums)
``````

Possibility 3:

``````def find_missing_number(numbers):
if numbers == []:return 1
diff = listing(set(vary(1, len(numbers)+1))- set(numbers))
if diff == []:return max(numbers)+1
return diff
``````

## Take a look at instances to validate our answer#

``````check.assert_equals(find_missing_number([2, 3, 4]), 1)
check.assert_equals(find_missing_number([1, 3, 4]), 2)
check.assert_equals(find_missing_number([1, 2, 4]), 3)
check.assert_equals(find_missing_number([1, 2, 3]), 4)
``````
RELATED ARTICLES