Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# author: Ryan Brigden
# The "brute" in brute force
# Question: Given a (large) list of words and a list of top-level domains (TLDs)
# from the Internet Assigned Numbers Authority (IANA), such as ".com" and ".net",
# find all of the possible "singleton" domains that can be registered with words
# from the word list. A singleton domain is defined as a sensical word (ie from
# the word list) whose suffix is a legitimate TLD (ie from the TLD list). You are
# given a function (is_available) that checks whether a given domain name is
# available for registration.
# Example:
# open(wordlist, "r").readlines() = ["surprisingly", "lifeboat", "google"]
# open(output, "r").readlines() = ["", ""]
# Model Solution:
WORDS = "path/to/words.txt"
TLDS = "path/to/tlds.txt"
OUT = "path/to/out.txt"
# make request to determine if the domain is available for registration
# (function provided)
def is_available(domain):
# ...
return availability
def normalize(s):
return s.strip().lower()
def main():
# open and init file descriptors
wf = open(WORDS, "r")
tf = open(TLDS, "r")
of = open(OUT, "w")
# ensure the lazy-loading of file data by line (word list can be very large)
for word in wf.xreadlines():
for tld in tf.xreadlines():
suffix = word[-(len(tld)):]
prefix = word[:-(len(tld))]
domain = "%s.%s" % (prefix, suffix)
if suffix == tld and is_available(domain):
of.write("%s\n" % domain)
# cleanup
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment