# Missing Numbers

Trying to solve the Code Coach problem where you are given a number repesenting the length of a list of numbers and another input giving rhe actual list of numers. The numbers are sorted in ascending order and you have to figure out the missing numbers in the range and print the missing numbers with a space seperating them. The example they gave goes like this: Sample input: 5 2 4 5 7 8 Sample output: 3 6 So, below, I used their sample input to test my code and it resulted in a list of the missing numbers seperated by a space. When I ran it dor the answer (minus my the sample inputs), I get an error message saying: num = 5 numarr = 2,4,5,7,8 numarr = list(numarr) def range1(start, end): return range(start, end+1) new = range1(min(numarr), max(numarr)) numarr1 = set(numarr) diff = ({*new} - numarr1) diff = list(diff) diff1 = str(diff).replace( ',' , ' ' ) print(diff1) Got an error message "line 3, numarr = list(numarr), TypeError: 'int' object not iterablle. I think it is because of their list input.

9/23/2021 3:50:31 AM

Robert Haugland12 Answers

New AnswerRik Wittkopp, Thanks for the cleaner code change. It produced the missing numbers with a space between them, as needed. The only problem is that it comes back as a set type and they want it to be a string.

Robert Haugland try print(str(*diff)) Not sure if it will work as I haven't checked. Let me know if it does not work ^ I will find time to investigate further

Thanks Robert Haugland It is hard to provide exact information without access to the challenge details. It sounds like a complex challenge. The good thing is that our mistakes provide a learning path we can use to improve. Let me know how you go. 😁👍

Robert Haugland I am intrigued by this challenge of yours, so forgive me. Does this work? num = int(input()) N = [] for i in range(num): N.append(int(input())) def range1(start, end): return range(start, end +1) new = range1(min(N), max(N)) diff = set(new) - set(N) print(*(sorted(diff)))

Hi Robert Haugland The first num sets the length of the list. num = int(input()) lst = [] # empty list for i in range(num): # now you have the ability to vary the length of your list. lst.append(int(input()) # appending input numbers to lst Then you can do your magic to find the missing numbers

Rik Wittkopp, Thanks for the tip. Now when I run the code for the answer, it displays the correct numbers, but as a list(with[]). I get the same result on the Python program in my PC. Apparently yhey want a the answer.displayed as a string. When i print the item type it says 'class string'. This is what I wrote: num = int(input()) N = [] for i in range(num): N.append(int(input())) def range1(start, end): return range(start, end +1) new = range1(min(N), max(N)) N1 = set(N) diff = ({*new} - N1) diff = list(diff) diff1 = str(diff).replace(",", " ") diff2 = [] for i in diff1: diff2.append(str(i)) diff2 = "".join(diff2) print(diff2) Not sure what else thwy are wanting me to do as I am not gettinh any green lights whem I run the code.

Hi Robert Haugland Things started to get a bit strange after your def statement. The output list you were creating had spaces within it, which the computer will see as an extra character. I liked your attempt at using sets to identify the differences, so much more efficient than loops. I took the liberty to adapt your code to something I think will work num = int(input()) N = [] for i in range(num): N.append(int(input())) def range1(start, end): return range(start, end +1) new = range1(min(N), max(N)) diff = set(new) - set(N) print(*diff)

Robert Haugland My last suggestion will not work, so change print(*diff)) to print(*[str(i) for i in diff]) this will output string items which have been unpacked from a list

Rik Wittkopp, When you sent the earlier fix and didn't work, I immediately tried print(str(*diff)) and it just gave an error. I just tried your latest suggestion and it did not work. The program is wanting the answer to be sorted. So, I tried your latest suggestion with "sorted" added: print(*sorted([str(i) for i in diff])) This green lighted 4 of the five tests where three of them were locked. The one test it did not pass was where my output was: 8 11 7 and the expected output was: 7 8 11. I am going to look at the actual input for that test to see if I can play with it on my PC to get the desired output for all 5 tests. I absolutely appreciate all the help you have provided, thus far. Extremely informative and helpful.

Robert Haugland PS: I would suggest you sort your input list before comparing with your range. N = sorted(N) put this in after you have created your full list

Rik Wittkopp, "Print(*(sorted(diff)))" worked! It passed all five tests and got celebratory, shooting confetti. Thanks an incredible ton!!