Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements

Introduction

As a part of Ruby learning, we will challenge "algorithm practical test". We will output what we have learned in the learning for that purpose. This time, from the third question (third) of the "1st Algorithm Practical Test". 1st Algorithm Practical Test Past Question

I will introduce my answer and the expressions and methods I learned while answering.

problem

Six different integers A, B, C, D, E, F are given. Create a program that looks up the third largest of these.

Constraint ・ 1 ≤ A, B, C, D, E, F ≤ 100 ・ A, B, C, D, E, F are all different -All the values being entered are integers.

The input is given in the following form.

A B C D E F

Input example
4 18 25 20 9 13
Output example
=> 13

answer

First of all, from the answer I submitted.

ary = gets.split(" ").map(&:to_i)
r_ary = ary.sort
print r_ary[3]

Receives the input character string as an integer in an array It sorts using the sort method and outputs the third largest integer.

From here, I will summarize the sort method used for the first time this time.

sort method

Sorts the contents of the array. Comparisons between elements are done using the <=> operator, and sort produces and returns a sorted array.

Sorting strings

If you sort an array containing character strings, it will be sorted in the order of "a, b, c ..." or "a, i, u ...".

ary1 = [ "d", "a", "e", "c", "b" ]
p ary1.sort
#=> ["a", "b", "c", "d", "e"]

If the numbers are in the array as elements of the string, it looks like this:

ary2 = ["9", "7", "10", "11", "8"]
p ary2.sort
#=> ["10", "11", "7", "8", "9"]                        

Sorting integers

If it is included in the array as an integer element from the beginning, it can be written as concisely as the answer, If it's in an array as a string, use block {} to do the following:

ary2 = ["9", "7", "10", "11", "8"]
p ary2.sort{|a, b| a.to_i <=> b.to_i }
#=> ["7", "8", "9", "10", "11"]

However, it is converted to an integer using the to_i method to make a comparison in the block. Note that the generated array reverts to a string.

The same can be done with the sort_by method.

ary2 = ["9", "7", "10", "11", "8"]
p ary2.sort_by{|a| a.to_i}
#=> ["7", "8", "9", "10", "11"]

Finally

The above is a summary of the sorts learned while solving the third question (third) of the "1st Algorithm Practical Test".

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

Recommended Posts

Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learning Ruby with AtCoder 14 [3rd Algorithm Practical Test Sprinkler] Hash creation, key / value addition
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Ruby] The role of subscripts in learning elements in arrays