Strings
//hacer que no funcione con una expresion regular con el caracter fin de cadena public class ConfusedDouble { public static String clearDouble(String str) { if (str == null || str.length() == 0 || str.matches("^[^.]*$")){ return ""; } double num = Double.parseDouble(str.replaceAll("[^0-9.]", "")); String result = Double.toString(num); return result; } }
- //hacer que no funcione con una expresion regular con el caracter fin de cadena
- public class ConfusedDouble {
- public static String clearDouble(String str) {
if (str == null || str.length() == 0){- if (str == null || str.length() == 0 || str.matches("^[^.]*$")){
- return "";
- }
- double num = Double.parseDouble(str.replaceAll("[^0-9.]", ""));
- String result = Double.toString(num);
- return result;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import org.junit.jupiter.api.Tag; class ConfusedDoubleTest { @Test void BasicTests() { assertEquals("4.3454", ConfusedDouble.clearDouble("dsa4da.3fsdf45fds4")); assertEquals("23.8822", ConfusedDouble.clearDouble("dfjhjed`´<23.882fhhjk2")); assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj")); assertEquals("", ConfusedDouble.clearDouble("95847352414153884")); assertEquals("", ConfusedDouble.clearDouble("")); assertEquals("", ConfusedDouble.clearDouble(null)); } @Test @Tag("RandomTests") void randomTest() { Random r = new Random(); String randomText = ""; for (int i = 0; i < 99; i++) { int numAscii = ThreadLocalRandom.current().nextInt(1, 255); randomText += (char)numAscii; } System.out.println(randomText); assertEquals(Kata.clearDouble(randomText), ConfusedDouble.clearDouble(randomText)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- import org.junit.jupiter.api.Tag;
- class ConfusedDoubleTest {
- @Test
- void BasicTests() {
// assertEquals("expected", "actual");- assertEquals("4.3454", ConfusedDouble.clearDouble("dsa4da.3fsdf45fds4"));
- assertEquals("23.8822", ConfusedDouble.clearDouble("dfjhjed`´<23.882fhhjk2"));
//assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj"));//assertEquals("", ConfusedDouble.clearDouble("95847352414153884"));- assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj"));
- assertEquals("", ConfusedDouble.clearDouble("95847352414153884"));
- assertEquals("", ConfusedDouble.clearDouble(""));
- assertEquals("", ConfusedDouble.clearDouble(null));
- }
- @Test
- @Tag("RandomTests")
- void randomTest() {
- Random r = new Random();
- String randomText = "";
- for (int i = 0; i < 99; i++) {
- int numAscii = ThreadLocalRandom.current().nextInt(1, 255);
- randomText += (char)numAscii;
- }
- System.out.println(randomText);
- assertEquals(Kata.clearDouble(randomText), ConfusedDouble.clearDouble(randomText));
- }
- }
Strings
Descripción de prueba
clearDouble("dsa4da.3fsdf45fds4") == 4.3454
ejemplo de prueba
- some tests will include very large strings.
- más ejemplos aquí.
//hacer que no funcione con una expresion regular con el caracter fin de cadena
public class Solution {
public static double clearDouble(String str) {
if (str == null || str.length() == 0) return 0;
return Double.parseDouble(str.replaceAll("[^0-9.]", ""));
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
@Test
void BasicTests() {
// añadir tests de prueba
// assertEquals("expected", "actual");
}
}