Created
January 6, 2017 23:27
-
-
Save rbrigden/9c63e2d814c8d5d976425b88749efc30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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