Missing numbers (code coach) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Missing numbers (code coach)

Task: Create a program that takes in a list of numbers and outputs the missing numbers in the sequence separated by spaces. Input Format: The first input denotes the length of the list (N). The next N lines contain the list elements as integers. Output Format: A string containing a space-separated list of the missing numbers. Sample Input: 5 2 4 5 7 8 Sample Output: 3 6 So I know how to solve it in other way without sets, but why is the output is in a different order after using set(l)? And how can I sort sets in this case? For example I have 8 11 7 instead of 7 8 11 in output https://code.sololearn.com/cWgzP95BB8A4/?ref=app

14th Aug 2023, 7:37 PM
N/A
4 Answers
+ 6
Sets are unordered and unindexed. Their advantage is that they filter out duplicates and that checking for existence in them is more efficient than in a list. There is a class called OrderedSet for Python that would perform ordering for you, but it's in a module that you'd need to install.
14th Aug 2023, 8:13 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 4
N/A , i did a try with your code, but it seems to get not all missing numbers. so i did like: > the code uses an input list of numbers. this will be converted to a set. > we also need a *reference* range for all numbers from min(l) upto max(l). > the set method *difference()* is used to get all numbers that are in the reference but not in the input data. these are the missing numbers. the result will be sorted. > for the output the members of the list has to be strings, so a list comprehension is used. > the resulting list is finally joined and output. l = [5, 2, 4, 5, 7, 8, 11] # 3 6 9 10 expected result l = set(l) x = set(range(min(l), max(l)+1)) print(' '.join([str(i) for i in sorted(x.difference(l))])) basic code and code above: https://code.sololearn.com/clR60PCzmzVg/?ref=app
15th Aug 2023, 7:19 PM
Lothar
Lothar - avatar
+ 2
N/A To the best of my recollection, sets in Java and another languages also do the same thing cause set is an unordered data structures. This means that orders are not important. Ordered set(or LinkedHashSet in Java) solves the order problem, but it is likely to work less efficient than the unordered ones. You can read more articles talking about it to fully understand the reason why it changes order.. Here is what I found in the internet: https://stackoverflow.com/questions/9792664/converting-a-list-to-a-set-changes-element-order https://www.udacity.com/blog/2021/11/JUMP_LINK__&&__python__&&__JUMP_LINK-ordered-sets-an-overview.html#:~:text=This%20is%20because%20the%20elements,order%20in%20which%20they%20appear. https://math.stackexchange.com/questions/2883526/if-the-order-in-a-set-doesn-t-matter-can-we-change-order-of-say-bbbn I also saw this video while looking for the articles: https://youtu.be/fuyWqnWT5IM
16th Aug 2023, 9:08 AM
Dragon RB
Dragon RB - avatar
+ 1
Your code works, yes. But I think my main question was "WHY set function changes order?" Input values are given sorted from the beginning, but set function changes their order
15th Aug 2023, 11:44 PM
N/A