Solving the FizzBuzz problem in R and Python

2020-08-24 practice

Why FizzBuzz and friends?

The FizzBuzz problem is one of those classic programming problems that is awesome for beginners of pretty much any language. Even though it is rather simple, it can also be a good refresher on some basic programming concepts.

Here is the actual problem:

Write a short program that prints each number from 1 to 100 on a new line. For each multiple of 3, print “Fizz” instead of the number. For each multiple of 5, print “Buzz” instead of the number. For numbers which are multiples of both 3 and 5, print “FizzBuzz” instead of the number.

Why is it a good problem for beginners?

The problem focuses on many fundamentals that are ubiquitous in just about any programming language:

  • conditionals
  • functions
  • booleans
  • data types
  • operators
  • iteration

There are also different ways to arrive at the same answer, which I would argue should be viewed as a good thing.


When learning the ropes of a new programming language, trying to solve problems like the FizzBuzz problem can help you gauge where you are at in your understanding of the basics. That being said, the best way to learn new skills is by working on real world projects. Whether that means wrangling and exploring messy data with R, or developing a basic game with Python, the idea is to practice what you are learning as quickly as you can. Here is an example of a small project that I worked on to improve my web scraping skills.

Don’t fall down the rabbit hole of never ending videos or tutorials without applying what you learn towards something bigger. Establish a solid foundation of working with real data, real projects, and real people. It’s not MOOCs all the way down.

Small practice problems and personal projects can be both fun and challenging. And remember, googling is allowed! While having a solid understanding and ability of the basics is critical, memorizing tons and tons of code is not worth your time. Allow Google and Stack Overflow to become your best friends. Learning how to learn and effectively solve problems is much more important than rote memorization of tons of code.


STOP! I mean, stop if you want to. If you scroll down more, you will see solutions to the FizzBuzz problem in both R and Python. I would encourage you to try to solve the problem yourself before continuing. Choose R or Python, or both (or something else)!

Too easy? Time yourself. If you are multilingual, you could even solve the problem in multiple languages and see how long it takes you in each. Have fun with it.

I will probably place an ad or two here in order to put more space in between you and the solutions.


hackerearth is a popular place to find more programming problems.

CodeChef and w3schools are also good resources for practice and learning.


FizzBuzz solutions

Keep in mind, there are multiple roads that lead to the same destination. What road did you take?

R solution

Here is one way to do it in R:

fizz_buzz <- function(num) {
    if (num %% 3 == 0 & num %% 5 == 0) {
        print("FizzBuzz")
    } else if (num %% 3 == 0) {
        print("Fizz")
    } else if (num %% 5 == 0) {
        print("Buzz")
    } else {
        print(num)
    }
}

purrr::walk(1:100, fizz_buzz)
## [1] 1
## [1] 2
## [1] "Fizz"
## [1] 4
## [1] "Buzz"
## [1] "Fizz"
## [1] 7
## [1] 8
## [1] "Fizz"
## [1] "Buzz"
## [1] 11
## [1] "Fizz"
## [1] 13
## [1] 14
## [1] "FizzBuzz"
## [1] 16
## [1] 17
## [1] "Fizz"
## [1] 19
## [1] "Buzz"
## [1] "Fizz"
## [1] 22
## [1] 23
## [1] "Fizz"
## [1] "Buzz"
## [1] 26
## [1] "Fizz"
## [1] 28
## [1] 29
## [1] "FizzBuzz"
## [1] 31
## [1] 32
## [1] "Fizz"
## [1] 34
## [1] "Buzz"
## [1] "Fizz"
## [1] 37
## [1] 38
## [1] "Fizz"
## [1] "Buzz"
## [1] 41
## [1] "Fizz"
## [1] 43
## [1] 44
## [1] "FizzBuzz"
## [1] 46
## [1] 47
## [1] "Fizz"
## [1] 49
## [1] "Buzz"
## [1] "Fizz"
## [1] 52
## [1] 53
## [1] "Fizz"
## [1] "Buzz"
## [1] 56
## [1] "Fizz"
## [1] 58
## [1] 59
## [1] "FizzBuzz"
## [1] 61
## [1] 62
## [1] "Fizz"
## [1] 64
## [1] "Buzz"
## [1] "Fizz"
## [1] 67
## [1] 68
## [1] "Fizz"
## [1] "Buzz"
## [1] 71
## [1] "Fizz"
## [1] 73
## [1] 74
## [1] "FizzBuzz"
## [1] 76
## [1] 77
## [1] "Fizz"
## [1] 79
## [1] "Buzz"
## [1] "Fizz"
## [1] 82
## [1] 83
## [1] "Fizz"
## [1] "Buzz"
## [1] 86
## [1] "Fizz"
## [1] 88
## [1] 89
## [1] "FizzBuzz"
## [1] 91
## [1] 92
## [1] "Fizz"
## [1] 94
## [1] "Buzz"
## [1] "Fizz"
## [1] 97
## [1] 98
## [1] "Fizz"
## [1] "Buzz"

Python solution

Here is one way to do it in Python:

def fizzbuzz(num):
    if num % 3 == 0 and num % 5 == 0:
        return("FizzBuzz")
    elif num % 3 == 0:
        return("Fizz")
    elif num % 5 == 0:
        return("Buzz")
    else:
        return(num)
        
numbers = list(range(1, 101))

for number in numbers:
    print(fizzbuzz(number))
## 1
## 2
## Fizz
## 4
## Buzz
## Fizz
## 7
## 8
## Fizz
## Buzz
## 11
## Fizz
## 13
## 14
## FizzBuzz
## 16
## 17
## Fizz
## 19
## Buzz
## Fizz
## 22
## 23
## Fizz
## Buzz
## 26
## Fizz
## 28
## 29
## FizzBuzz
## 31
## 32
## Fizz
## 34
## Buzz
## Fizz
## 37
## 38
## Fizz
## Buzz
## 41
## Fizz
## 43
## 44
## FizzBuzz
## 46
## 47
## Fizz
## 49
## Buzz
## Fizz
## 52
## 53
## Fizz
## Buzz
## 56
## Fizz
## 58
## 59
## FizzBuzz
## 61
## 62
## Fizz
## 64
## Buzz
## Fizz
## 67
## 68
## Fizz
## Buzz
## 71
## Fizz
## 73
## 74
## FizzBuzz
## 76
## 77
## Fizz
## 79
## Buzz
## Fizz
## 82
## 83
## Fizz
## Buzz
## 86
## Fizz
## 88
## 89
## FizzBuzz
## 91
## 92
## Fizz
## 94
## Buzz
## Fizz
## 97
## 98
## Fizz
## Buzz
comments powered by Disqus