[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!

In this article, I've summarized the FizzBuzz issue!

You may have seen this problem very early in your learning of programming.

The FizzBuzz problem is not too difficult for beginners and is an effective learning material for programming.

I will do my best to make it easy for beginners to understand, so please stay with me until the end!

What is the FizzBuzz problem?

The FizzBuzz problem in programming is "Fizz" if you enter a number that can be divided by 3, "Buzz" if you enter a number that can be divided by 5, or a number that can be divided by both (that is, divide by 15). It's a problem of writing a program that responds with "FizzBuzz" when you enter a number that can be used.

It is often mentioned when you buy an introductory reference book for Ruby, so many of you may say that you have actually solved it.

How to write and think about FizzBuzz

First, let's take a look at the completed code.

FizzBuzz.rb


def FizzBuzz(n)

 if n % 15 == 0
  'FizzBuzz'
 elsif n % 3 == 0
  'Fizz'
 elsif n % 5 == 0
  'Buzz'
 else 
  n.to_s
 end

end

First, when n is divided by 15, too much is 0, that is, if a number that can be divided by 15 is input, it is instructed to return'FizzBuzz'.

Next, if you enter a number that can be divided by 3, it will return'Fizz', and if you enter a number that can be divided by 5, it will return'Buzz'.

Finally, if any other number is entered, it is instructed to convert that number to a string and return it.

Let's actually run this program.

First, under the method I just wrote, I will write an instruction using the method.

FizzBuzz.rb


def FizzBuzz(n)

 if n % 15 == 0
  'FizzBuzz'
 elsif n % 3 == 0
  'Fizz'
 elsif n % 5 == 0
  'Buzz'
 else 
  n.to_s
 end

end

FizzBuzz(1)
FizzBuzz(3)
FizzBuzz(4)
FizzBuzz(5)
FizzBuzz(10)
FizzBuzz(15)

Then, give the following command to the terminal.


$ ruby lib/FizzBuzz.rb

This is a command to execute a file called FizzBuzz.rb directly under a directory (folder) called lib.

When you actually run it, you should see the following results.

1
Fizz
4
Buzz
10
FizzBuzz

If you get the same result, you will be successful!

Summary

The FizzBuzz question is a beginner's entry-level programming exercise.

Write the program so that if you enter a multiple of 3, Fizz will be output, if it is 5, Buzz will be output, and if it is a multiple of both, FizzBuzz will be output.

How was it? I would appreciate it if you could let me know if there are any points that are difficult to understand!

It will be a material for rewriting this article, and it will lead to my own growth, so I would love to hear from you!

Recommended Posts

[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to solve the problem of Google Tech Dev Guide
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
I tried the FizzBuzz problem
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to summarize the basic grammar of Ruby briefly
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried to build the environment of PlantUML Server with Docker
I tried to check the operation of gRPC server with grpcurl
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I tried to measure and compare the speed of GraalVM with JMH
[Competition Pro] Solve the knapsack problem with Ruby
I checked the number of taxis with Ruby
I tried to compare the infrastructure technology of engineers these days with cooking.
I tried to get the distance from the address string to the nearest station with ruby
I tried to summarize the state transition of docker
[Java] I tried to solve Paiza's B rank problem
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
[At Coder] Solve the ABC183 D problem with Ruby
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[At Coder] Solve the ABC182 D problem with Ruby
I tried to check the operation of http request (Put) with Talented API Tester
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
Sazae-san's rock-paper-scissors I tried to verify the theoretical value and the measured value of the probability of the same hand 5 consecutive times with Ruby
I tried DI with Ruby
Explanation of the FizzBuzz problem
Let's solve the FizzBuzz problem!
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
[Swift] I tried to implement the function of the vending machine
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to build the environment of WSL2 + Docker + VSCode
[Ruby] I want to reverse the order of the hash table
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to explain what you can do in a popular language for web development from a beginner's point of view.
I tried to express the result of before and after of Date class with a number line
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to implement the image preview function with Rails / jQuery
I tried to interact with Java
I tried to explain the method
A memorandum of the FizzBuzz problem
I tried to reimplement Ruby Float (arg, exception: true) with builtin
I tried to take a look at the flow of Android development environment construction with Android Studio
I tried to summarize the methods of Java String and StringBuilder
I want to change the value of Attribute in Selenium of Ruby
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
[Rails] I tried to summarize the passion and functions of the beginners who created the share house search site!
I tried to make a sample program using the problem of database specialist in Domain Driven Design
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
I tried to make a parent class of a value object in Ruby
I tried to summarize the key points of gRPC design and development
[Ruby] I want to extract only the value of the hash and only the key