Homework 6: FizzBuzz

HW
Week08
Author

Your Name

Published

March 22, 2024

Download the starter qmd file here

FizzBuzz

The “FizzBuzz Test” is a famous programming interview question.

Write a program that prints the numbers from 1 to 30. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”

Program Flow Map – Pre-Work

Create a program flow map (on paper, or using an online tool of your choice, such as Excalidraw) showing how FizzBuzz should be evaluated for a given integer x. You do not need to worry about the for loop in this program flow map. Upload your program flow map image to imgur.com and provide an image link here using markdown image syntax. You will need to link directly to the image (with image file extension) - right click on your uploaded image and choose ‘copy image address’ to get the proper link.

Substitute picture of a cat

A few questions to consider:

  • Can you think of another way to solve FizzBuzz using a different logical structure?
  • What do you need to know how to do in R/python for each solution?
    • Do you need to know how to concatenate strings?
    • Convert something from an integer to a string?
    • Output stuff to the command line?

Thinking through these requirements can help you decide which solution is the simplest and/or best.

List out the requirements of the solution in your program flow map here:

Fizzbuzz for one

In the chunk below, write code which will solve this problem for a single number, x. You should be able to change only the value of x and get the correct answer.

(Do this in R or python - just pick a language for this assignment. If you choose python, just change the chunk types below.)

x = 20

# Your code goes here

Fizzbuzz in loops

In the chunk below, write code which will solve this problem using a for loop that goes from 1 to 30. Store the result of each of the 30 numbers in a character vector instead of printing it out to the command line.

# R code goes here

Fizzbuzz in a function

Write a FizzBuzz function named fizzbuzz1. Your function should take a single number and return (not print out) a single string evaluating the number that was passed in. Use input validation to ensure that you are given a number, and provide a useful error if you are not given a number as the function argument.

# Your code goes here

This code will test your fizzbuzz function. Please make sure it runs as you expect it to run before you submit this file to Canvas!

fizzbuzz1(3)
## Error in fizzbuzz1(3): could not find function "fizzbuzz1"
fizzbuzz1(5)
## Error in fizzbuzz1(5): could not find function "fizzbuzz1"
fizzbuzz1(7)
## Error in fizzbuzz1(7): could not find function "fizzbuzz1"
fizzbuzz1(15)
## Error in fizzbuzz1(15): could not find function "fizzbuzz1"
fizzbuzz1(0)
## Error in fizzbuzz1(0): could not find function "fizzbuzz1"
fizzbuzz1("this should fail")
## Error in fizzbuzz1("this should fail"): could not find function "fizzbuzz1"

Vectorized FizzBuzz

Write a FizzBuzz function named fizzbuzz that can handle a vector of numbers and will return (not print out) a vector of character/string values. Check to ensure the input vector is numeric.

Hint: You might use a loop and call your function from the previous part on each value in the vector.

This code will test your fizzbuzz function. Please make sure it runs as you expect it to run before you submit this file to Canvas!

fizzbuzz(c(0, 3, 5, 7, 11, 15))
## Error in fizzbuzz(c(0, 3, 5, 7, 11, 15)): could not find function "fizzbuzz"

fizzbuzz(month.name) # this should fail
## Error in fizzbuzz(month.name): could not find function "fizzbuzz"