HackerRank Python- set-discard-remove-pop



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

You have a non-empty set s, and you have to execute N commands given in N lines.

The commands will be pop, remove and discard.



  1. if __name__ == "__main__":
  2. n = int(input())
  3. s = set(map(int, input().split()))
  4. num_cmds = int(input())
  5. for _ in range(num_cmds):
  6. cmd = list(input().strip().split(' '))
  7. if cmd[0] == 'pop':
  8. s.pop()
  9. elif cmd[0] == 'remove':
  10. s.remove(int(cmd[1]))
  11. elif cmd[0] == 'discard':
  12. s.discard(int(cmd[1]))
  13. print(sum(s))

download android app