Homework: Fizzbuzz

HW
Author

Your Name

Published

September 13, 2024

Note: This assignment must be submitted in github classroom.

Instructions:

Introduction to 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”

Start by filling in the following table for the numbers 1:30 manually, to get a feel for the task.

Fizzbuzz for 1:30
Input Output
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
28
29
30

On paper or using a tool such as https://excalidraw.com, create a program flow map for the sequence of if-statements you need to evaluate for fizzbuzz. Add the picture to the folder containing this file, and name the picture flowchart.png. Add the picture to Git and commit/push your changes.

Program Flow map for FizzBuzz

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

x <- 3

# FizzBuzz code goes here
x = 3

# FizzBuzz code goes here

Modify the code above so that the result is stored in a value y.

x <- 3
y <- NA
# FizzBuzz code goes here



print(paste("For x = ", x, " my code produces ", y, sep = ""))
[1] "For x = 3 my code produces NA"
import numpy as np
x = 3
y = np.nan
# FizzBuzz code goes here



print("For x = "+ str(x)+ " my code produces "+ str(y))
For x = 3 my code produces nan

A vector of FizzBuzz

The code in the previous problem only solves FizzBuzz for a single value of x. Extend your code using a loop so that it will work for all values in a vector xx, storing values in a corresponding vector yy.

You can copy/paste code from previous chunks to make this chunk easier.

xx <- 1:30
yy <- rep(NA, times = 30)

# FizzBuzz code goes here


# Printing the results in a data frame
res <- cbind(x = xx, result = yy)
res
       x result
 [1,]  1     NA
 [2,]  2     NA
 [3,]  3     NA
 [4,]  4     NA
 [5,]  5     NA
 [6,]  6     NA
 [7,]  7     NA
 [8,]  8     NA
 [9,]  9     NA
[10,] 10     NA
[11,] 11     NA
[12,] 12     NA
[13,] 13     NA
[14,] 14     NA
[15,] 15     NA
[16,] 16     NA
[17,] 17     NA
[18,] 18     NA
[19,] 19     NA
[20,] 20     NA
[21,] 21     NA
[22,] 22     NA
[23,] 23     NA
[24,] 24     NA
[25,] 25     NA
[26,] 26     NA
[27,] 27     NA
[28,] 28     NA
[29,] 29     NA
[30,] 30     NA
import pandas as pd
xx = np.array(range(30)) + 1
yy = [np.nan]*30

# FizzBuzz code goes here


# Printing the results in a data frame
res = pd.DataFrame({"x": xx, "result": yy})
res
     x  result
0    1     NaN
1    2     NaN
2    3     NaN
3    4     NaN
4    5     NaN
5    6     NaN
6    7     NaN
7    8     NaN
8    9     NaN
9   10     NaN
10  11     NaN
11  12     NaN
12  13     NaN
13  14     NaN
14  15     NaN
15  16     NaN
16  17     NaN
17  18     NaN
18  19     NaN
19  20     NaN
20  21     NaN
21  22     NaN
22  23     NaN
23  24     NaN
24  25     NaN
25  26     NaN
26  27     NaN
27  28     NaN
28  29     NaN
29  30     NaN

Functions and FizzBuzz

In the previous question, you extended your fizzbuzz code to iterate through a vector xx and produce a result yy. Can you generalize this, writing a function fizzbuzz that takes a variable x and returns a corresponding fizzbuzzified variable? Your function should be able to handle x that is a vector or a scalar value, and should store your solution in yy.

xx <- sample(1:100, 10) # get a random xx
yy <- rep(NA, 10)

fizzbuzz <- function(x) {
  # Your code goes here
}

yy <- fizzbuzz(x = xx)

# Printing the results in a data frame
res <- cbind(x = xx, result = yy)
res
        x
 [1,]  65
 [2,]   7
 [3,]  71
 [4,]  36
 [5,]  66
 [6,]  20
 [7,]   3
 [8,]  86
 [9,]  21
[10,] 100
import pandas as pd
from random import choices
xx = np.array(choices(range(100), k = 10)) + 1


def fizzbuzz(x):
  y = [np.nan]*len(x) # this just defines something to return
  # Your code goes here
  return y

yy = fizzbuzz(x = xx)

# Printing the results in a data frame
res = pd.DataFrame({"x": xx, "result": yy})
res
    x  result
0  32     NaN
1  98     NaN
2  31     NaN
3  42     NaN
4  40     NaN
5  36     NaN
6  36     NaN
7  13     NaN
8  53     NaN
9  33     NaN

Defensive Programming

You cannot always assume that the person using your functions knows what they’re doing. Add a check to the function you wrote in the last question so that it will handle non-numeric input by issuing an error message.

In R, you can use the function stopifnot() to halt function execution if there is an error; this will give you a basic error message.

stopifnot(2 > 3)
Error: 2 > 3 is not TRUE

In Python, you can use a try: statement with a except: clause. This functions like an if-else statement, where if no error occurs, the except statement is never executed.

try: 
  int("hello")
except ValueError: 
  print("Error: could not turn value into an integer")
Error: could not turn value into an integer

See more examples of this in the Input Validation section of the textbook.

xx <- sample(1:100, 10) # get a random xx
yy <- rep(NA, 10)

fizzbuzz <- function(x) {
  # Your code goes here
}

yy <- fizzbuzz(x = xx)

# Printing the results in a data frame
res <- cbind(x = xx, result = yy)
res
       x
 [1,] 88
 [2,] 10
 [3,] 38
 [4,] 36
 [5,] 48
 [6,] 66
 [7,] 21
 [8,] 78
 [9,] 40
[10,] 24
import pandas as pd
from random import choices
xx = np.array(choices(range(100), k = 10)) + 1


def fizzbuzz(x):
  y = [np.nan]*len(x) # this just defines something to return
  # Your code goes here
  return y

yy = fizzbuzz(x = xx)

# Printing the results in a data frame
res = pd.DataFrame({"x": xx, "result": yy})
res
    x  result
0  99     NaN
1  84     NaN
2  75     NaN
3  28     NaN
4  57     NaN
5  60     NaN
6  43     NaN
7  22     NaN
8  19     NaN
9  80     NaN