println("Hola, practiquemos con unos ejercicios") // 1 //////////////////////////////////////////////////////////////////////////////////////// //Necesitamos una función que imprima en una línea una secuencia separada por espacios como la siguiente: //La secuencia debe imprimir hasta el elemento definido por el parámetro de entrada "length". //El primer elemento es un parámetro de entrada opcional, debe ser 0 por defecto. //El segundo elemento es un parámetro de entrada opcional, debe ser 1 por defecto. //A partir del tercer elemento, el elemento se calcula como la suma de los 2 elementos anteriores. def printLucasSequence(def inSeqLen, def inFirstParam, def inSecondParam) { } // Input: 8 // 0 1 1 2 3 5 8 13 // Input: 4, 2, 3 // 2 3 5 8 // 2 //////////////////////////////////////////////////////////////////////////////////////// // Un parámetro de solicitud del servicio web del cliente debe tener 10 caracteres. // Debe contener un ID de cliente, pero este ID no siempre tiene 10 caracteres. // Por lo tanto, si es necesario, el ID debe ser completado con 0s a la izquierda. // O si el ID es demasiado largo, se deben tomar los últimos 10 caracteres. // Crear una función que tome el ID del cliente y devuelva la cadena de 10 caracteres formateada. // IN: ID del cliente como cadena. // OUT: Cadena con caracteres de longitud. def formatIdForRequest(def inId) { } // Input: 1234 // 0000001234 // 3 //////////////////////////////////////////////////////////////////////////////////////// // La respuesta de un cliente de webservice contiene una lista de IDs de sus clientes. // Los IDs válidos tienen el formato *****-NNNN con * una letra mayúscula y N un número // Crear una función que valide el formato de los IDs y devuelva un mapa con 2 elementos // IN: Lista con los IDs como cadenas. // OUT: Mapa con 2 elementos: // El 1er elemento clave es "válido" y su valor es una lista de IDs válidos. // El 2do elemento clave es "inválido" y su valor es una lista de IDs inválidos. def getValidInvalidMap(def inList) { } // println(getValidInvalidMap(testList)) // Input: def idList = ["ABC-1234", "D4F4", "ZXY-0000", "123-ABCD", "65ad", "testing", "RTE-9876"] // [valid:[ABC-1234, ZXY-0000, RTE-9876], invalid:[D4F4, 123-ABCD, 65ad, testing]] // 4 //////////////////////////////////////////////////////////////////////////////////////// // Crear un método que: // imprima los IDs válidos y sus deudas sólo si la deuda es mayor o igual a 0 como: // el id y "es nuestro propietario por" la cantidad de "millones de dólares". // si la deuda es menor que 0, imprime el id y " parece que es nuestro prestamista :/ ." // si no se encuentra un ID válido imprime el id y " ¡podría ser un cliente fantasma!" // Los IDs de las deudas vienen como un mapa de entrada // IN: Mapa de ids válidos e inválidos devueltos por el método anterior. // Mapa con los IDs como claves y las deudas como valores. def getDebtOfIds(def inIdMap, def inDebtMap) { } //getDebtOfIds(getValidInvalidMap(testList), debtMap) /* Input: def inDebtMap = ["ABC-1234": -45, "ZXY-0000": 45.3] ABC-1234 parece que es nuestro prestamista :/ . ZXY-0000 es nuestro propietario por 45,3 millones de dólares. ¡RTE-9876 ¡podría ser un cliente fantasma! */
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 |