Bhaskar Gupta
Bhaskar Gupta

Reputation: 146

'set' object does not support indexing

An array A of length N is said to be pseudo-sorted if it can be made non-decreasing after performing the following operation at most once.

for i in range(int(input())):
    N = int(input())
    A = list(map(int,input().split()))
    C = set(A)
    for _ in range(len(A)):
        if A[_]==C[_]:
            print("YES")
        else:
            print("NO")

Input:

3
5
3 5 7 8 9
4
1 3 2 3
3
3 2 1

Output Should be:

YES
YES
NO

Real Output:

Traceback (most recent call last):
File "./prog.py", line 7, in <module>
TypeError: 'set' object does not support indexing

Upvotes: 0

Views: 479

Answers (2)

Darkstar
Darkstar

Reputation: 21

As the error says sets do not support indexing so you can convert C into a list as @farch said.

Though this will result in an error if the list is larger than the set so you might wanna loop over it using

for i, j in zip(A, C):

Upvotes: 0

farch
farch

Reputation: 492

Set data structure has following operations,

  • union
  • intersection
  • difference
  • substract

If you need to access your set you have to transform it to a list, like this

list(set(A))

Upvotes: 2

Related Questions