The chromatic scale is a 12-note scale in music in which all notes are evenly spaced: that is, the ratio of the frequency between any two consecutive notes is constant. The notes are typically labeled in the following sequence: A, A#, B, C, C#, D, D#, E, F, F#, G, G# After G#, the labels loop back and start over with A (one octave higher). To convert between musical keys, you can shift all notes in a piece of music a constant number of steps along the scale above. For example, the sequence of notes E, E, F, G, G, F, E, D, C, C, D, E, E, D, D can be converted to another musical key by shifting everything up three steps: E, E, F, G, G, F, E, D, C, C, D, E, E, D, D G, G, G#, A#, A#, G#, G, F, D#, D#, F, G, G, F, F Notice that G was converted to A#, since going three steps up required us to loop off of the top of the scale back to the bottom: G -> G# -> A -> A#. Technically we should note that this would be A# of the next octave up, but we’ll ignore that for this problem. Write a function change_key(notes, up) which takes in a list of strings notes, each of which represents one of the 12 possible musical notes listed above, and an integer up, which represents how many steps up the scale the notes should be shifted (if up is a negative number, then need to shift notes down instead). change_key should return a new list of strings representing the notes shifted an appropriate number of times. Hints:
Problem B Musical Key Conversion
The chromatic scale is a 12-note scale in music in which all notes are evenly spaced: that is, the
ratio of the frequency between any two consecutive notes is constant. The notes are typically labeled in the following sequence:
A, A#, B, C, C#, D, D#, E, F, F#, G, G#After G#, the labels loop back and start over with A (one octave higher). To convert between musical keys, you can shift all notes in a piece of music a constant number of steps along the scale above. For example, the sequence of notes
E, E, F, G, G, F, E, D, C, C, D, E, E, D, Dcan be converted to another musical key by shifting everything up three steps:
E, E, F, G, G, F, E, D, C, C, D, E, E, D, D G, G, G#, A#, A#, G#, G, F, D#, D#, F, G, G, F, FNotice that G was converted to A#, since going three steps up required us to loop off of the top of the scale back to the bottom: G -> G# -> A -> A#. Technically we should note that this would be A# of the next octave up, but we’ll ignore that for this problem.
Write a function change_key(notes, up) which takes in a list of strings notes, each of which represents one of the 12 possible musical notes listed above, and an integer up, which represents how many steps up the scale the notes should be shifted (if up is a negative number, then need to shift notes down instead). change_key should return a new list of strings representing the notes shifted an appropriate number of times.
Hints:
● Use this list:
● This will probably go easier if you have some system to convert notes into numbers and back again, and use modular arithmetic ( %). For example, in the list above, 'G' appears at index 10. To shift it up by 3 notes, I would look for the element at index 10+3=13. There is no index 13, but we can divide by 12 and take the remainder to figure out what index we’d end up at after looping back around to the start of the list: 13%12=1, so we get the element at index 1, which is 'A#'.
>>> change_key(['G', 'G', 'D', 'D', 'E', 'E', 'D'], 2) ['A', 'A', 'E', 'E', 'F#', 'F#', 'E']
>>> change_key(['A', 'A', 'A', 'F', 'C', 'A', 'F', 'C', 'A'], -4) ['F', 'F', 'F', 'C#', 'G#', 'F', 'C#', 'G#', 'F'] >>> change_key(['E', 'E', 'F', 'G', 'G', 'F', 'E', 'D', 'C', 'C', 'D', 'E', 'E', 'D', 'D'], 3) ['G', 'G', 'G#', 'A#', 'A#', 'G#', 'G', 'F', 'D#', 'D#', 'F', 'G', 'G', 'F', 'F'] >>> change_key(['E', 'F#', 'A', 'F#', 'C#', 'C#', 'B', 'E', 'F#', 'A', 'F#', 'B', 'B', 'A', 'G#', 'F#'], -1987) ['A', 'B', 'D', 'B', 'F#', 'F#', 'E', 'A', 'B', 'D', 'B', 'E', 'E', 'D', 'C#', 'B']Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images