"Introduction to Ruby for those who want to become a professional" I am a programming beginner after learning the so-called cherry book. When I wanted to move my hands and put into practice what I input, I found an article by the author. "If you have trouble with output material !? I collected programming problems for Ruby beginners (10 questions in total)"
I tried to solve this second problem.
First question: Calendar creation problem (fun Ruby practice problem) Second question: Karaoke machine creation problem Third question: Bingo card creation problem Fourth question: Bonus drink problem Fifth question: Phonebook creation problem
For details, see Actual problem statement. If you know the problem, [Jump to answer example](# answer example).
Karaoke has a function to change the key. +1 will raise the key by one. -1 will lower the key by one. For example, if you raise the key of the melody "Dremifaso" by two, it becomes "Remifa #Sora". Katakana like "Dremi Faso" is difficult to handle in the program, so let's replace it with the English reading, that is, the alphabet. Doremi Faso → C D E F G Remifa #Sora → D E F # G A
The melody of Kaeru no Uta is expressed by the following character strings. Change the key for the specified size such as (+6) or (-11).
"C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
The execution example looks like this.
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C " karaoke = KaraokeMachine.new(melody) karaoke.transpose(2)
karaoke.transpose(-1)
#1 octave(12 tones)You can change the above karaoke.transpose(14)
# Answer example
It turned out to be something like this.
```ruby
class KaraokeMachine
def initialize(melody)
@melody = melody
end
def transpose(amount)
scales = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
@melody.split(/(\w#?)/).map{
|m| /(\w#?)/.match?(m) ?
scales[(scales.find_index(m) + amount) % scales.length] : m
}.join
end
end
At first I thought it was completed with the code below, but
class KaraokeMachine
def initialize(melody)
@melody = melody
end
def transpose(amount)
scales = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
@melody.split("").map{
|m| m == " " || m == "|" ?
m : scales[(scales.find_index(m) + amount) % scales.length]
}.join
end
end
In this case, I noticed that the alphabet
and #
were separated when the following melody came, and corrected it using a regular expression.
melody = "F# G# A# B |A# G# F# |A# B C# D# |C# B A# |F# F# |F# F# |F#F#G#G#A#A#BB|A# G# F# "
I will write it as my memorandum.
The scale in this problem is a repetition of "Dodo #Rele #Mifafa #Soso #Lara #Shi" including the sharp.
scales = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]`
I made one array like this.
The character string received by melody =
is separated into scale
andother
to make an array.
melody = "C D E F |E D C |E F G A |G F E |C C |C C |CCDDEEFF|E D C "
melody.split(/(\w#?)/)
=> ["", "C", " ", "D", " ", "E", " ", "F", " |", "E", " ", "D", " ", "C", " |", "E", " ", "F", " ", "G", " ", "A", " |", "G", " ", "F", " ", "E", " |", "C", " ", "C", " |", "C", " ", "C", " |", "C", "", "C", "", "D", "", "D", "", "E", "", "E", "", "F", "", "F", "|", "E", " ", "D", " ", "C", " "]
Each element of the array is found in the block of the map method
to find the boolean value of the condition, and each value is added to the new array.
scale
, change the specified minute key.otherwise
, add the element as it is.
Will be executed by the following code.@melody.split(/(\w#?)/).map{
|m| /(\w#?)/.match?(m) ?
scales[(scales.find_index(m) + amount) % scales.length] : m
}.join
Dividing the number obtained by adding the ʻindex of the
scaleand the increment / decrement of the specified key by the
length of the
scales gives the ʻindex
of the corresponding scale
to which the key was changed. ..
Therefore, if you substitute the value of scales []
and specify ʻindex, you can find the
scale` with the key change.
Finally, join
thisscale
and other
again and convert it to a character string, and you're done.
I think there are some places that have become a little forcible. Please let me know if you have any mistakes or opinions.
Recommended Posts