curriculum/challenges/english/blocks/daily-coding-challenges-python/68cae5b538ff798bbd4da005.md
Given a string, determine if it is a valid email address using the following constraints:
@ symbol.@):
a-z, A-Z), digits (0-9), dots (.), underscores (_), or hyphens (-).@):
validate("[email protected]") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), True)`)
}})
validate("[email protected]") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), True)`)
}})
validate("[email protected]") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), False)`)
}})
validate("[email protected]") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), False)`)
}})
validate("freecodecamp.org") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("freecodecamp.org"), False)`)
}})
validate("develop.ment_user@c0D!NG.R.CKS") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("develop.ment_user@c0D!NG.R.CKS"), True)`)
}})
validate("[email protected]") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), False)`)
}})
validate("[email protected]") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("[email protected]"), False)`)
}})
validate("develop..ment_user@c0D!NG.R.CKS") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("develop..ment_user@c0D!NG.R.CKS"), False)`)
}})
validate("git@[email protected]") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(validate("git@[email protected]"), False)`)
}})
def validate(email):
return email
import re
def validate(email):
if '..' in email:
return False
parts = email.split('@')
if len(parts) != 2:
return False
local, domain = parts
if local.startswith('.') or local.endswith('.'):
return False
if not re.match(r'^[a-zA-Z0-9._-]+$', local):
return False
if '.' not in domain:
return False
tld = domain.split('.')[-1]
if len(tld) < 2 or not tld.isalpha():
return False
return True