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
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!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started