"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
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
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