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

Sort by

recency

|

1021 Discussions

|

  • + 0 comments

    For pypy3

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

    n_2 = int(input())

    for i in range(n_2): string = input() try: if "remove" in string or "discard" in string: command, value = string.split() value = int(value) if command == "remove": s.remove(value) elif command == "discard": s.discard(value)

        else:
            r=list(s)
            s=set(r[1:])
    except KeyError:
        pass
    

    print(sum(s))

  • + 0 comments

    Use Python3 instead of pypy3 to get this correct

  • + 0 comments
    n = int(input())
    s = set(map(int, input().split()))
    cn = int(input())
    
    for i in range(cn):
        command, *number = input().split()
        
        if number:
            number = int(number[0])
            eval(f"s.{command}(number)")
        
        else:
            eval(f"s.{command}()")
    
    print(sum(s))
    
  • + 0 comments

    Here's my code:

    n, s = int(input()), set(map(int, input().split()))
    for i in range(int(input())):
        f, _, v = input().partition(' ')
        if not hasattr(set, f):
            continue
        getattr(s, f)(int(v)) if v else getattr(s, f)()
    print(sum(s))
    
  • + 0 comments
    n = int(input())
    s = set(map(int, input().split()))
    
    n_2 = int(input())
    
    for i in range(n_2):
        string = input()
        if "remove" in string or "discard" in string:
            command, value = string.split()
            value = int(value)
            if command == "remove":
                s.remove(value)
            elif command == "discard":
                s.discard(value)
    
        else:
            s.pop()
    print(sum(s))