I can't believe this question is still in the quiz | Sololearn: Learn to code for FREE!

+4

# I can't believe this question is still in the quiz

"What is the output of this code? x = {1:0, 0:1} y = [1, [4,6,5,4], 2] print(list(set(y[x[0]]))[1]) The answer is 5" What's going on here, you may ask. First of all, by the definition "A set object is an __unordered collection__ of distinct hashable objects. " Second, try changing the code a little bit: x = {1:0, 0:1} y = [1, [-1,6,5,-1], 2] print(list(set(y[x[0]]))[1]) What answer do you expect in this case? The "correct" answer is -1 Or may be you like this combination [-1,-3,0,-1]?

12/6/2021 3:27:58 AM

Alexey Kopyshev

+3

A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ This is in principle an incorrect question because there is no order in Set. The answer will depend on the hash implementation.

+2

A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Did you read properly my question? Second part? Without knowing the hash implementation, can you predict the behavior of x = {1:0, 0:1} y = [1, [-1,6,5,-1], 2] print(list(set(y[x[0]]))[1])? or may be: x = {1:0, 0:1} y = [1, [-1,0,-3,-1], 2] print(list(set(y[x[0]]))[1])?

+2

True, just because the set conversion gave the same order multiple times is not a guarantee that it always will. So we should not expect the result of the problem to always be the same. And yes, it is not a good problem because it gives the impression that sets are ordered. more discussion at SO: https://stackoverflow.com/questions/3812429/is-pythons-set-stable

+1

A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ As I said, it depends on the specific hash implementation. You can go to https://github.com/python/cpython/blob/main/Python/pyhash.c for details. A change in the implementation will not change the set properties, but it will change the answer to this quiz.

0

Alexey Kopyshev Yes there is no order that's why set([4, 6, 5, 4]) returns [6, 5, 4] so why this question is wrong. If there is order in set then it should return [4, 5, 6]. Is it not?

0

Alexey Kopyshev In this case [-1, -3, 0, -1] getting -3. I think it should be 0. There is little confusion. Can you explain why this behaviour?

0

EXEMPLE : S=0 for i in range (1,5): S+=i print (i, end=" + ") print ("=", S) #output # 1 + 2 + 3 + 4 +=10 I want to remove the last + on the output

-1

Alexey Kopyshev x = {1:0, 0:1} y = [1, [4, 6, 5, 4], 2] list(set(y[x[0]]))[1]) x[0] = 1 So list(set(y[1]))[1]) Now y[1] = [4, 6, 5, 4] So list(set([4, 6, 5, 4]))[1] As set doesn't allow duplicate so list(6, 5, 4)[1] = 5

-4

سلم