Complete the method wich accepts value, and return a String like following example:
-
If the value is positive, you display the result like this example:
- The value is 3, the result will be: "1-22-333"
- The value is 5, the result will be: "1-22-333-4444-55555"
-
If the value is negative, you display the result like this example:
- The value is -3, the result will be: "1+22+333"
- The value is -5, the result will be: "1+22+333+4444+55555"
class Solution {
public static String numberOfNumbers(int value) {
StringBuilder finalWord = new StringBuilder();
int j = 0;
String sign = value < 0 ? "+" : "-";
if (value == 0) return "";
if (value < 0) value *=-1;
for (int i = 1; i <= value; ++i) {
j = i;
while (j > 0) {
finalWord.append(i);
--j;
}
finalWord.append(sign);
}
return finalWord.deleteCharAt(finalWord.length() - 1).toString();
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void testThree() {
assertEquals("1-22-333", Solution.numberOfNumbers(3));
}
@Test
public void testOne() {
assertEquals("1", Solution.numberOfNumbers(1));
}
@Test
public void testNone() {
assertEquals("1", Solution.numberOfNumbers(-1));
}
@Test
public void testNFive() {
assertEquals("1+22+333+4444+55555", Solution.numberOfNumbers(-5));
}
@Test
public void testZero() {
assertEquals("", Solution.numberOfNumbers(0));
}
@Test
public void testFive() {
assertEquals("1-22-333-4444-55555", Solution.numberOfNumbers(5));
}
@Test
public void testTen() {
assertEquals("1-22-333-4444-55555-666666-7777777-88888888-999999999-10101010101010101010", Solution.numberOfNumbers(10));
}
}