Part of the job passes tests, but the last test gives a not fully sorted list. Task from the site: https://pythontutor.ru/lessons/sets/problems/sets_intersection/

a=set(input().split()) b=set(input().split()) c=(set(a.intersection(b))) #print(a) #print(b) #print(c) c=list(c) c=sorted(c) #c.sort() print(" ".join(c)) 
  • 2
    What does " not fully sorted list" mean? Give an example. - RiotBr3aker
  • Sets 1 and 82 54 91 100 70 33 88 14 52 48 56 20 63 16 22 23 30 8 84 75 45 95 51 98 4 86 78 24 5 77 76 18 97 10 17 66 2 43 53 53 21 69 19 39 7 11 72 40 79 57 68 96 80 71 67 13 99 83 35 27 28 73 36 6 25 55 Set2: 10 44 77 90 43 75 25 24 5 21 71 70 83 68 18 92 81 57 27 67 48 6 87 36 64 49 19 72 62 29 22 82 7 17 1 73 54 30 9 66 61 95 55 28 86 39 3 42 74 60 93 2 52 78 34 51 32 94 11 37 26 23 69 58 35 14 84 Displays a non-sorted list: 10 11 14 17 18 19 2 21 22 23 24 25 27 28 30 35 36 39 43 48 5 51 52 54 55 57 6 61 66 67 68 69 7 70 71 73 75 77 78 82 83 84 86 95 - Alex Sapsay

1 answer 1

The problem is that input.split () returns a list of strings, not numbers. Then you compare these lines in the hope of getting a sort of numbers, and this is not the same thing.

This can be seen on the site you provided with step-by-step execution: enter image description here


It is enough when parsing the entered string to bring each substring to a number, in this case, apparently, to an int:

 a=set([int(x) for x in input().split()]) b=set([int(x) for x in input().split()]) 

In the end, we get the following code:

 a=set([int(x) for x in input().split()]) b=set([int(x) for x in input().split()]) c=(set(a.intersection(b))) c=list(c) c.sort() print(*c) 

UPD

Why did the original code work correctly on some test data?

It's simple, in the first two tests the intersection of two lists did not contain a number greater than 9, i.e. the substrings consisted of one element, the comparison of such strings occurs by their numerical representation, be it ASCII or any other encoding. Comparing strings with 2+ characters is a completely different story, they are compared element by element + and the result of this comparison is affected by the size of the string.

What is * s?

In this case, this operation unpacks the list into separate elements, which are eventually substituted into print() as positional arguments.

  • Thanks for the detailed response and detailed explanation! There was still a question why the first two tests passed the correct answer. After all, is there also sorting of the list of strings? And what does print (* c) mean, it’s not clear * what does this operation do? - Alex Sapsay
  • one
    @AlexSapsay, added the answer. - RiotBr3aker
  • Thanks, now I understand why 2 tests passed. But * s- all sounds like magic. Is it possible to replace * s with the entry print ('' .join ([str (i) for i in c])). Yes, it turns out you can. Your recording is much smaller. But so far this is not an attainable understanding of Kung Fu for me - Alex Sapsay
  • one
    @AlexSapsay, but actually unpacking tuples / lists is a useful and simple thing, google "tutple unpacking" :) - RiotBr3aker
  • Ok, I'll try! It looks of course very cool - Alex Sapsay