HackerRank Python - Validating Email Addresses With a Filter


You are given an integer N followed by N email addresses. Your task is to print a list containing only valid email addresses in lexicographical order.

  1. def fun(email):
  2. try:
  3. username, url = email.split('@')
  4. website, extension = url.split('.')
  5. except ValueError:
  6. return False
  7. if username.replace('-', '').replace('_', '').isalnum() is False:
  8. return False
  9. elif website.isalnum() is False:
  10. return False
  11. elif len(extension) > 3:
  12. return False
  13. else:
  14. return True
  15. def filter_mail(emails):
  16. return list(filter(fun, emails))
  17. if __name__ == '__main__':
  18. n = int(input())
  19. emails = []
  20. for _ in range(n):
  21. emails.append(input())
  22. filtered_emails = filter_mail(emails)
  23. filtered_emails.sort()
  24. print(filtered_emails)

download android app