Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c9587f
commit e7523c5
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
class StringCalculator{ | |||
//Firma de método para las tareas 1,2 y 3 | |||
//int add(String numbers, String delimiter = ";"){ | |||
//Firma de método para la tarea 4 | |||
int add(String numbers){ | |||
//Código para tareas 1 y 2 | |||
//def arrayNumbers = numbers.split(',') | |||
//Código para tarea 3 | |||
//def arrayNumbers = numbers.split(',|\n') | |||
//Código para tarea 3 | |||
def delimitador = ',' | |||
if(numbers.startsWith("//") && numbers.charAt(3) == "\n"){ | |||
delimitador = numbers.charAt(2) | |||
}else if(numbers.startsWith("//") && numbers.charAt(2) == "["){ | |||
def cierre = numbers.indexOf("]") | |||
if(numbers.charAt(cierre + 1) == "\n") | |||
delimitador = numbers.substring(3,cierre) | |||
println "DELIMITADOR:" + delimitador | |||
} | |||
def arrayNumbers = numbers.split("^//|${delimitador}|\n|\[|\]") | |||
//Código para tarea 5 | |||
def negatives = arrayNumbers.findAll { Integer.valueOf(it ?: 0 ) < 0 } | |||
if(negatives) | |||
throw new Exception("negatives not allowed: ${negatives.join(',')}") | |||
//arrayNumbers.collect{ Integer.valueOf(it ?: 0 ) }.sum() | |||
// Código para tarea 6 | |||
arrayNumbers.collect{ | |||
def number = Integer.valueOf(it ?: 0 ) | |||
number < 1000 ? number : 0 | |||
}.sum() | |||
} | |||
} |
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,73 @@ | |||
import groovy.util.GroovyTestCase; | |||
|
|||
class StringCalculatorTest extends GroovyTestCase{ | |||
|
|||
StringCalculator stringCalculator = new StringCalculator() | |||
|
|||
void testAdd1(){ | |||
assert stringCalculator.add("") == 0 | |||
assert stringCalculator.add("0") == 0 | |||
assert stringCalculator.add("3") == 3 | |||
assert stringCalculator.add("4,5") == 9 | |||
assert stringCalculator.add("3,7") == 10 | |||
assert stringCalculator.add("0,70") == 70 | |||
assert stringCalculator.add("56,0") == 56 | |||
assert stringCalculator.add("56,45") == 101 | |||
} | |||
|
|||
void testAdd2(){ | |||
// Definimos una cantidad desconocida de números | |||
int n = (int)(Math.random() * 1000) | |||
def lista = [] | |||
def acumulador = 0 | |||
n.times{ | |||
// Generamos números al azar entre 0 y 100 | |||
int x = (int)(Math.random() * 100) | |||
lista << x | |||
// Mientras en el acumulador llevamos la cuenta del total de la suma | |||
acumulador += x | |||
} | |||
// Generamos el String de números | |||
String numbers = lista.join(",") | |||
// Corroboramos que el resultado del acumulador sea igual al de la ejecución del método | |||
assert stringCalculator.add(numbers) == acumulador | |||
} | |||
|
|||
void testAdd3(){ | |||
assert stringCalculator.add("1\n2,3") == 6 | |||
assert stringCalculator.add("1\n2,3\n4\n5,6\n7,8,9") == 45 | |||
} | |||
|
|||
void testAdd4(){ | |||
assert stringCalculator.add("//;\n1;2") == 3 | |||
assert stringCalculator.add("//!\n1!2!3!4!5!6!7!8!9") == 45 | |||
} | |||
|
|||
void testAdd5(){ | |||
shouldFail(Exception){ | |||
stringCalculator.add("//;\n1;-2") | |||
} | |||
shouldFail(Exception){ | |||
stringCalculator.add("//;\n-1;2") == 3 | |||
} | |||
shouldFail(Exception){ | |||
stringCalculator.add("//!1\n2!3!-4\n5!-6!7\n8!-9") == 45 | |||
} | |||
try{ | |||
stringCalculator.add("//!\n1!2!3!-4\n5!-6!7\n8!-9") == 45 | |||
}catch(Exception e){ | |||
println "\n" + e.message | |||
} | |||
} | |||
|
|||
void testAdd6(){ | |||
assert stringCalculator.add("//;\n1;2;3000;4000") == 3 | |||
assert stringCalculator.add("//!\n1001!1!2!3001!3!4001!4!5!6!7!8!9") == 45 | |||
} | |||
|
|||
void testAdd7(){ | |||
assert stringCalculator.add("//[!!!]\n1!!!2!!!3000!!!4000") == 3 | |||
assert stringCalculator.add("//[!!!]\n1001!!!1!!!2!!!3001!!!3!!!4001!!!4!!!5!!!6!!!7!!!8!!!9") == 45 | |||
} | |||
|
|||
} |