"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 fourth 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
When you buy a drink at a candy store, you will get a new drink with 3 empty bottles. Create a program that calculates the total number of drinks you can drink from the number of drinks you purchased first. Also, if you purchase 100 bottles for the first time, how many bottles can you drink in total?
** Remarks ** This problem is based on the math problem in the third grade of elementary school.
** Example **
|Number of purchases|Number of drinks| |--:|--:| | 0 | 0 | | 1 | 1 | | 3 | 4 | | 11 | 16 | | 100 | (Calculate by program) |
It turned out to be something like this
class BonusDrink
def self.total_count_for(amount)
if amount.zero?
0
elsif amount.odd?
3 * amount / 2
elsif amount.even?
3 * (amount - 1) / 2 + 1
end
end
end
Here is the test code.
require_relative '../Bonus_drink/drink'
RSpec.describe BonusDrink do
it "total_count_for" do
expect(BonusDrink.total_count_for(0)).to eq 0
expect(BonusDrink.total_count_for(1)).to eq 1
expect(BonusDrink.total_count_for(3)).to eq 4
expect(BonusDrink.total_count_for(11)).to eq 16
end
end
I think that it is established as a program, but there is a slight feeling that this solution was good as the purpose of the programming problem. (Mostly I moved the paper and pen to solve it mathematically.)
As you can see in the problem, you can get a " exchange 3 empty bottles for a new one "
.
The essence of this problem is that even one new bottle that you exchange and receive will be subject to exchange.
So
Number of purchased: 3 Number of newly exchanged:1 Total number of drinks: 4
However, if you add the bottles you received and reach 3 bottles, you can exchange another one. For example
Number of purchased: 5 Number of newly exchanged:1 Additional books exchanged: 1 Total number of drinks: 7
It means that. Therefore, as the number of purchases increases, additional exchanges will be repeated many times.
I thought that it was a problem to express this by programming and repeatedly substitute the total number of bottles and the number of replaced bottles to get closer to the maximum total number, but I could not think of it and gave up. I would appreciate it if you could let me know in the comments if you have any examples of answers.
So how did I solve it? I wrote down the number of bottles I bought and the number of bottles I could drink.
Number of purchases | Number of drinks |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 4 |
4 | 5 |
5 | 7 |
6 | 8 |
7 | 10 |
8 | 11 |
9 | 13 |
10 | 14 |
11 | 16 |
Then, *** "When the number of purchased bottles n is odd, the value of (n * 3) matches the total number of drinkable bottles at n and the number of drinkable bottles at (n + 1)" *** I found out. By the way, when it is 0, it does not hold. (There are various other rules, but this time I will use this equation.) In particular,
Number of purchases | Number of drinks |
---|---|
5 | 7 |
6 | 8 |
5 * 3 = 15
7 + 8 = 15
It will be! In other words, *** "If you divide n by 3 and divide it by 2, you can find the number of drinks you can drink" ***. If you translate this into programming,
#When odd
3 * amount / 2
#When it is an even number, it is calculated from the previous odd number.
3 * (amount - 1) / 2 + 1
[Example of the above answer](#Example of answer) is a summary of the above contents.
I understand that such a law does not hold, but I don't quite understand ** "Why this happens" **. If you can explain in an easy-to-understand manner, I would like to ask you to teach with embarrassment.
Thank you!
Recommended Posts