[RUBY] Array pattern calculation and extraction program

It is an output because I programmed it in practice. This time, the code was refreshing personally.

qiita.rb


number = gets.chomp.split(' ').map{|i| i .to_i}
total = []

number.permutation(4) do |n|
  total.push((n[0]*10+n[1]) + (n[2]*10 + n[3]))
end

puts total.max

-Cut the four numbers you entered into two and give each one a tens place or a ones place. ・ Add the resulting two-digit numbers. ・ Apply it to all four numbers entered in advance -The one with the largest total value was output.

I think it's easy to understand, so the processing content is as follows.

qiita.rb


# Entered value
 1 2 3 4  

#Pattern output
46 ←12+ 34
55 ←34+ 21
37 or less all patterns
55
37
46
55
64
37
64
37
55
55
73
46
73
46
55
64
73
55
73
55
64

#Maximum value 73

In doing this, I somehow came up with the flow of processing. If I was just trying to find out how to extract the pattern of the array sequence I found a permutation.

Reference article https://qiita.com/shshimamo/items/5a458ecc88e7c24d5112

I want to be able to do it in less time ... If you have any advice, please leave a comment!

Recommended Posts

Array pattern calculation and extraction program
Ruby score calculation program