Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]


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 6th question (DoubleCamelCase Sort) of "1st Algorithm Practical Test". 1st Algorithm Practical Test Past Question

This time, I would like to summarize what I was able to learn based on the helpful answers.


Given the string S. It is a concatenation of one or more words (without spaces in between). Here, each word has two or more letters, only the first and last letters are uppercase letters, and all other letters are lowercase letters.

Constraint -S is a character string with a length of 2 or more and 100,000 or less. -Each letter S is uppercase or lowercase. ・ S is a concatenation of words as described in the problem sentence.

The input is given in the following form.


Input example
Output example


Here is the answer that I will refer to this time.

puts gets.scan(/[A-Z][a-z]*[A-Z]/).sort_by(&:upcase).join

The received character string is arranged as an element for each word. After sorting using the sort_by method, all the elements of the array are concatenated using the join method and output.

The following is a summary of each method and the expression in regular expressions.

scan method

The pattern specified in () is repeatedly matched to the object, and the matched substring is returned as an array.

p "test".scan(/../)
#=> ["te", "st"]
p "test".scan("t")
#=> ["t"]
p "testet".scan(/te./)
#=> ["tes", "tet"]

It's the best method for cases like this one that deal with words that start and end with a capital letter.

Regular expression part

The words handled this time are expressed as regular expressions as follows.


/ / //で囲むことで正規表現オブジェクトを生成することが出来ます。 In other words, it is a declaration that "what is enclosed in // is a regular expression".

[ ] The part between the square brackets [] is called the character class. An enumeration of one or more characters that matches any one character (1 of 0-9 above).

[A-Z] Represents a single uppercase letter.

[a-z] Represents a single lowercase alphabetic character.

It is a metacharacter that means that the previous expression is repeated 0 or more times. In this case, it means that lowercase letters are repeated 0 or more times. When an English word with three or more letters is shown, it means that it is always lowercase between uppercase letters.

Now you can match the word according to the problem specification.

sort_by method

It compares the contents of the array, sorts it, and creates a new array in the sorted state. In (), in order to make a correct comparison, the upcase method is applied to each element, and all capital letters are used before comparison.

p ["AA", "AC", "AaA"].sort_by(&:upcase)
#=>["AA", "AaA", "AC"]

join method

Returns a string in which the elements of the array are concatenated with the specified delimiter in parentheses (). If () is not described, it will be concatenated without a delimiter.

p ["AA", "AaA", "AC"].join(",")
p ["AA", "AaA", "AC"].join


The above is a summary of the methods learned in the sixth question (DoubleCamelCase Sort) of the "1st Algorithm Practical Test". I would like to use regular expressions and various methods to make the answer as simple and easy to understand as the answer I referred to this time.

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

Recommended Posts

Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
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 14 [3rd Algorithm Practical Test Sprinkler] Hash creation, key / value addition
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby Learning # 8 Working With String
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Solve with Ruby AtCoder ABC177 D UnionFind
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)