Python Challenge - Find the Missing Numbers

This community-built FAQ covers the “Find the Missing Numbers” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Find the Missing Numbers

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Per instructions, ‘copy and paste to the forums’,

def missing_nos(arr, k):
  a = set(arr)
  b = set(range(1, len(a) + k + 1))
  return sorted([*(b.difference(a))])

print (missing_nos([2, 3, 4, 5, 6, 7, 8, 9], 2))

This also passed, but it was before I read up on the difference method.

def missing_nos(arr, k):
  a = set(arr)
  b = set(range(1, len(a) + k + 1))
  for i in a:
    if i in b:
      b.remove(i)
  return sorted([*b])

print (missing_nos([2, 3, 4, 5, 6, 7, 8, 9], 2))

This also passed, after considering other approaches…

def missing_nos(arr, k):
  return sorted([x for x in range(1, len(arr) + k + 1) if x not in arr])

print (missing_nos([2, 3, 4, 5, 6, 7, 8, 9], 2))

In the truest sense, this does not use sets so may allow duplicates. Using sets would be the way to go with this one. Easily solved,

[x for x in set(range(1, len(arr) + k + 1)) if x not in set(arr)]
1 Like

Completely forgot about the in keyword (whoops!) so did some fun things

def missing_nos(arr, k): # Write your code here true_size = len(arr) + k check_list = [i+1 for i in range(true_size)] arr += ([0] * k) result_list = [] for i in range(true_size): if arr[i] != check_list[i] and arr[i] != 0: arr.insert(i,0) arr.pop() result_list.append(check_list[i]) return (result_list) print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
def missing_nos(arr, k):
  full_set = {x + 1 for x in range( len(arr) + k)}
  result =  list ( full_set.difference(arr) )
  return sorted(result)

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

Seems to work fine now, but the thing that tripped me up was I tried sorting the original array (arr) at the start, assuming ‘result’ would dump out the comparison already in correct order… it doesn’t.

def missing_nos(arr, k): return [x for x in range(arr[0], arr[-1] + 1) if x not in arr] print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
def missing_nos(arr, k):
  num = 1
  missing = []
  for i in arr:
    if i == num:
      pass
    else:
      missing.append(num)
      num=num+1
    num+=1
  return(missing)
      


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

One of the easiest of the challenges.

def missing_nos(arr, k): missing = [] length = len(arr) + k for i in range(length): print(i + 1) if i + 1 not in arr: missing.append(i + 1) return missing print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
def missing_nos(arr, k): complete_set = set(range(1,max(arr)+1)) return sorted(list(complete_set - set(arr))) # Example 1: correctly returns [3,9] print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2)) # Example 2: incorrectly returns [3] print(missing_nos([1, 2, 4, 5, 6, 7, ], 2))

Looks like the test cases are incorrect/incomplete.
This solution is passing test cases. however the result for both should be [3,9] (with this implementation is [3] for second example)

You should get, [3, 8] is you remove the comma after the 7.

def missing_nos(arr, k): # Write your code here missings = [] no_missings = [i for i in range(1, arr[-1])] for num in no_missings: if num not in arr: missings.append(num) return missings

Did this pass the tests in the challenge? It shouldn’t have and so should not be posted here unless you have a question.

def missing_nos(arr, k): l = min(arr) h = max(arr) + 1 missing_num = [] for i in range(l, h): if i not in arr: missing_num.append(i) return missing_num print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))
def missing_nos(arr, k):
  given=set(arr)
  whole_set=set(range(1,len(given)+k+1))
  for i in given:
    if i in whole_set:
      whole_set.remove(i)
  return (sorted(whole_set))
print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))

def missing_nos(arr, k):

Write your code here

if arr == :
return [i+1 for i in range(k)]
j = 1
ans =
for i in range(len(arr)):
if arr[i] != j:
ans.append(j)
if len(ans) == k:
return ans
j = arr[i]
if i == len(arr) - 1:
while len(ans) < k:
j += 1
ans.append(j)
return ans
j += 1
print(missing_nos([2], 5))

def missing_nos(arr, k):
setList = set(arr)
newL = len(arr)
mainList = set(range(1, newL+k+1))
l = mainList.difference(setList)
return sorted(l)

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

def missing_nos(arr, k): # Write your code here ar=[] for i in range(len(arr)-1): if arr[i+1]!= arr[i]+1: ar.append(arr[i]+1) return ar print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))

You know, I just realized that it’s a python challenge, but they still let you pick which language your codebyte is in. :sweat_smile:

def missing_nos(arr, k): missing_list = [] for number in arr: count = 1 if len(missing_list) == k: break while number + count != arr[arr.index(number)+1]: missing_list.append(number+count) count+=1 return sorted(missing_list)

Easy peasy:

def missing_nos(arr, k): n = len(arr) + k klist = list(num for num in range(1, n+1) if not num in arr) return klist

Your code doesn’t work if the first number of the array was removed. Try inserting an if/else if the length of your returned array is != k

def missing_nos(arr, k): # Write your code here full_array = set(range(1, max(arr) + 1)) missing_nums = list(full_array - set(arr)) if len(missing_nums) != k: missing_nums.append(max(arr) + 1) return sorted(missing_nums) print(missing_nos([1, 2, 4, 5, 6, 7, 8, 10], 2))