I'm new to python and I need help in trying to find all unused and the first unused/missing number in an array (in python 2.7.3)?
The array is of length 20 and is used by an application. When the application is launched, the array is empty, but as users begin to populate it, I need to find all unused numbers and first unused number.
Think of it as a car-park with 20 spots numbered 1 to 20. As people begin to park their cars, the spaces get filled. People may not necessarily park cars in a sequence, two people may park in spots 1 and 16, so i need to find all the missing spots and the first unused parking spot.
Parking cars is given as an example to help you understand what I'm trying to convey. Array will always be integer, and will hold values from 1 to 20.
Below is what the code should do
On Launch, myArray is empty:
myarray = []
So the first missing number should be 1 (i.e. Park car in first spot)
missingNumbers = [1,2,3,.......20]
firstMissingNoInMyArray = 1
As spaces in the array are filled, the array will look like this
myarray = [1, 5, 15]
So first missing number and missing numbers are:
missingNumbers = [2,3,4,6,7,8,9,10,11,12,13,14,16,17,18,19,20].
firstMissingNoInMyArray = 2
I need to see both missing numbers list and the first missing number, can anyone help with Python code, I'm restricted to using Python 2.7.3; if you have a solution in Python 3, do write it, it may help Python 3 users.
Many thanks in advance.
missingNumbersandfirstMissingNoInMyArrayitems?