When such a tweet came to TL, I felt the atmosphere of Zundokokiyoshi, so I implemented it in Java.
Musume I thought about the game with 5 years old. The title is "Sunshine Ikezaki Game". The rules are simple. If you sing a song and get "i", just change it to "yes!". Musume 5 years old "Big old-fashioned Yeah !, Uncle Yeah! San's Toke Yeah !!" I laughed. https://twitter.com/ziuziu/status/918070729341587457
As a specification -If there is "i" or "i" in the entered character string, replace it with "yes". ・ If "i" and "i" are consecutive, replace the second and subsequent times with "-".
Click here for its implementation Maybe it was because I made it at the end of work. It's a pretty dirty code, but ... (~~ This is your ability ~~)
import java.util.Arrays;
public class Yeah {
/**
*If "i" or "i" matches the entered character string, replace it with "yes".
*However, if it is continuous, replace it with "-" from the second time onward.
*/
public String convert(String text) {
boolean alwaysHit = false;
String[] hit = {"I", "I"};
String[] strArray = toArray(text);
for (int i = 0; i <= strArray.length - 1; i++) {
if (Arrays.asList(hit).contains(strArray[i])){
if (alwaysHit == false) {
strArray[i] = "Yeah";
alwaysHit = true;
} else {
strArray[i] = "-";
}
}else{
alwaysHit = false;
}
}
return Arrays.toString(strArray).replace(" ", "").replace(",", "").replace("[", "").replace("]", "");
}
private String[] toArray(String str) {
String[] returnStr = new String[str.length()];
//Turn by the length of the variable str
for (int i = 0; i <= str.length() - 1 ; i++) {
//Store in sequence in an array
returnStr[i] = String.valueOf(str.charAt(i));
}
return returnStr;
}
}
Write the following test code and execute the above code
import static org.junit.Assert.*;
import org.junit.Test;
public class YeahTest {
@Test
public void testConverter() {
Yeah yeah = new Yeah();
assertEquals("Yeah yeah yeah yeah yeah", yeah.convert("Good good good yeah yeah yeah good"));
assertEquals("Yeah yeah yeah yeah yeah yeah yeah yeah yeah", yeah.convert("Yeah yeah yeah yeah yeah yeah"));
assertEquals("Singing Yen Finity", yeah.convert("Singing Infinity"));
}
}
I've done what I want to do, so now ... I'm going to sleep. (˘ω˘) Sue
Recommended Posts