Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print a number not in a list python

I’m having an issue with a simple task it seems, but cannot figure it out. I believe the solution is within the code:

n = input().split(',')

list1 = []
list2 = []
for x in n:
    list1.append(int(x))

for y in range(1, len(list1 + 1)):
    if y not in list1:
        list2.append(y)
print(list2)

The task is:
Given an array of integers, some elements appear twice and others appear once.
Each integer is in the range of [1, N], where N is the number of elements in the array.

Find all the integers of [1, N] inclusive that do NOT appear in this array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Constrain:
N will always be in the range of [5, 1000]

Input:
1,2,3,3,5
Output:
4
Input:
1,1,1,1,1,1,1,1
Output:
2,3,4,5,6,7,8

My idea is to have two empty arrays. The first one I will write all the numbers using a for loop. Once I have them all there I will use another loop where I can look and find the missing numbers. I guess it related to some formatting that is killing my logic.
Any help would be appreciated!
Thanks

>Solution :

You only have to change this line:

for y in range(1, len(list1 + 1)):

to this one:

for y in range(1, len(list1)+1):

The problem is that you added one to the list, but you want to add 1 to the length of the list.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading