Write a program to find missing numbers in a sorted list using Python
Problem : Given a sorted list of integers but some are missing, write a Python program to find all the missing integers.
Example :
Input : [1, 2, 4, 6, 7, 9, 10] Output : [3, 5, 8] Input : [5, 6, 10, 11, 13] Output : [7, 8, 9, 12]
In this article, TipsMake.com will learn with you how to write a program to find missing numbers in a sorted list using Python.
Method 1: Use List Comprehension
You can work around this with Python's built-in List Comprehension. Here is the sample code for your reference:
# Python3 program to Find missing # integers in list def find_missing(lst): max = lst[0] for i in lst : if i > max : max= i min = lst [0] for i in lst : if i < min: min = i missing = max+1 list1=[] for _ in lst : max = max -1 if max not in lst : list1.append(max) return list1 # Driver code lst = [1,5,4,6,8, 2,3, 7, 9, 10] print(find_missing(lst))
The returned result is:
[3, 5, 8]
Method 2: Use List Comprehension with zip()
Still similar to method 1, but you use zip() to shorten the code. Here is sample code:
# Python3 program to Find missing # integers in list def find_missing(lst): return [i for x, y in zip(lst, lst[1:]) for i in range(x + 1, y) if y - x > 1] # Driver code lst = [1, 2, 4, 6, 7, 9, 10] print(find_missing(lst))
The returned result is:
[3, 5, 8]
Method 3: Using set
Using Python's set is an efficient and simple way to find missing numbers in a list. We will convert the list to a set and simply output the difference between this set and the set of integers min to max.
Here is the sample code for your reference:
# Python3 program to Find missing # integers in list def find_missing(lst): return sorted(set(range(lst[0], lst[-1])) - set(lst)) # Driver code lst = [1, 2, 4, 6, 7, 9, 10, 12, 13, 15] print(find_missing(lst))
The returned result is:
[3, 5, 8, 11, 14]
Method 4: Use difference()
This is a similar approach to #3. However, with a slight difference, instead of using the "-" operator to find the difference between two sets, we can use the difference( ) of Python.
Here is sample code:
# Python3 program to Find missing # integers in list def find_missing(lst): start = lst[0] end = lst[-1] return sorted(set(range(start, end + 1)).difference(lst)) # Driver code lst = [1, 2, 4, 6, 7, 9, 10] print(find_missing(lst))
The returned result is:
[3, 5, 8]
Conclude
Finding the missing number in the sorted list is a frequently asked question by employers to check the qualifications of candidates. TipsMake.com hopes that this article will help you.
You should read it
- Write a program to combine two sorted lists in Python
- Why should you learn Python programming language?
- 5 choose the best Python IDE for you
- What is Python? Why choose Python?
- Python online editor
- Write a program to check duplicate values in Python
- For in Python loop
- More than 100 Python exercises have solutions (sample code)
May be interested
- Write a program to move zeros in Pythonin this article, tipsmake.com will work with you to write a program to move zeros in python.
- List () function in Pythonlist () creates a list in python. so what is the syntax of list () function, what parameters does it have and how to use it? invites you to read the track.
- Write a program to turn multiple integers into a single integer using Pythonin this article, tipsmake.com will learn how to write a program to turn multiple integers into a single integer using the python programming language.
- Python online editorthis python online compiler has two parts: the above is called script.py, where you enter python code. click run code when writing the code, the output will display below, in the ipython shell section.
- How to Write a Class in Pythonin python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
- Python data type: string, number, list, tuple, set and dictionaryin this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
- Write a program to calculate the number of ways to climb stairs in Pythonin this article, tipsmake.com will learn with you how to write a program to count the number of ways to climb stairs in python.
- How to Quick Sort an Array in C++sorting is a very useful tool in programming. it is often necessary to arrange the members of a list in ascending or descending order. a sorted list allows a user to search and find information very quickly. sorting a list requires the...
- sorted() function in Pythonwhat is the sorted function in python used for? here's everything you need to know about the sort function in python.
- How to Launch Python Files Using Windows Command Prompttoday's tipsmake will show you how to open python files using the built-in command prompt program on windows computers. in most cases, you will open the file without any problems as long as python is available on your device. if your computer uses an old version of python, or you customized it when installing and did not add the 'python' command to the 'path' variable list, proceed to add python to the 'path' variable list to be able to start. run python files through command prompt.