Skip to content

Instantly share code, notes, and snippets.

@rbrigden
Created January 6, 2017 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbrigden/9c63e2d814c8d5d976425b88749efc30 to your computer and use it in GitHub Desktop.
Save rbrigden/9c63e2d814c8d5d976425b88749efc30 to your computer and use it in GitHub Desktop.
#!/bin/python
# 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() = ["surprising.ly", "lifebo.at"]
# 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
fw.close()
ft.close()
fo.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment