Skip to content

Instantly share code, notes, and snippets.

@iokiwi
Created December 28, 2017 07:15
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 iokiwi/b5bdb5b091de26843fe178dc451077bd to your computer and use it in GitHub Desktop.
Save iokiwi/b5bdb5b091de26843fe178dc451077bd to your computer and use it in GitHub Desktop.
from itertools import permutations
from functools import reduce
def as_email(first_name, last_name, domain_name, full=False):
if full:
return "{}.{}@{}".format(first_name, last_name, domain_name)
else:
return "{}@{}".format(first_name, domain_name)
def domain_name_permuations(domains, r=2, accumulate=False):
""" description
>>> domain_name_permuations(['a', 'b'], r=1, accumulate=True)
['a', 'b']
>>> domain_name_permuations(['a', 'b'], r=2, accumulate=False)
['ab', 'ba']
>>> domain_name_permuations(['a', 'b'], r=2, accumulate=True)
['a', 'b', 'ab', 'ba']
"""
if r >= 2 and accumulate:
perms = list(domain_name_permuations(domains, r-1, True)) + list(permutations(domains, r))
else:
perms = list(permutations(domains, r))
return ["".join(p) for p in perms]
def with_tld(domain, tld):
return "{}.{}".format(domain, tld)
def with_possible_tlds(domain,possible_tlds):
return [with_tld(domain, tld) for tld in possible_tlds]
def calculate_column_widths(domain_names, first_name, last_name):
longest_domain_name = max(domain_names, key=len)
longest_email_short = as_email(first_name, last_name, longest_domain_name)
longest_email_full = as_email(first_name, last_name, longest_domain_name, True)
return [len(longest_domain_name)+1, len(longest_email_short), len(longest_email_full)]
def main():
first_name = "simon"
last_name = "merrick"
possible_name_fragments = ["kiwi", "hax", "io", "bit", "cloud", "quantum", "infosec",
"stack", "dev", "simon", "merrick", "sec"]
possible_tlds = ["com", "net", "guru", "expert", "ninja", "nz", "web", "io",
"tech", "co","codes", "solutions", "codes"]
domain_names = domain_name_permuations(possible_name_fragments, r=2, accumulate=False)
domain_names = reduce(lambda x,y: x + y, [with_possible_tlds(domain_name, possible_tlds) for domain_name in domain_names])
column_widths = calculate_column_widths(domain_names, first_name, last_name)
column = " ".join(["{{:<{}}}".format(column_widths[i]) for i in range(len(column_widths))])
print(column.format("Domain Name", "Email", "Email (Longer)"))
for domain_name in domain_names:
email_short = as_email(first_name, last_name, domain_name)
email_full = as_email(first_name, last_name, domain_name, full=True)
print(column.format(domain_name, email_short, email_full))
if __name__ == "__main__":
import doctest
doctest.testmod()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment