Last active
September 20, 2015 12:39
-
-
Save sathish316/12131c42283705740116 to your computer and use it in GitHub Desktop.
Generate email ids for oneplus registration based on an amazon review (http://www.amazon.in/review/REA7WFX7PV3KO)
This file contains 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
object OnePlus { | |
def main(args: Array[String]) { | |
generate_aliases("typeyouremailhere@gmail.com").foreach(println) | |
} | |
def generate_aliases(email:String) = { | |
val email_pattern = """(\w+)@([\w\.]+)""".r | |
email match { | |
case email_pattern(address, domain) => { | |
interleave_all_with(address, '.').map {case address => s"$address@$domain"} | |
} | |
} | |
} | |
private def interleave_all_with(address:String, char:Char):Array[String] = interleave_all(address, char, address.length-1) | |
private def interleave_all(address:String, char:Char, times:Int):Array[String] = times match { | |
case 1 => interleave(address, char, 1, address.length, Array.empty[String]) | |
case n => { | |
val aliases = interleave_all(address, char, times-1) | |
val results = aliases ++ aliases.flatMap {case (alias:String) => interleave_with(alias, char)} | |
results.map(result => result.replaceAll("\\.\\.", ".")).filterNot(_.endsWith(".")).distinct | |
} | |
} | |
private def interleave_with(address:String, char: Char):Array[String] = interleave(address, char, 1, address.length, Array.empty[String]) | |
private def interleave(address:String, char: Char, from: Int, to: Int, results: Array[String]): Array[String] = if (from < to) | |
interleave(address, char, from + 1, to, results ++ Array(alias(address, char, from, to))) | |
else | |
results | |
private def alias(address:String, char:Char, from:Int, to:Int) = address.slice(0, from) + char + address.slice(from, to) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment