import java.text.Normalizer /******************************************* * Build possible email addresses from name *******************************************/ // Temp user object class User{ // Accented characters are removed swapped for base glyphs // Non alpha numeric characters are removed. // Numbers are ignored when building initials // Both First and middle names can have multiple words separated by space or hyphen String first_name = "Henry" String middle_name = "the Vacuum Cleaner" // Last name can have multiple words separated by space or hyphen and accepts apostrophes String last_name = "o'Hoover!" //String maiden_name = "Dyson" // Not yet implemented } /** * getEmailOptions() * * Take a user object and work out all of the possible email options available for a user to choose from. * * @param User u * @return List<String> */ List<String> getEmailOptions(User u) { String first_name, middle_name, last_name, maiden_name // Get First Name from User Object try { first_name = normalizeInput(u.first_name.toLowerCase()) } catch (Exception ex) { first_name = "" } // Get Middle Names from User Object try { middle_name = normalizeInput(u.middle_name.toLowerCase()) } catch (Exception ex) { middle_name = "" } // Get Last Name from User Object try { last_name = normalizeInput(u.last_name.toLowerCase()) } catch (Exception ex) { last_name = "" } // Get Maiden Name from User Object //try { // maiden_name = normalizeInput(u.maiden_name.toLowerCase()) //} //catch (Exception ex) { // maiden_name = "" //} List<String> initials = getInitials(first_name, middle_name) List<String> last_names = getLastNames(last_name) List<String> email_options = [] for(String sInitials : initials){ for(String sLastName: last_names ) { email_options.add(sInitials + '.' + sLastName) } } return email_options } /** * getInitials() * * Take first and middle names and get all combinations of initials * e.g. first_name: Joe * middle_name: George * ouput: [ * 'j', * 'j.g' * ] * * @param String first_name * @param String middle_name * @return List<String> */ List<String> getInitials(String first_name, String middle_name = "") { // Get initials // Join First and Middle Names String first_and_middle_names = [first_name, middle_name].join("-") String first_and_middle_initials = "" // Loop through each word in first_and_middle_names for (String s : first_and_middle_names.split("-")) { for(char c : s.toCharArray()) { if (c.isLetter()) { first_and_middle_initials+=c break } } } // Get different possible combinations of initials List<String> initials = [] for (int i = 1; i <= first_and_middle_initials.length(); i++){ String inits = first_and_middle_initials.substring(0,i).toCharArray().join(".") initials.add(inits) } return initials } /** * getLastNames * * Sanatise last_name and build array of possible combinations * @param last_name * @return List<String> */ List<String> getLastNames(String last_name) { // Create list and add original last_name to list List<String> last_names = [last_name] // Remove apostrophes if (last_name.contains("'")) last_names.add(last_name.replaceAll("'", "")) // Remove hyphens if (last_name.contains("-")) last_names.add(last_name.replaceAll("-", "")) // Remove both apostrophes and hypthens if (last_name.contains("'") && last_name.contains("-")) last_names.add(last_name.replaceAll("'", "").replaceAll("-", "")) return last_names } /** * normalizeInput() * * Take input and normalize it * * @param String input * @return String */ String normalizeInput(String input) { input = input.trim() // Remove leading and trailing spaces input = input.replaceAll(" +", " ") // Replace groups of multiple spaces with single spaces input = stripAccents(input) // Replace accented characters with base glyph input = input.replaceAll("[^a-zA-Z0-9 '-]", "") // Remove all none alpha-numberic characters input = input.replaceAll(" ", "-") // Replace spaces with hyphens return input } /** * stripAccents() * * Strip accents using Java Normalizer * * @param String input * @Return String */ String stripAccents(String input){ return input == null ? null : Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); } User user = new User() for(String email : getEmailOptions(user)) { println email }
Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.
OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.
def name = System.in.newReader().readLine()
println "Hello " + name
Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.
Data type | Description | Range |
---|---|---|
String | To represent text literals | NA |
char | To represent single character literal | NA |
int | To represent whole numbers | -2,147,483,648 to 2,147,483,647 |
short | To represent short numbers | -32,768 to 32,767 |
long | To represent long numbers | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 |
double | To represent 64 bit floating point numbers | 4.94065645841246544e-324d to 1.79769313486231570e+308d |
float | To represent 32 bit floating point numbers | 1.40129846432481707e-45 to 3.40282346638528860e+38 |
byte | To represent byte value | -128 to 127 |
boolean | To represent boolean values either true or false | True or False |
You can define variables in two ways
data-type variable-name;
[or]
def variable-name;
0.upto(n) {println "$it"}
or
n.times{println "$it"}
where n is the number of loops and 0 specifies the starting index
When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
List allows you to store ordered collection of data values.
def mylist = [1,2,3,4,5];
List Methods | Description |
---|---|
size() | To find size of elements |
sort() | To sort the elements |
add() | To append new value at the end |
contains() | Returns true if this List contains requested value. |
get() | Returns the element of the list at the definite position |
pop() | To remove the last item from the List |
isEmpty() | Returns true if List contains no elements |
minus() | This allows you to exclude few specified elements from the elements of the original |
plus() | This allows you to add few specified elements to the elements of the original |
remove() | To remove the element present at the specific position |
reverse() | To reverse the elements of the original List and creates new list |