Problem:
This is slightly more difficult version of the famous FizzBuzz problem which is sometimes given as a first problem for job interviews. (See also: FizzBuzz Code.) Consider the series of numbers beginning at start and running up to but not including end, so for example start=1 and end=5 gives the series 1, 2, 3, 4. Return a new String[] array containing the string form of these numbers, except for multiples of 3, use "Fizz" instead of the number, for multiples of 5 use "Buzz", and for multiples of both 3 and 5 use "FizzBuzz". In Java, String.valueOf(xxx) will make the String form of an int or other type. This version is a little more complicated than the usual version since you have to allocate and index into an array instead of just printing, and we vary the start/end instead of just always doing 1..100.
fizzBuzz(1, 6) → {"1", "2", "Fizz", "4", "Buzz"}
fizzBuzz(1, 8) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7"}
fizzBuzz(1, 11) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"}
Solution:
public String[] fizzBuzz(int start, int end) { int n = end - start; String[] result = new String[n]; int pos = 0; for (int i = start; i < end; i++) { boolean fizz = i % 3 == 0; boolean buzz = i % 5 == 0; if (fizz && buzz) result[pos] = "FizzBuzz"; else if (fizz) result[pos] = "Fizz"; else if (buzz) result[pos] = "Buzz"; else result[pos] = String.valueOf(i); pos++; } return result; }
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] result = new String[end - start];
for (int i = start; i < end; i++){
if(i % 3 != 0 && i % 5 != 0){
result[i - start] = String.valueOf(i);
}
if(i % 3 == 0){
result[i - start] = "Fizz";
}
if(i % 5 == 0){
result[i - start] = "Buzz";
}
if(i % 3 == 0 && i % 5 == 0){
result[i - start] = "FizzBuzz";
}
} return result;
}
String[] nwarr = new String[end-start];
ReplyDeletefor(int i=0; i<nwarr.length; i++){
if(start % 3 == 0 && start % 5 == 0) nwarr[i] = "FizzBuzz";
else if(start % 5 == 0) nwarr[i] = "Buzz";
else if(start % 3 == 0) nwarr[i] = "Fizz";
else {
nwarr[i] = String.valueOf(start);
}
start++;
}
return nwarr;
int diff = end - start;
ReplyDeleteString[] res = new String[diff];
int index = 0;
for (int i = start; i < end; i++) {
if (i % 5 == 0 && i % 3 == 0) {
res[index] = "FizzBuzz";
index++;
}
else if (i % 3 == 0) {
res[index] = "Fizz";
index++;
} else if (i % 5 == 0) {
res[index] = "Buzz";
index++;
} else {
res[index] = String.valueOf(i);
index++;
}
}
return res;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] result = new String[end-start];
for (int i = start; i < end; i++) {
if ( i%3 == 0 && i%5 == 0 ) {
result[i-start] = "FizzBuzz";
} else if ( i%3 == 0 ) {
result[i-start] = "Fizz";
} else if ( i%5 == 0 ) {
result[i-start] = "Buzz";
} else {
result[i-start] = String.valueOf(i);
}
}
return result;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] fizzB = new String[end-start];
for (int i = start, j=0; i < end; i++, j++) {
fizzB[j] = String.valueOf(i);
if (i % 3 == 0 && i % 5 == 0) {
fizzB[j] = "FizzBuzz";
} else if (i % 3 == 0) {
fizzB[j] = "Fizz";
} else if (i % 5 == 0) {
fizzB[j] = "Buzz";
}
}
return fizzB;
String arr[] = new String[end-start];
ReplyDeletefor(int i = 0;i<arr.length;i++)
{
arr[i] = String.valueOf(start+i);
if((start+i)%3==0)arr[i] = "Fizz";
if((start+i)%5==0)arr[i] = arr[i].replaceAll("\\d","") + "Buzz";
}
return arr;
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] ret = new String[end - start];
for (int i = start; i < end; i++)
ret[i - start] = i % 3 == 0 ? (i % 5 == 0 ? "FizzBuzz" : "Fizz") : (i % 5 == 0 ? "Buzz" : String.valueOf(i));
return ret;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] res = new String[end-start];
for (int i=0; i<res.length; i++) {
if (start%3==0) {
if (start%5==0) res[i] = "FizzBuzz";
else res[i] = "Fizz";
}
else if (start%5==0) res[i] = "Buzz";
else res[i] = String.valueOf(start);
start++;
}
return res;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] result = new String[end-start];
for(int i=start; i<end; i++){
String value = "";
if(i%3==0) value += "Fizz";
if(i%5==0) value += "Buzz";
if(value=="") value = i+"";
result[i-start]=value;
}
return result;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString[] result = new String[end - start];
for (int i = start; i < end; i++){
if(i % 15 != 0){
result[i - start] = String.valueOf(i);
}
if(i % 3 == 0){
result[i - start] = "Fizz";
}
if(i % 5 == 0){
result[i - start] = "Buzz";
}
if(i % 15 == 0){
result[i - start] = "FizzBuzz";
}
} return result;
}
With Java Stream
ReplyDeletepublic static String[] fizzBuzz(int start, int end) {
String[] fb = IntStream.range(start, end)
.boxed()
.map(i -> {
if (i % (3 * 5) == 0) {
return "FizzBuzz";
} else if (i % 3 == 0) {
return "Fizz";
} else if (i % 5 == 0) {
return "Buzz";
} else {
return Integer.toString(i);
}
})
.toArray(String[]::new);
return fb;
}
public String[] fizzBuzz(int start, int end) {
ReplyDeleteString str[] = new String[end-start];
for(int i=0 ; i<str.length ;i++){
if(start%3==0 && start%5==0){
str[i] = "FizzBuzz";
}
else if(start%3==0 ){
str[i] = "Fizz";
}
else if( start%5==0){
str[i] = "Buzz";
}
else {
str[i] = String.valueOf(start);
}
start++;
}
return str;
}