[Ruby] Sequence pattern calculation and extraction program

less than 1 minute read

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

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

・ Divide the entered four numbers into two parts and give the ten’s place and the one’s place respectively. ・Add the two-digit number you made. ・Apply it to all four numbers entered in advance ・The one with the largest total value was output.

I think it will be easy to understand, so it is the following processing contents.

qiita.rb


# Entered value
 1 2 3 4

# Pattern output
4612+34
5534+21
All patterns below 37
55
37
46
55
64
37
64
37
55
55
73
46
73
46
55
64
73
55
73
55
64

# Maximum value 73
  • Actually, the pattern output is also omitted.

When doing this, I somehow came up with the flow of processing, I was searching for how to extract the array pattern, I found permutation.

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

I want to be able to do it in less time, We welcome your comments if you have any advice!

Tags:

Updated: