﻿ Python - Sum of missing numbers of a list of integers

# Python Exercises: Sum of missing numbers of a list of integers

## Python List: Exercise - 278 with Solution

Write a Python program to sum the missing numbers in a given list of integers.

Sample Data:
([0, 3, 4, 7, 9]) -> 22
([44, 45, 48]) -> 93
([-7, -5, -4, 0]) -> -12

Sample Solution-1:

Python Code:

``````def test(nums):
max_num = max(nums)
min_num = min(nums)
return (max_num+min_num)*(max_num-min_num+1)//2 - sum(nums)

nums = [0, 3, 4, 7, 9]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
nums = [44, 45, 48]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
nums = [-7, -5, -4, 0]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
```
```

Sample Output:

```Original list:
[0, 3, 4, 7, 9]
Sum of missing numbers of the said list of integers:
22
Original list:
[44, 45, 48]
Sum of missing numbers of the said list of integers:
93
Original list:
[-7, -5, -4, 0]
Sum of missing numbers of the said list of integers:
-12
```

Flowchart: ## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

``````def test(nums):
max_num = max(nums)
min_num = min(nums)
return sum(range(min_num, max_num+1))-sum(nums)

nums = [0, 3, 4, 7, 9]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
nums = [44, 45, 48]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
nums = [-7, -5, -4, 0]
print("Original list:")
print(nums)
print("Sum of missing numbers of the said list of integers:")
print(test(nums))
```
```

Sample Output:

```Original list:
[0, 3, 4, 7, 9]
Sum of missing numbers of the said list of integers:
22
Original list:
[44, 45, 48]
Sum of missing numbers of the said list of integers:
93
Original list:
[-7, -5, -4, 0]
Sum of missing numbers of the said list of integers:
-12
```

Flowchart: ## Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: Largest, lowest gap between sorted values of a list.
Next Python Exercise: Check if each number is prime in a list of numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Print Imported Modules:

```import sys
imported_modules = [m.__name__ for m in sys.modules.values() if m]
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook