I tried to solve the Ruby bingo card creation problem (there is an example of the answer)

Introduction

"Introduction to Ruby for those who want to become a professional" I am a programming beginner after learning the so-called cherry book. When I wanted to move my hands and put into practice what I input, I found an article by the author. "If you have trouble with output material !? I collected programming problems for Ruby beginners (10 questions in total)"

I tried to solve this third problem.

Click here for other issues First question: Calendar creation problem (fun Ruby practice problem) Second question: Karaoke machine creation problem Third question: Bingo card creation problem Fourth question: Bonus drink problem Fifth question: Phonebook creation problem

problem

For details from here

B: Any of 1 to 15 I: Any of 16 to 30 N: Any of 31-45 G: Any of 46-60 O: Any of 61-75 Make the following bingo cards according to the rule.

B | I | N | G | O 13 | 22 | 32 | 48 | 61 3 | 23 | 43 | 53 | 63 4 | 19 | | 60 | 65 12 | 16 | 44 | 50 | 75 2 | 28 | 33 | 56 | 68

 > The output of the Bingo.generate_card method must meet the following specifications in addition to the number rules mentioned above.
 --Generate a different card each time.
 --Separate each column with a pipe (|).
 --Numbers and "BINGO" characters are output right-justified.
 --The space is output in the middle (where it becomes FREE).



# Answer example
 It turned out to be something like this.

```ruby
class Bingo
  def self.generate_card
    title = "BINGO".split("").map{|bingo| sprintf("%2s",bingo)}.join(" | ")

    numbers = [*1..75].each_slice(15).to_a.map{|b| b.sample(5)}
    numbers[2][2] = "  "
    body = []
    for i in 0..4
      body << numbers.map{|number| sprintf("%2s",number[i])}.join(' | ')
    end

    [title,body].join("\n")
  end
end

at the end

The answers of other people were also posted on the link below. Ruby-We are pleased to announce the 3 best works of "Bingo Card Creation Problem"!

I didn't use the transpose method because I didn't know it, but looking at the answers of other people, there seems to be more room for refactoring.

If you have any opinions, please let us know in the comments. Thank you.

Recommended Posts

I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
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 problem of "multi-stage selection" with Ruby
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to solve the problem of Google Tech Dev Guide
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to summarize the basic grammar of Ruby briefly
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I want to make the frame of the text box red when there is an input error
I tried to make full use of the CPU core in Ruby
[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
I want to know the answer of the rock-paper-scissors app
Since the argument of link_to is nil (null) and an unexpected link was generated, I tried to verify it
Since the reading of JdbcCodeList of TERASOLUNA is slow, I tried to register multiple at once.
I made the server side of an online card game ①
[Swift] I tried to implement the function of the vending machine
I tried to build the environment of WSL2 + Docker + VSCode
[Ruby] I want to reverse the order of the hash table
I tried the FizzBuzz problem
I tried to build the environment of PlantUML Server with Docker
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
Want to know what Ruby n is the power of 2? (Power judgment of 2)
I tried to understand how the rails method "redirect_to" is defined
I tried to check the operation of gRPC server with grpcurl
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods of Java String and StringBuilder
I tried to make Numeron which is not good in Ruby
I want to change the value of Attribute in Selenium of Ruby
I tried to explain the method
I tried to make a sample program using the problem of database specialist in Domain Driven Design
There is LSP and I tried to create an environment to write Java with Vim (NeoVim), but after all I could not beat the IDE ...
I tried to make a parent class of a value object in Ruby
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
[Ruby] I tried to diet the if statement code with the ternary operator
[Ruby] I want to extract only the value of the hash and only the key
[Introduction to Java] I tried to summarize the knowledge that I think is essential
Get the type of an array element to determine if it is an array
How to solve the local environment construction of Ruby on Rails (MAC)!
I tried to visualize the access of Lambda → Athena with AWS X-Ray
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to measure and compare the speed of GraalVM with JMH
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
What I tried when I wanted to get all the fields of a bean
I tried to compare the infrastructure technology of engineers these days with cooking.
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
I tried to get the distance from the address string to the nearest station with ruby
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu
I want to solve the problem that JS is not displayed properly unless reloaded when transitioning with Turbolinks: link_to