I can't believe this question is still in the quiz | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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]?

6th Dec 2021, 3:27 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
9 Answers
+ 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.
6th Dec 2021, 6:15 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 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])?
6th Dec 2021, 8:03 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 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
8th Dec 2021, 12:00 AM
Bob_Li
Bob_Li - avatar
+ 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/JUMP_LINK__&&__python__&&__JUMP_LINK/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.
6th Dec 2021, 8:19 AM
Alexey Kopyshev
Alexey Kopyshev - avatar
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?
6th Dec 2021, 7:53 AM
A͢J
A͢J - avatar
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?
6th Dec 2021, 7:59 AM
A͢J
A͢J - avatar
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
8th Dec 2021, 12:48 AM
AYOUB OTAKU
- 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
6th Dec 2021, 4:55 AM
A͢J
A͢J - avatar
- 4
سلم
7th Dec 2021, 5:36 PM
بلال بلال