+ 1

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.

23rd Sep 2021, 3:50 AM
Robert Haugland
Robert Haugland - avatar
13 ответов
+ 2
Rik 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.
23rd Sep 2021, 11:47 PM
Robert Haugland
Robert Haugland - avatar
+ 2
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
23rd Sep 2021, 11:50 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
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. 😁👍
24th Sep 2021, 9:46 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
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)))
24th Sep 2021, 10:23 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
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
24th Sep 2021, 1:03 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
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
23rd Sep 2021, 4:41 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
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.
23rd Sep 2021, 6:49 PM
Robert Haugland
Robert Haugland - avatar
0
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)
23rd Sep 2021, 9:44 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
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.
24th Sep 2021, 9:41 AM
Robert Haugland
Robert Haugland - avatar
0
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
24th Sep 2021, 9:47 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Rik Wittkopp, "Print(*(sorted(diff)))" worked! It passed all five tests and got celebratory, shooting confetti. Thanks an incredible ton!!
24th Sep 2021, 2:14 PM
Robert Haugland
Robert Haugland - avatar
0
Robert Haugland Awesome!! Well done for persevering
24th Sep 2021, 10:20 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
thanks, I was able to figure out what the problem wanted just by reading your code. I thought he was asking to guess what were the missing numbers of a sequence with a pattern to deduct and also deduct the amount of numbers to return. I thought it was an exaggerated difficulty so set up... I shortened your code 😉 N = [int(input()) for i in range(int(input()))] new = range(min(N), max(N)+1) diff = set(new) - set(N) print(*sorted(diff))
5th Aug 2023, 6:51 AM
Giordano Balanzo
Giordano Balanzo - avatar