Set .discard(), .remove() & .pop()

Sort by

recency

|

1066 Discussions

|

  • + 0 comments

    n = int(input()) s = set(map(int, input().split()))

    N = int(input())

    for _ in range(N): command = input() if command == 'pop': s.pop() if command.startswith('remove'): s.remove(int(command.split()[1])) if command.startswith('discard'): s.discard(int(command.split()[1]))

    print(sum(s))

  • + 0 comments

    n = int(input()) s = set(map(int, input().split())) N = int(input()) for i in range(N): a, *b = input().split() getattr(s, a)(*map(int,b)) print(sum(s))

  • + 0 comments

    This works in Python3

    n = int(input()) s = set(map(int, input().split())) N = int(input())

    for _ in range(N): command = input().split()

    if command[0] == 'pop':
        s.pop()
    elif command[0] == 'discard':
        s.discard(int(command[1]))
    else:
        s.remove(int(command[1]))
    

    print(sum(s))

  • + 0 comments

    for those wondering why their code doesn't work, is all because how pop() method is handled in both Python3 and PyPy3 (the default version of python in hackerrank). in Python3 it deletes the last index. in PyPy3, pop() method deletes the first number. all you gotta do i switch to python3 and it will work <3.

  • + 0 comments

    for those wondering why their code doesn't work, is all because how pop() method is handled in both Python3 and PyPy3 (the default version of python in hackerrank). in Python3 it deletes the last index. in PyPy3, pop() method deletes the first number. all you gotta do i switch to python3 and it will work <3.