w3resource

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:

Flowchart: Sum of missing numbers of a list of integers.

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:

Flowchart: Sum of missing numbers of a list of integers.

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.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Generates a list, containing the Fibonacci sequence, up until the nth term:

Example:

def fibonacci(n):
  if n <= 0:
    return [0]
  sequence = [0, 1]
  while len(sequence) <= n:
    next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]
    sequence.append(next_value)
  return sequence
print(fibonacci(7))

Output:

[0, 1, 1, 2, 3, 5, 8, 13]