Learn Ruby with AtCoder Beginners Selection [Coins] Answer with short code

Introduction

As part of learning Ruby, we will challenge "competitive programming (competitive professional)". We will output what we have learned in the learning for that purpose. This time from the fifth question (Coins) of "At Coder Beginners Selection". https://atcoder.jp/contests/abs

problem

I have A for 500 yen, B for 100 yen, and C for 50 yen. How many ways are there to choose some of these coins and make the total amount X Yen? You cannot distinguish between coins of the same type.

Constraint 0 ≤ A,B,C ≤ 50 A + B + C ≥ 1 50 ≤ X ≤ 20,000 A, B, C are integers. X is a multiple of 50.

The input is given in the following form.

A
B
C
X

#Example
2
2
2
100
Output example
#In the case of the above example
=> 2

There are two ways to select the conditions. 0 sheets for 500 yen, 1 sheet for 100 yen, 0 sheets for 50 yen 0 for 500 yen, 0 for 100 yen, 2 for 50 yen

answer

First is the code I wrote first.

a = gets.to_i
b = gets.to_i
c = gets.to_i
x = gets.to_i

count = 0

a_array = []
b_array = []
c_array = []

for i in 0..a do
    a_array.push(500 * i)
end

for i in 0..b do
    b_array.push(100 * i)
end 

for i in 0..c do
    c_array.push(50 * i)
end

all_array = a_array.product(b_array, c_array)

all_array.each do |one_array|
    if one_array.inject(:+) == x
        count += 1
    end
end

print count

(1) For each type of coin, generate an array with the amount of money that can be selected from 0 to n (for ~ do ~ end).

I answered in the above flow. The methods I learned while answering this question are summarized below.

for statement

for variable in object do
Process to be executed
end

Specifies the range to repeat for the object. In the answer, from 0 to the number of coins you have, 0, 1 and 2 are taken out in order and put in a variable. After that, the process specified in the block is repeated, and so on. In the answer, the amount of money according to the number of sheets is put in order in the array generated immediately before.

push method

Array object.push(element, …)

The push method adds the specified element to the end of the array.

product method

#Example
[1,2].product([3,4],[5,6])
=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]

Returns an array that combines all the elements of multiple arrays.

each method

Array object.each do |variable|
Process to be executed
end

Take the elements one by one from the array, assign them to variables, and then Executes the specified processing for the number of elements.

inject method

Calculates and returns the sum of the elements for the array. This time, we use the notation using the symbol (: +).

#Example
array = [1, 2, 3]
result = array.inject(:+)
print result
=> 6

I found out by investigating it again, but it seems that it is easier to write using the sum method.

#Example
array = [1, 2, 3]
result = array.sum
print result
=> 6

Answer with a short code

From here, I will shorten this answer.

Receives multiple integer inputs arranged vertically

A, B, C, x = 4.times.map{gets.to_i}

times method

It is used to repeat the process a specified number of times.

#Example
3.times { puts "Hello, World!" } 
=>Hello, World!
=>Hello, World!
=>Hello, World!

Use the entered integer for direct judgment without generating an array

The answer was to generate an array and add the amount of money for all patterns, This made the answer very long. The length of the code was reduced by using the entered integer as it is for the judgment using the each statement.

(0..A).each{|a|
  (0..B).each{|b|
    (0..C).each{|c|
      x == (500*a + 100*b + 50*c) ? count +=1 : count += 0
    }
  }
}

Complete

A, B, C, x = 4.times.map{gets.to_i}
count = 0
(0..A).each{|a|
  (0..B).each{|b|
    (0..C).each{|c|
      x == (500*a + 100*b + 50*c) ? count +=1 : count += 0
    }
  }
}
print count

The code, which had 32 lines with spaces, is now 9 lines.

Finally

So far, we have introduced the methods and notations learned from Ruby learning [Coins] at AtCoder Beginners Selection. I always want to practice answering with short chords like this time. Looking at the answers of other people, some of them answered in one line. It's a long way to go, but it's also rewarding!

If you have any mistakes, I would be grateful if you could point them out.

Recommended Posts

Learn Ruby with AtCoder Beginners Selection [Coins] Answer with short code
Format Ruby with VS Code
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Solve with Ruby AtCoder ABC177 D UnionFind
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Learn Java with "So What" [For beginners]