Python Challenge - Find the Missing Numbers

With the assumption that we don’t have tiles with negative numbers, couldn’t he just substitute 1 for arr[0]? I don’t understand the purpose of your proposed if/else statement.

With regards to the first number, you are correct. You could simply just set the range to start at 1. However, if the last number is removed, you need to verify if the number of items removed is equal to k. In the example of the coding challenge, if I were to remove 10 instead of 9, then my max value would be 9 and I wouldn’t be accounting for this missing number.

I was thinking a similar thing, he could then just set the upper end of his range to be len(arr) + k + 1, that way the total range is 1 to len of the complete set.

def missing_nos(arr, k): return [x for x in range(1, (len(arr) + k + 1)) if x not in arr] print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
1 Like
def missing_nos(arr, k): lst = [] for number in range(1, len(arr) + k): if number not in arr: lst.append(number) return lst print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))

def missing_nos(arr, k):

sort first and set variables

arr.sort()
length = len(arr)

final =

for i in range(length + k):

if arr[i] != i + 1:
  arr.insert(i, -1)
  final.append(i + 1)

return final

print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))

from collections import Counter

def missing_nos(arr, k):
  numbers = Counter(arr) # create counter dict to check between itrebles 
  lst = [] # empty list, fills with result values.
  if len(arr) == 0: # checks if list is not empty.
    return 0

  for i in range(1, max(arr)): # loop trough list and get the max element
    if i not in numbers: # check if the temp variable is not in numbers 
      lst.append(i) # append to new list.

  return lst # returns our new list.
print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
def missing_nos(arr, k):
  # Write your code here
  purelen = len(arr)+2
  y = [x for x in range(arr[0],arr[-1]+1)]
  return sorted(list(set(y).difference(set(arr))))
print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))