Use Swift code Create a Swift playground for the FizzBuzz problem for a value be
ID: 3879422 • Letter: U
Question
Use Swift code
Create a Swift playground for the FizzBuzz problem for a value between 0 and 100, including 0 and 100. A FizzBuzz program will print exactly one of the following: - print "Fizz" to the debug area if the value is divisible by three - print "Buzz" if the value is divisible by five - print "FizzBuzz" if the value is divisible by both three and five - otherwise, print "Blah" First, declare the following constant. This is the FizzBuzz value we want to compute. It can be any value between 0 and 100, here we set it to 15. let value = 15
Part 1: the straightforward approach Use an if / else-if / else statement to print out the correct FizzBuzz result for value per the instructions above. See the hints below. I should be able to change the value to any number between 0 and 100 and the correct FizzBuzz result should be displayed. Make sure to test your calculation for a few different values to make sure it works the way it should!
Part 2: the lookup table approach The approach used in Part 1 works well, but let's imagine that we have millions of people from around the world who want to compute a FizzBuzz result every day. For every user, we need to perform a calculation, and we'll wind up performing the same calculations over and over again, which might lead to poor performance. A common technique used in software development for situations like this is to calculate the result one time for each possible value, and store the results in an array, called a lookup table. Then, when we need the result for a new value, we can simply look it up in the lookup table instead of calculating it again. Here is one approach to do this: 1. Declare an array of type String named results with 101 elements and initialize each element to the empty string: "" (that's two double quotes right next to each other). Think about why we need 101 elements and not 100 elements. 2. Now, use a for-in loop to iterate over each element and replace the empty string with the correct value in the array. For example, results[0] should get the value "FizzBuzz", and results[1] should get the value "Blah" etc. all the way up to results[100] which should get the value "Buzz". 3. Now we have our array populated, i.e. each element holds the correct FizzBuzz result for its index. To find out the FizzBuzz result for value, we can look it up in our array by checking the value at results[value] -- print this to the debug area. There are other approaches that work; you can use any approach you like as long as your program calculates FizzBuzz results correctly and places them in an array. Do not calculate the FizzBuzz results yourself and manually add them to an array!
Explanation / Answer
Part 1 and Part 2 solved together
import java.util.Scanner;
public class MyClass {
private static String[] arr=new String[101]; \array which store result for 0 to 100 FizzBuzz
//this function is for storing FizzBuzz values for all number between 0 to 100.
private static void fillArray(){\function to generate FizzBuzz number between 0 to 100
for(int i=0;i<=100;i++)
arr[i]=fizzBuzz(i);\calls fizzBuzz number for each value from 0 to 100 which return whether number is FizzBuzz type
}
//below function is for part 1
private static String fizzBuzz(int n){\function that checks whether number is fizzBuzz or not
String result="";
if(n%3==0 && n%5==0)
result="FizzBuzz";\set result as FizzBuzz if number is divisible by 3 and 5 both
else if(n%3==0)
result="Fizz";\set result as Fizz if number is divisible by 3
else if(n%5==0)
result="Buzz";\set result as Buzz if number is divisible by 3 and 5 both
else
result="Blah";\set result as Blah if number is neither divisible by 3 nor 5
return result; eturn result corresponding to the number
}
//main function is for user giving numerous number of input
public static void main(String args[]) {
fillArray();\function called to set array with FizzBuzz
Scanner sc=new Scanner(System.in);\to take input from user
System.out.println("Enter the number you want to find fizzBuzz for");
int number=sc.nextInt();\take input number and store it as integer
while(number>=0 && number <=100){\shows number is FizzBuzz type if number entered between 0 to 100 else break loop
System.out.println(arr[number]);\print FizzBuzz value correspond to that number in array
number=sc.nextInt();\takes next input
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.