Source
CheckIO / Elementary / Python /
Fizz Buzz
Description
Given a positive integer n
, return:
- “Fizz” if
n
is divisible by 3
- “Buzz” if
n
is divisible by 5
- “FizzBuzz” if
n
is divisible by 3 and by 5
- A string representation of the number in any other case
1
2
3
4
| 6 ⟹ "Fizz"
5 ⟹ "Buzz"
15 ⟹ "FizzBuzz"
7 ⟹ "7"
|
My solution
1
2
3
4
5
6
7
8
9
10
11
12
| public class fizzbuzzSingle {
public static String fizzbuzz(int n) {
String nstr = String.valueOf(n);
String result = "";
if (n < 0) return nstr;
if (n % 3 == 0) result = "Fizz";
if (n % 5 == 0) result += "Buzz";
return result.length() > 0 ? result : nstr;
}
}
|
Tests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| import org.junit.*;
import static org.junit.Assert.*;
public class fizzbuzzSingleTest {
fizzbuzzSingle x = new fizzbuzzSingle();
@Test
public void testNegative() {
assertEquals(x.fizzbuzz(-1), "-1");
}
@Test
public void testFizz() {
assertEquals(x.fizzbuzz(6), "Fizz");
}
@Test
public void testBuzz() {
assertEquals(x.fizzbuzz(5), "Buzz");
}
@Test
public void testFizzBuzz() {
assertEquals(x.fizzbuzz(15), "FizzBuzz");
}
}
|