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