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.
S
Input example
FisHDoGCaTAAAaAAbCAC
Output example
=> AAAaAAbCACCaTDoGFisH
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.
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.
The words handled this time are expressed as regular expressions as follows.
/[A-Z][a-z]*[A-Z]/
/ / //で囲むことで正規表現オブジェクトを生成することが出来ます。 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.
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"]
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(",")
#=>AA,AaA,AC
p ["AA", "AaA", "AC"].join
#=>AAAaAAC
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