Strings
Arrays
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { nextIndex++; } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
int nextIndex = (i + n) % str.length();- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex++;
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
Arrays
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { nextIndex++; } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex++;
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" }; assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1)); String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); String[] reverseLeterBye = { "eye", "bbe", "byy" }; assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ') { if (str.charAt(nextIndex) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } else { while (str.charAt(nextIndex) == ' ') { nextIndex++; } strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };
- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
- if (str.charAt(nextIndex) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- } else {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex++;
- }
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- }
- return arr.toArray(new String[arr.size()]);
- }
- }
Strings
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String [] reverseLeterHello = {"lello", "hollo", "hehlo", "heleo", "helll"}; String [] reverseLeterBye = {"eye", "bbe", "byy"}; String [] reverseLeterCodewars = {"oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc"}; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7)); } @Test @Tag("UnitTest") void parseExceptionFecha() { assertThrows(NullPointerException.class, () -> { int nRandom = new Random().nextInt(15)+1; Kata.replaceLetter(null, nRandom); }); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?%$&/([] "; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length(); if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String [] reverseLeterHello = {"lello", "hollo", "hehlo", "heleo", "helll"};
- String [] reverseLeterBye = {"eye", "bbe", "byy"};
- String [] reverseLeterCodewars = {"oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc"};
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7));
- }
- @Test
- @Tag("UnitTest")
- void parseExceptionFecha() {
- assertThrows(NullPointerException.class, () -> {
- int nRandom = new Random().nextInt(15)+1;
- Kata.replaceLetter(null, nRandom);
- });
- }
- @Tag("RandomTest")
- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!?%$&/([] ";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
Strings
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { int nextIndex = (i + n) % str.length() ; if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } System.out.println(arr.toString()); String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- public class Kata{
- public static String[] replaceLetter(String str, int n) {
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length(); i++) {
int nextIndex = 0;- int nextIndex = (i + n) % str.length() ;
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- System.out.println(arr.toString());
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String [] reverseLeterHello = {"lello", "hollo", "hehlo", "heleo", "helll"}; String [] reverseLeterBye = {"eye", "bbe", "byy"}; String [] reverseLeterCodewars = {"oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc"}; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7)); } @Test @Tag("UnitTest") void parseExceptionFecha() { assertThrows(NullPointerException.class, () -> { int nRandom = new Random().nextInt(15)+1; Kata.replaceLetter(null, nRandom); }); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
String [] reverseLeterHola = {"oola", "hlla", "hoaa", "holh"};String [] reverseLeterAdios = {"ddios", "aiios", "adoos", "adiss", "adioa"};assertArrayEquals(reverseLeterHola, Kata.replaceLetter("hola",1));assertArrayEquals(reverseLeterAdios, Kata.replaceLetter("adios",1));- String [] reverseLeterHello = {"lello", "hollo", "hehlo", "heleo", "helll"};
- String [] reverseLeterBye = {"eye", "bbe", "byy"};
- String [] reverseLeterCodewars = {"oello codewars", "hdllo codewars", "heelo codewars", "helwo codewars", "hella codewars", "hello sodewars", "hello chdewars", "hello coeewars", "hello codlwars", "hello codelars", "hello codewors", "hello codewarc"};
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7));
- }
- @Test
- @Tag("UnitTest")
- void parseExceptionFecha() {
- assertThrows(NullPointerException.class, () -> {
- int nRandom = new Random().nextInt(15)+1;
- Kata.replaceLetter(null, nRandom);
- });
- }
- }
Strings
Controlar el indice del array. SmellCode
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; class Kata { public static String[] replaceLetter(String str) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != ' ') { //comprobar si i + 1 es un espacio int nextIndex = (i + 1) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } // Convertimos ArrayList a Array String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- class Kata {
- public static String[] replaceLetter(String str) {
// Hola -> oola, Hlla, Hoaa, HolH// Prueba añadir descripción- ArrayList<String> arr = new ArrayList<>();
StringBuilder strFinal = new StringBuilder(str);- StringBuilder strFinal = new StringBuilder(str);
for (int i = 0; i < str.length() - 1; i++) {- for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {- if (str.charAt(i) != ' ') {
- //comprobar si i + 1 es un espacio
- int nextIndex = (i + 1) % str.length();
strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);}- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
strFinal.setCharAt(str.length() - 1, str.charAt(0));arr.add(strFinal.toString());System.out.println(arr.toArray().toString());// Convertimos ArrayList a ArrayString[] array = arr.toArray(new String[arr.size()]);- }
- // Convertimos ArrayList a Array
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
return array;- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String [] reverseLeterHola = {"oola", "hlla", "hoaa", "holh"}; String [] reverseLeterAdios = {"ddios", "aiios", "adoos", "adiss", "adioa"}; assertArrayEquals(reverseLeterHola, Kata.replaceLetter("hola")); assertArrayEquals(reverseLeterAdios, Kata.replaceLetter("adios")); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Test- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String [] reverseLeterHola = {"oola", "hlla", "hoaa", "holh"};
- String [] reverseLeterAdios = {"ddios", "aiios", "adoos", "adiss", "adioa"};
- assertArrayEquals(reverseLeterHola, Kata.replaceLetter("hola"));
- assertArrayEquals(reverseLeterAdios, Kata.replaceLetter("adios"));
- }
- }
Strings
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; class Kata { public static String[] replaceLetter(String str) { // Hola -> oola, Hlla, Hoaa, HolH // Prueba añadir descripción ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length() - 1; i++) { if (str.charAt(i) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } strFinal.setCharAt(str.length() - 1, str.charAt(0)); arr.add(strFinal.toString()); System.out.println(arr.toArray().toString()); // Convertimos ArrayList a Array String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- class Kata {
- public static String[] replaceLetter(String str) {
- // Hola -> oola, Hlla, Hoaa, HolH
- // Prueba añadir descripción
//Prueba- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length() - 1; i++) {
- if (str.charAt(i) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- strFinal.setCharAt(str.length() - 1, str.charAt(0));
- arr.add(strFinal.toString());
- System.out.println(arr.toArray().toString());
- // Convertimos ArrayList a Array
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
Strings
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; class Kata { public static String[] replaceLetter(String str) { // Hola -> oola, Hlla, Hoaa, HolH // Prueba añadir descripción //Prueba ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length() - 1; i++) { if (str.charAt(i) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } strFinal.setCharAt(str.length() - 1, str.charAt(0)); arr.add(strFinal.toString()); System.out.println(arr.toArray().toString()); // Convertimos ArrayList a Array String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- class Kata {
- public static String[] replaceLetter(String str) {
- // Hola -> oola, Hlla, Hoaa, HolH
- // Prueba añadir descripción
- //Prueba
- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length() - 1; i++) {
- if (str.charAt(i) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- strFinal.setCharAt(str.length() - 1, str.charAt(0));
- arr.add(strFinal.toString());
- System.out.println(arr.toArray().toString());
- // Convertimos ArrayList a Array
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }