Replace Letter
Given a string and a number (n), generate an array in which each position will be that same string but modifying the letter of its first position (0) by the one in its n position.
Input:
hello, 2
Output:
[lello, hollo, hehlo, heleo, helll]
Restrictions:
-
Where n will always be a number between 0 and the length of the string.
-
It is case-insensitive and does not discriminate between letters, numbers, and special characters.
-
When current position + n is greater than the length of the string, it will return to start.
-
The spaces will be considered:
-
If there is a space, that position will not be replaced.
-
The space will not replace any letter.
Keep in mind there may be more than one white space !!
-
For example
Given the string = "Hello world"
and n=1
Should return:
["eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH"]
It would not admit:
Hellowword
Hell word
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) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { 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) == ' ') {
- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- 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()]);
- }
- }
Contemplado el caso de que al final de la palabra haya un espacio
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) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { 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) {- public static String[] replaceLetter(String str, int n) {
ArrayList<String> arr = new ArrayList<>();StringBuilder strFinal = new StringBuilder(str);- 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) != ' ') {- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
if (str.charAt(nextIndex) != ' ') {- if (str.charAt(nextIndex) != ' ') {
strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- 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) == ' ') {- } else {
- while (str.charAt(nextIndex) == ' ') {
- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- nextIndex++;
- }
strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- }
}- 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()]);- }
- 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[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; String[] reverseLeterBye = { "eye", "bbe", "byy" }; 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)); } @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(100) @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) == ' ') { if (nextIndex == str.length() - 1) { nextIndex = 0; } else { 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[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- 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));
- }
- @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)- @RepeatedTest(100)
- @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);- 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) != ' ') {- for (int i = 0; i < str.length(); i++) {
- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ') {
if (str.charAt(nextIndex) != ' ') {- if (str.charAt(nextIndex) != ' ') {
strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- 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) == ' ') {- } else {
- while (str.charAt(nextIndex) == ' ') {
- if (nextIndex == str.length() - 1) {
- nextIndex = 0;
- } else {
- nextIndex++;
- }
strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));arr.add(strFinal.toString());strFinal.replace(0, str.length(), str);- }
}- 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()]);- }
- return arr.toArray(new String[arr.size()]);
- }
- }
Replace Letter
Given a string and a number (n), generate an array in which each position will be that same string but modifying the letter of its first position (0) by the one in its n position.
Input:
hello, 2
Output:
[lello, hollo, hehlo, heleo, helll]
Restrictions:
-
where n will always be a number between 0 and the length of the string.
-
It is case-insensitive and does not discriminate between letters, numbers, and special characters.
-
When current position + n is greater than the length of the string, it will return to start.
-
The spaces will be considered:
- If there is a space, that position will not be replaced.
- The space will not replace any letter.
For example
Given the string = "hello world"
and n=1
Should return:
[eello world, Hlllo world, Hello world, Heloo world, Hello oorld, Hello wrrld, Hello wolld, Hello wordd, Hello worlH]
It would not admit:
hellowword
hell word
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); } } 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) != ' ' && 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;- 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[] 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" }; String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2)); assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7)); 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) != ' ' && 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" };
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
- assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));
- assertArrayEquals(reverseLeterCodewars, Kata.replaceLetter("hello codewars", 7));
- 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));
- }
@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;
- }
- }
RandomTest añadido
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 = (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; 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";
- 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;
- }
- }
Parametro n añadido
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; 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) {- 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 + 1) % str.length();- int nextIndex = 0;
// check white space- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
nextIndex = (i + 1) % str.length();- 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; // 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)); } }
- 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"));- assertArrayEquals(reverseLeterHola, Kata.replaceLetter("hola",1));
- assertArrayEquals(reverseLeterAdios, Kata.replaceLetter("adios",1));
- }
- }
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
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 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"));
}
}