<- 3
x
# FizzBuzz code goes here
Homework: Fizzbuzz
Note: This assignment must be submitted in github classroom.
Instructions:
You can answer the questions below in either R or Python. I will give you 50% extra credit if you provide answers in both languages. Otherwise, please feel free to delete the code chunks corresponding to the language you don’t wish to work in.
Once you have finished this assignment, render the document (Ctrl/Cmd-Shift-K or the Render button).
Commit the qmd file and any other files you have changed to the repository and push your changes.
In Canvas, submit a link to your github repository containing the updated files.
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.
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.
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.
= 3
x
# FizzBuzz code goes here
Modify the code above so that the result is stored in a value y
.
<- 3
x <- NA
y # 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
= 3
x = np.nan
y # 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.
<- 1:30
xx <- rep(NA, times = 30)
yy
# FizzBuzz code goes here
# Printing the results in a data frame
<- cbind(x = xx, result = yy)
res 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
= np.array(range(30)) + 1
xx = [np.nan]*30
yy
# FizzBuzz code goes here
# Printing the results in a data frame
= pd.DataFrame({"x": xx, "result": yy})
res 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
.
<- sample(1:100, 10) # get a random xx
xx <- rep(NA, 10)
yy
<- function(x) {
fizzbuzz # Your code goes here
}
<- fizzbuzz(x = xx)
yy
# Printing the results in a data frame
<- cbind(x = xx, result = yy)
res 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
= np.array(choices(range(100), k = 10)) + 1
xx
def fizzbuzz(x):
= [np.nan]*len(x) # this just defines something to return
y # Your code goes here
return y
= fizzbuzz(x = xx)
yy
# Printing the results in a data frame
= pd.DataFrame({"x": xx, "result": yy})
res 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.
<- sample(1:100, 10) # get a random xx
xx <- rep(NA, 10)
yy
<- function(x) {
fizzbuzz # Your code goes here
}
<- fizzbuzz(x = xx)
yy
# Printing the results in a data frame
<- cbind(x = xx, result = yy)
res 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
= np.array(choices(range(100), k = 10)) + 1
xx
def fizzbuzz(x):
= [np.nan]*len(x) # this just defines something to return
y # Your code goes here
return y
= fizzbuzz(x = xx)
yy
# Printing the results in a data frame
= pd.DataFrame({"x": xx, "result": yy})
res 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